当文档在 Intranet 上打开时,在 VBA 中访问活动文档

发布于 2024-09-16 16:32:01 字数 927 浏览 2 评论 0原文

我们有最初为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赴月观长安 2024-09-23 16:32:06

我确实将 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.

╰ゝ天使的微笑 2024-09-23 16:32:05

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

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

Sub ProcessActiveDocument
    'ActiveDocument works here
End Sub

I've got the same problem, my dodgy solution for now is this

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

Sub ProcessActiveDocument
    'ActiveDocument works here
End Sub
汹涌人海 2024-09-23 16:32:03

我还没有使用过 Word 2010 中的 Word 事件模型,但我想了解的东西很少。

首先,查看是否有您可以挂钩的其他事件。在 Word 2000 中,我只看到新建打开关闭。也许在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 不加载什么都没有并且将允许程序继续进行。诚然,这假设文档实际上会公开,但这是值得尝试的事情。要了解此代码的工作原理,请查看以下代码:

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 对象。一旦对象被实例化,代码就可以继续执行。

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, and Close. Perhaps in Word 2010, there are other events such as Loaded? 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:

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

The DoEvents within the loop should allow the document to load and the While condition will eventually catch that ActiveDocument is not Nothing 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:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文