当文档在 Intranet 上打开时,在 VBA 中访问活动文档
我们有最初为 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 给出错误
此命令不可用,因为没有打开文档
是使用 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>
如果我从本地计算机运行脚本或通过 Windows 打开文档,则不会收到错误消息
We have old Word templates that were originally written for Word 97. For each new version, we've updated the templates. Now we'll go from Word 2003 to Word 2010, and of course there are problems.
The template contains the following code:
Private Sub Document_Open()
On Error Resume Next
If ActiveDocument.Type = wdTypeDocument Then
' Update the document from database'
End If
End Sub
The problem is that the ActiveDocument gives the error
This command is not available Because no document is open
The documents is open with script on the 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>
If I run the script from my local computer or open the document via Windows, I don't get the error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我确实将 Intranet 服务器添加到了 Internet Explorer 中的本地 Intranet 区域。然后我将“初始化和脚本未标记为脚本安全的 ActiveX 控件”设置为“启用”。
I did add the intranet server to the Local intranet zone in Internet Explorer. And then I set "Initialize and script ActiveX controls not marked as safe for scripting" to Enabled.
我也遇到了同样的问题,目前我的解决方案是这样的
I've got the same problem, my dodgy solution for now is this
我还没有使用过 Word 2010 中的 Word 事件模型,但我想了解的东西很少。
首先,查看是否有您可以挂钩的其他事件。在 Word 2000 中,我只看到
新建
、打开
和关闭
。也许在Word 2010中,还有其他事件,例如Loaded
?如果是这样,您可以尝试将代码放置在文档肯定已加载的事件之一中。否则,您可能会编写一些“等待”直到
ActiveDocument
设置为对象实例的代码。您可以尝试这样的操作:循环中的
DoEvents
应允许加载文档,而While
条件最终将捕获ActiveDocument
不加载什么都没有
并且将允许程序继续进行。诚然,这假设文档实际上会公开,但这是值得尝试的事情。要了解此代码的工作原理,请查看以下代码:上面的代码任意暂停 5 秒,以便您可以看到代码如何循环,直到设置
doc
对象。一旦对象被实例化,代码就可以继续执行。I haven't worked with the Word event model in Word 2010, but there are few a things I would look at.
First, see if there are additional events that you may be able to hook into. In Word 2000, I only see
New
,Open
, andClose
. Perhaps in Word 2010, there are other events such asLoaded
? If so, you might try placing the code in one of those events where the document is sure to already be loaded.Otherwise, you might write some code that "waits" until
ActiveDocument
is set to an instance of object. You might try something like this:The
DoEvents
within the loop should allow the document to load and theWhile
condition will eventually catch thatActiveDocument
is notNothing
and will allow the program to proceed. Granted, this assumes that document will in fact become open, but it's something worth trying. To get an idea of how this code might work, look at the following code:The above code arbitrarily pauses for 5 seconds so you can see how the code loops until the
doc
object is set. Once the object is instantiated, the code can move forward.