【问题标题】:Access Active Document in VBA when document is opening on the intranet当文档在 Intranet 上打开时访问 VBA 中的 Active Document
【发布时间】:2015-02-25 05:33:33
【问题描述】:

我们有最初为 Word 97 编写的旧 Word 模板。对于每个新版本,我们都更新了模板。现在我们从 Word 2003 转到 Word 2010,当然会有问题。

模板包含以下代码:

Private Sub Document_Open()
    On Error Resume Next
    If ActiveDocument.Type = wdTypeDocument Then
        ' Update the document from database'
    End If
End Sub

问题是 ActiveDocument 给出了错误

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

文档是在内网上用脚本打开的:

<a href="javascript:opendokument('P:\\01\\2-010-01.doc')">012-010-01</a>
<SCRIPT language=javascript> 
function opendokument(dokument){
var objAppl;;

try{
    objAppl = GetObject("","Word.Application");
    objAppl.Documents.open(dokument);
}
catch(exception){
    objAppl = new ActiveXObject("Word.Application");
    objAppl.Visible = true;
    objAppl.Documents.open(dokument);
}   
objAppl = null; 
}
</script>

如果我从本地计算机运行脚本或通过 Windows 打开文档,我不会收到错误提示

【问题讨论】:

    标签: javascript vba ms-word


    【解决方案1】:

    我没有在 Word 2010 中使用过 Word 事件模型,但我会看的东西很少。

    首先,看看是否有其他事件可以挂钩。在 Word 2000 中,我只看到 NewOpenClose。也许在 Word 2010 中,还有其他事件,例如Loaded?如果是这样,您可以尝试将代码放在文档肯定已经加载的事件之一中。

    否则,您可能会编写一些“等待”代码,直到将ActiveDocument 设置为对象实例。你可以试试这样的:

    Private Sub Document_Open()
    
      Do While ActiveDocument Is Nothing
        DoEvents
      Loop
    
      If ActiveDocument.Type = wdTypeDocument Then
        Debug.Print "Now the document is open"
      End If
    
    End Sub
    

    循环中的DoEvents 应该允许加载文档,While 条件最终会发现ActiveDocument 不是Nothing 并允许程序继续运行。当然,这假设文档实际上会打开,但这是值得尝试的。要了解此代码的工作原理,请查看以下代码:

    Private Sub Document_Open()
    
    Dim dtmLater As Date
    Dim doc As Document
    
    dtmLater = DateAdd("s", 5, Now())
    
      Do While doc Is Nothing
        DoEvents
        If Now() >= dtmLater Then
          Set doc = ActiveDocument
        End If
      Loop
    
      If ActiveDocument.Type = wdTypeDocument Then
        Debug.Print "Now the document is open"
      End If
    
    End Sub
    

    上述代码任意暂停 5 秒,因此您可以看到代码如何循环,直到设置了 doc 对象。一旦对象被实例化,代码就可以向前推进了。

    【讨论】:

    • "Do While ActiveDocument Is Nothing" 给出与以前相同的错误。
    • 如果我尝试第二个代码示例,我会得到同样的错误,即使我将超时设置为一个很高的数字
    • @magol 它在哪里抛出错误?你能把它固定到单独的代码行吗?您可能会考虑删除 On Error Resume Next 以更好地找到引发错误的代码行。
    • @magol 诚然,第二个例子可能有点令人困惑,因为docActiveDocument 无关。尝试在代码中探索ActiveDocument.Type,并确保您使用的是正确的对象。例如,尝试执行Debug.Print ActiveDocument.Name 以查看ActiveDocument 对象是否至少“工作”。
    • @Ben McCormack 当我调试时,我将 On error resume next 替换为 on error goto 0。我得到的确切异常是:“运行时错误'4248':此命令不可用,因为没有文档开了。”我在 Set doc = ActiveDocument 行上得到了异常,如果我尝试 Debug.Print ActiveDocument.Name,我也会得到异常。
    【解决方案2】:

    我遇到了同样的问题,我现在的狡猾的解决方案是这样的

    Private Sub Document_Open()
        Application.OnTime (Now + TimeValue("00:00:02")), "ProcessActiveDocument"
    End Sub
    
    Sub ProcessActiveDocument
        'ActiveDocument works here
    End Sub
    

    【讨论】:

      【解决方案3】:

      我确实将 Intranet 服务器添加到 Internet Explorer 的本地 Intranet 区域。然后我将“初始化并编写未标记为可安全执行脚本的 ActiveX 控件”设置为“启用”。

      【讨论】:

        猜你喜欢
        • 2018-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多