在 VBA 中后期绑定对象
我在尝试后期绑定到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Dennis,
如果您从 Word 中运行它,那么您不需要使用 CreateObject()。
设置 vbProj = ActiveDocument.VBProject 即可。
如果您从其他地方运行此程序,则可能需要首先创建 Word 对象并加载文档:
在这两种情况下,您可能会收到“对 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:
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.
你从哪里想到那个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.