在Excel中后期绑定VBIDE.VBE

时间:2015-04-17 01:29:19

标签: excel vba excel-vba excel-2007

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

Dim VBAEditor As VBIDE.VBE

取而代之的是与此类似的(后期绑定):

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.

的启发

1 个答案:

答案 0 :(得分:4)

是的,根据Excel forms: identify unused code,您可以使用后期绑定

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

相关问题