在VB中绑定VBIDE.VBE
是否有可能延迟绑定Excel中的VBIDE.VBE对象? 例如:
Dim VBAEditor As VBIDE.VBE
而是成为类似的东西(后期绑定):
Dim VBAEditor As Object: set VBAEditor = CreateObject ("VBIDE.VBE")
我的目标是避免必须手动进入select“Microsoft Visual Basic for Applications Extensibility 5.3”参考的checkbox。
解
使用下面的反馈,我能够以编程方式dynamic地添加“Microsoft Visual Basic for Applications Extensibility 5.3”参考。 解决scheme如下:
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
我深受这篇关于在Excel中dynamic引用的文章的启发。
是的,根据Excel表格:识别未使用的代码,您可以使用后期绑定
Dim VBProj Dim VBComp Set VBProj = ActiveWorkbook.VBProject For Each VBComp In VBProj.vbcomponents
等等