gpt4 book ai didi

vba - excel 中的后期绑定(bind) VBIDE.VBE

转载 作者:行者123 更新时间:2023-12-01 23:23:08 24 4
gpt4 key购买 nike

是否可以在 Excel 中后期绑定(bind) VBIDE.VBE 对象?例如:

Dim VBAEditor As VBIDE.VBE

而是变成类似这样的东西(后期绑定(bind)):

Dim VBAEditor As Object: set VBAEditor = CreateObject ("VBIDE.VBE")

我的目标是避免手动选择“Microsoft Visual Basic for Applications Extensibility 5.3”引用的复选框。

解决方案

根据下面的反馈,我能够以编程方式动态添加“Microsoft Visual Basic for Applications Extensibility 5.3”引用。解决办法如下:

Sub mainFunction()

Call AddLib("VBIDE", "{0002E157-0000-0000-C000-000000000046}", 5, 3)

' Bunch of working code goes here

End Sub

'******************************************************************************
'AddLib: Adds a library reference to this script programmatically, so that
' libraries do not need to be added manually.
'******************************************************************************
Private Function AddLib(libName As String, guid As String, major As Long, minor As Long)

Dim exObj As Object: Set exObj = GetObject(, "Excel.Application")
Dim vbProj As Object: Set vbProj = exObj.ActiveWorkbook.VBProject
Dim chkRef As Object

' Check if the library has already been added
For Each chkRef In vbProj.References
If chkRef.Name = libName Then
GoTo CleanUp
End If
Next

vbProj.References.AddFromGuid guid, major, minor

CleanUp:
Set vbProj = Nothing
End Function

我深受 this stack article on dynamic referencing in excel. 的启发

最佳答案

是的,按照Excel forms: identify unused code您可以使用后期绑定(bind)

Dim VBProj
Dim VBComp
Set VBProj = ActiveWorkbook.VBProject
For Each VBComp In VBProj.vbcomponents

等等

关于vba - excel 中的后期绑定(bind) VBIDE.VBE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29688683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com