gpt4 book ai didi

excel - 如何使用 VBA 以编程方式添加引用

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

我编写了一个程序,该程序运行并在完成时向 Skype 发送信息。我需要添加 Skype4COM.dll 的引用才能通过 Skype 发送消息。我们在网络上有十几台计算机和一个共享文件服务器(除其他外)。所有其他计算机都需要能够运行该程序。我希望避免手动设置引用。我计划将引用放在共享位置,并在程序运行时以编程方式添加它。

我似乎无法弄清楚如何使用 VBA 以编程方式将引用添加到 Excel 2007。我知道如何手动执行此操作:打开 VBE --> 工具 --> 引用 --> 浏览 --_> 文件位置和名称。但这对于我的目的来说不是很有用。我知道Access Vb.net中有办法做到这一点类似的代码不断出现,但我不确定我是否理解它,或者它是否相关:

ThisWorkbook.VBProject.References.AddFromGuid _
GUID:="{0002E157-0000-0000-C000-000000000046}", _
Major:=5, Minor:=3

到目前为止,在所提供的解决方案中,为了以编程方式添加引用,我需要手动添加引用并更改信任中心 - 这不仅仅是添加引用。虽然我想如果我遵循所提出的解决方案,我将能够以编程方式添加 future 的引用。这可能值得付出努力。

任何进一步的想法都会很棒。

最佳答案

省略

有两种方法可以通过 VBA 将引用添加到项目中

1)使用 GUID

2)直接引用dll。

让我来介绍一下两者。

但首先您需要注意以下三件事

a) 应启用宏

b) 在安全设置中,确保选中“信任对 Visual Basic 项目的访问”

enter image description here

c) 您已手动设置对“Microsoft Visual Basic for Applications Extensibility”对象的引用

enter image description here

方式 1(使用 GUID)

我通常会避免这种方式,因为我必须在注册表中搜索 GUID...我讨厌 LOL。有关 GUID 的更多信息 here .

主题:通过代码添加 VBA 引用库

链接:http://www.vbaexpress.com/kb/getarticle.php?kb_id=267

'Credits: Ken Puls
Sub AddReference()
'Macro purpose: To add a reference to the project using the GUID for the
'reference library

Dim strGUID As String, theRef As Variant, i As Long

'Update the GUID you need below.
strGUID = "{00020905-0000-0000-C000-000000000046}"

'Set to continue in case of error
On Error Resume Next

'Remove any missing references
For i = ThisWorkbook.VBProject.References.Count To 1 Step -1
Set theRef = ThisWorkbook.VBProject.References.Item(i)
If theRef.isbroken = True Then
ThisWorkbook.VBProject.References.Remove theRef
End If
Next i

'Clear any errors so that error trapping for GUID additions can be evaluated
Err.Clear

'Add the reference
ThisWorkbook.VBProject.References.AddFromGuid _
GUID:=strGUID, Major:=1, Minor:=0

'If an error was encountered, inform the user
Select Case Err.Number
Case Is = 32813
'Reference already in use. No action necessary
Case Is = vbNullString
'Reference added without issue
Case Else
'An unknown error was encountered, so alert the user
MsgBox "A problem was encountered trying to" & vbNewLine _
& "add or remove a reference in this file" & vbNewLine & "Please check the " _
& "references in your VBA project!", vbCritical + vbOKOnly, "Error!"
End Select
On Error GoTo 0
End Sub
<小时/>

方式2(直接引用dll)

此代码添加了对 Microsoft VBScript Regular Expressions 5.5 的引用

Option Explicit

Sub AddReference()
Dim VBAEditor As VBIDE.VBE
Dim vbProj As VBIDE.VBProject
Dim chkRef As VBIDE.Reference
Dim BoolExists As Boolean

Set VBAEditor = Application.VBE
Set vbProj = ActiveWorkbook.VBProject

'~~> Check if "Microsoft VBScript Regular Expressions 5.5" is already added
For Each chkRef In vbProj.References
If chkRef.Name = "VBScript_RegExp_55" Then
BoolExists = True
GoTo CleanUp
End If
Next

vbProj.References.AddFromFile "C:\WINDOWS\system32\vbscript.dll\3"

CleanUp:
If BoolExists = True Then
MsgBox "Reference already exists"
Else
MsgBox "Reference Added Successfully"
End If

Set vbProj = Nothing
Set VBAEditor = Nothing
End Sub

注意:我没有添加错误处理。建议在您的实际代码中使用它:)

编辑mischab1击败:)

关于excel - 如何使用 VBA 以编程方式添加引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9879825/

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