1

Word 2010 の Normal.dotm に次のマクロを追加しました。

Sub AutoOpen()
'
' AutoOpen Macro
'
'
   Dim aStory As Range
   Dim aField As Field

   For Each aStory In ActiveDocument.StoryRanges

      For Each aField In aStory.Fields
         aField.Update
      Next aField

   Next aStory

 ' set document as unchanged (prevents save dialog popping up when closing) - further changes will set this back
 ActiveDocument.Saved = True
End Sub

Word 2010 でいくつかのドキュメントを開くと、次のエラー メッセージが表示されます。

実行時エラー「4248」

ドキュメントが開かれていないため、このコマンドは使用できません

これまでのところ、保護されたビューで開かれているファイル (インターネットからダウンロードしたファイルや電子メールの添付ファイルなど) でこの問題が発生しているようです。トラスト センターで保護されたビューをオフにすると、問題はなくなります。

4

1 に答える 1

3

Microsoft は、保護モードがマクロ内から実行されているかどうかの検出に関するブログ エントリを書きました。

これは、ドキュメントが保護されたビューにないApplication.ActiveProtectedViewWindow場合のisの値を示しています。そのため、これを確認する If ステートメントで ActiveDocument を参照するマクロ関数をラップすると、ドキュメントが保護されたビューにあるときにこれらの関数が実行されなくなります。Nothing

上記のスクリプトは次のようになります。

Sub AutoOpen()
    '
    ' AutoOpen Macro
    '
    '
       Dim aStory As Range
       Dim aField As Field

    ' Check that document is not in Protected View before doing anything
    If Application.ActiveProtectedViewWindow Is Nothing Then

           For Each aStory In ActiveDocument.StoryRanges

              For Each aField In aStory.Fields
                 aField.Update
              Next aField

           Next aStory

         ' set document as unchanged (prevents save dialog popping up when
         'closing) - further changes will set this back
         ActiveDocument.Saved = True
    End If
End Sub
于 2012-08-20T12:01:48.917 に答える