打开Excel启用宏的工作簿时注册DLL文件

时间:2018-03-23 03:40:22

标签: excel vba visual-studio excel-vba dll

我想让用户有机会在打开启用宏的Excel工作簿时创建对.tlb类型库(如果不存在)的引用。

我有以下子程序(在VBA中),当从按钮触发时,或通过按VBA编辑器中的绿色三角形按钮运行时,它可以正常工作:

Private Sub Workbook_Open()

    Dim isDLLReferenced As Boolean
    isDLLRegistered = False

    For Each ref In ActiveWorkbook.VBProject.References
        If Right(LCase(ref.FullPath), 7) = LCase("qms.tlb") Then
            isDLLReferenced = True
            GoTo OutIf
            Exit For
        End If
    Next ref

OutIf:
    If isDLLReferenced = False Then GoTo MessageBox
    GoTo OutSub:

MessageBox:
    Dim question As Integer
    question = MsgBox("A dll file needs to be isDLLReferenced on your 
    computer in order for this template's macros to function", vbYesNo + 
    vbQuestion,  "")
    If question = 7 Then GoTo OutSub

    Dim filepath As String
    filepath = Application.ActiveWorkbook.Path & "\Reference\qms.tlb"
    Application.VBE.ActiveVBProject.References.AddFromFile filepath

OutSub:
End Sub

如果此代码放在Workbook_Open事件中,则不会创建引用,如上所示。

打开工作簿时会出现消息框,但不会对类型库进行引用。

我的问题是:如何在打开工作簿时以编程方式创建对.tlb的引用? (这可以吗?)

提前致谢。

0 个答案:

没有答案