如何自动设置Excel VBA引用?

huangapple go评论80阅读模式
英文:

How to Automatically set Excel VBA References?

问题

我理解我的脚本中使用的引用需要额外的引用才能运行我正在使用的所有函数,但现在我需要与许多其他人共享这个。这些都是每个人都应该安装的常见引用,只是未启用。

我在这个帖子1中找到了相同的问题,但我很难理解如何将其应用到我自己的需求中。

Dim rs As Object
Set rs = CreateObject("ADODB.Recordset")
rs.CursorLocation = 3 'adUseClient

我认为我只需要在上述代码中更改“ADODB”的名称以适应我正在使用的引用,但我不知道如何找到它。ADODB似乎不再作为一个选项存在,我也不知道这是.olb/.dll的确切名称还是库中的特定部分。

我还找到了帖子2来添加引用,但它与第一个帖子非常不同,而且更加复杂。

有人能指导我正确的方向吗?

我目前正在使用以下引用:

  • Microsoft Office 16.0 对象库
  • Microsoft Forms 2.0 对象库
  • Microsoft Outlook 16.0 对象库
  • Microsoft Scripting Runtime
  • Microsoft Word 16.0 对象库
  • NucleusNativeMessagingLib

帖子2建议添加 Microsoft Visual Basic for Applications Extensibility 5.3,以便我可以通过代码添加其他引用,但如果我现在需要手动添加它,其他用户如何通过代码添加该引用?

对此的任何帮助都将不胜感激。

祝一切顺利,
Will

英文:

I understand that the references I've used within my script require extra references to run all of the functions I am using but I now need this to be shared with many other people. They are all common references that everyone should have installed, just not enabled.

I have found this post1 with the same issue but I'm struggling to understand how to apply this to my own needs.

Dim rs As Object
Set rs = CreateObject("ADODB.Recordset")
rs.CursorLocation = 3 'adUseClient

I think I just need to change the "ADODB" in the above code to the right name for the references I'm using but I don't know how to find it. ADODB doesn't seem to exist as an option anymore and I don't know if this was the exact name of the .olb/.dll or a specific part within a library.

I have also found Post2 to add the references, but it is very different from the first post and is a lot more complicated.

Can anyone point me in the right direction?

I'm currently using the following references:

Microsoft Office 16.0 Object Library
Microsoft Forms 2.0 Object Library
Microsoft Outlook 16.0 Object Library
Microsoft Scripting Runtime
Microsoft Word 16.0 Object LIbrary
NucleusNativeMessagingLib

Post2 suggested adding Microsoft Visual Basic for Applications Extensibility 5.3 so that I could add the other references via code, but if I need to manually add that now, how do other uses add that Reference with code?

Any help with this is appreciated

All the best,
Will

答案1

得分: 2

为了从您的安装中提取必要的引用以添加到其他人的安装中,您可以使用以下函数:

Function getReference(wb As Workbook, refName As String) As Variant
    Dim i As Long
    
    For i = 1 To wb.VBProject.References.Count
        If wb.VBProject.References(i).Name = refName Or _
                      wb.VBProject.References(i).Description = refName Then
             With wb.VBProject.References(i)
                getReference = Array(.Name, .Description, .FullPath, .GUID, .Major, .Minor)
             End With
        End If
    Next i
End Function

它返回一个包含引用名称、描述、完整路径、GUID 以及主/次要版本的数组。有时,在引用窗口中看到的应该是引用描述,但实际上是引用名称(非常少见),但上述函数也能解决这种情况:

Sub TestgetReference()
  Dim refName As String: refName = "Microsoft Word 16.0 Object Library" '在此处使用引用,如您在引用窗口中所见
                        'refName = "mscorlib.dll"
                        'refName = "System_Windows_Forms" '没有描述 - 这是它的名称
                        'refName = "PDFCreator"           '没有描述 - 这是它的名称
                        'refName = "test not existing if all above ones also exist in the tested installation..."
  Dim wb As Workbook: Set wb = ThisWorkbook '在此处使用您需要的工作簿
  Dim arrRef
  
  arrRef = getReference(wb, refName)
  If Not IsEmpty(arrRef) Then
    Debug.Print Join(arrRef, "|")
  Else
    MsgBox """" & refName & """" & " 未添加到 " & """" & wb.Name & """" & " VBProject。", vbInformation, _
           "引用缺失"
  End If
End Sub

当然,您需要使用您需要的引用。我只在需要定义该函数时使用了我的引用。

英文:

In order to extract from your installation the necessary references to be added to "other people" installations, you can use the next function:

Function getReference(wb As Workbook, refName As String) As Variant
    Dim i As Long
    
    For i = 1 To wb.VBProject.References.count
        If wb.VBProject.References(i).name = refName Or _
                      wb.VBProject.References(i).Description = refName Then
             With wb.VBProject.References(i)
                getReference = Array(.name, .Description, .FullPath, .GUID, .Major, .Minor)
             End With
        End If
    Next i
End Function

It returns an array containing the reference name, description, full path, Guid and its Major/minor version. Sometimes, what you can see in the References window and it should be the reference description, is its name (very seldom) but the above function is able to solve this cases, too:

Sub TestgetReference()
  Dim refName As String: refName = "Microsoft Word 16.0 Object Library" 'use here a reference as you can see in the References window
                        refName = "mscorlib.dll"
                        'refName = "System_Windows_Forms" 'not having a description - this is its name
                        'refName = "PDFCreator"           'not having a description - this is its name
                        'refName = "test not existing if all above ones also exist in the tested installation..."
  Dim wb As Workbook: Set wb = ThisWorkbook 'use here the workbook you need
  Dim arrRef
  
  arrRef = getReference(wb, refName)
  If Not IsEmpty(arrRef) Then
    Debug.Print Join(arrRef, "|")
  Else
    MsgBox """" & refName & """ is not added to """ & wb.name & """ VBProject.", vbInformation, _
           "Reference missing"
  End If
End Sub

Of course, you have to use the references you need. I only played with mines when needed to define the function...

huangapple
  • 本文由 发表于 2023年7月13日 18:47:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76678503.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定