Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在内部网上打开文档时访问VBA中的活动文档_Javascript_Vba_Ms Word - Fatal编程技术网

Javascript 在内部网上打开文档时访问VBA中的活动文档

Javascript 在内部网上打开文档时访问VBA中的活动文档,javascript,vba,ms-word,Javascript,Vba,Ms Word,我们有最初为Word 97编写的旧Word模板。对于每个新版本,我们都更新了模板。现在我们将从Word2003转到Word2010,当然会有问题 该模板包含以下代码: Private Sub Document_Open() On Error Resume Next If ActiveDocument.Type = wdTypeDocument Then ' Update the document from database' End If End Sub

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

该模板包含以下代码:

Private Sub Document_Open()
    On Error Resume Next
    If ActiveDocument.Type = wdTypeDocument Then
        ' Update the document from database'
    End If
End Sub
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
问题是ActiveDocument给出了错误

此命令不可用,因为未打开任何文档

在intranet上使用脚本打开文档:

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

函数opendokument(dokument){
var-objAppl;;
试一试{
objAppl=GetObject(“,”Word.Application”);
对象文档打开(dokument);
}
捕获(例外){
objAppl=新的ActiveXObject(“Word.Application”);
objAppl.Visible=true;
对象文档打开(dokument);
}   
objAppl=null;
}

如果我在本地计算机上运行脚本或通过Windows打开文档,则不会出现错误

我在Word 2010中没有使用Word事件模型,但有几件事我会看一下

首先,看看是否有额外的事件,你可以钩入。在Word 2000中,我只看到
新建
打开
,和
关闭
。也许在Word 2010中,还有其他事件,例如加载的
?如果是这样的话,您可以尝试将代码放在其中一个事件中,确保文档已经加载

否则,您可能会编写一些“等待”直到
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()
    On Error Resume Next
    If ActiveDocument.Type = wdTypeDocument Then
        ' Update the document from database'
    End If
End Sub
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
对象。一旦对象被实例化,代码就可以前进。

我在Internet Explorer中将intranet服务器添加到本地intranet区域。然后我将“初始化并为ActiveX控件编写脚本,但未标记为脚本安全”设置为启用状态。

我也遇到了同样的问题,目前我的棘手解决方案是

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

Sub ProcessActiveDocument
    'ActiveDocument works here
End Sub

“Do While ActiveDocument Is Nothing”给出的错误与以前相同。如果我尝试secound代码示例,即使我将超时设置为高,也会得到相同的错误number@magol它在哪里抛出错误?你能把它归结为一行代码吗?您可能会考虑下一步在错误恢复时删除
以更好地找到引发错误的代码行。@magol不可否认,第二个示例可能有点混乱,因为
doc
ActiveDocument
无关。尝试浏览
ActiveDocument。在代码中键入
,确保使用的对象正确。例如,尝试执行
Debug.Print ActiveDocument.Name
以查看
ActiveDocument
对象是否至少处于“工作状态”。@Ben McCormack当我调试时,我将错误时替换为错误时继续下一步执行错误时转到0。我得到的确切异常是:“运行时错误'4248':此命令不可用,因为没有打开任何文档。”我在Set doc=ActiveDocument行上得到异常,如果我尝试Debug.Print ActiveDocument.Name,我也会得到异常。