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”

此命令不可用,因为没有打开文档

到目前为止,这似乎发生在受保护视图中打开的文件(例如从 Internet 下载的文件或电子邮件附件)中 - 如果我在信任中心关闭受保护视图,问题就会消失。

4

1 回答 1

3

微软写了一篇关于检测保护模式是否从宏中运行的博客文章。

这表明 的值Application.ActiveProtectedViewWindowNothing当文档不在受保护的视图中时。因此,将引用 ActiveDocument 的宏函数包装在 If 语句中检查这一点将阻止这些函数在文档处于受保护视图时运行。

上面的脚本变成:

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 回答