在 VBA 中后期绑定对象

发布于 2024-08-13 16:48:44 字数 579 浏览 4 评论 0原文

我在尝试后期绑定到 VBProject 对象时收到“运行时错误 429”:

Dim vbProj As Object
Set vbProj = CreateObject("ActiveDocument.VBProject")

是否有一些我无法理解的基本内容?

例如,如何编写文章 308340 中的代码来使用后期绑定?:

 Sub CheckReference()

        Dim vbProj As VBProject  
        Set vbProj = ActiveDocument.VBProject

        For Each chkRef In vbProj.References

          If chkRef.IsBroken Then
             Debug.Print chkRef.Name
          End If

        Next

    End Sub

I am getting the "run-time error 429" in my attempt at late binding to the VBProject object:

Dim vbProj As Object
Set vbProj = CreateObject("ActiveDocument.VBProject")

Is there something fundamental I am failing to understand?

For example, how would you write the code in article 308340 to use late binding?:

 Sub CheckReference()

        Dim vbProj As VBProject  
        Set vbProj = ActiveDocument.VBProject

        For Each chkRef In vbProj.References

          If chkRef.IsBroken Then
             Debug.Print chkRef.Name
          End If

        Next

    End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

千仐 2024-08-20 16:48:44

Dennis,

如果您从 Word 中运行它,那么您不需要使用 CreateObject()。

设置 vbProj = ActiveDocument.VBProject 即可。

如果您从其他地方运行此程序,则可能需要首先创建 Word 对象并加载文档:

  Dim a As Object
  Dim vbProj As Object

  Set a = CreateObject("Word.Application")
  a.Documents.Open "C:\temp\test1.docx"
  MsgBox a.Documents.Count
  Set vbProj = a.ActiveDocument.VBProject

在这两种情况下,您可能会收到“对 Visual Basic 项目的编程访问不受信任”的消息,该消息可通过宏安全设置解决,http://support.microsoft.com/kb/282830

我希望这能回答你的问题。

Dennis,

if you are running this from within Word, then you don't need to use CreateObject().

Set vbProj = ActiveDocument.VBProject will work.

if you are running this from elsewhere, then you may need to create Word object first and load the document:

  Dim a As Object
  Dim vbProj As Object

  Set a = CreateObject("Word.Application")
  a.Documents.Open "C:\temp\test1.docx"
  MsgBox a.Documents.Count
  Set vbProj = a.ActiveDocument.VBProject

In both cases you may get the "Programmatic Access to Visual Basic Project is not Trusted" which resolves through Macro security settings, http://support.microsoft.com/kb/282830.

I hope this answers your question.

一抹苦笑 2024-08-20 16:48:44

你从哪里想到那个progid(ActiveDocument.VBProject)?一般来说,progid 的格式为 AppName.ObjectName,如 Excel.Sheet 或 Word.Document 中那样。 IIRC、VB6本身不支持OLE自动化;相反,它支持创建 OLE 自动化服务器和客户端。

更新:

好的,我明白现在发生了什么。 ActiveDocument.VBProject 不是有效的 progid。 ActiveDocument 是 Word.Application 对象的一个​​属性,它的 progid 为(令人惊讶!)“Word.Application”。

所以你想要 Meringros 的答案。

Where did you come up with that progid (ActiveDocument.VBProject)? Generally progid's are of the form AppName.ObjectName, as in Excel.Sheet or Word.Document. IIRC, VB6 doesn't support OLE Automation itself; rather, it supports creating OLE Automation servers and clients.

Update:

Ok, I see what's going on now. ActiveDocument.VBProject isn't a valid progid. ActiveDocument is a property of the Word.Application object, which has a progid of (surprise!) "Word.Application".

So you want Meringros answer.

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