我正在尝试在Excel VBA中将库引用添加到MS Office的其他部分,但它无法正常工作。
代码运行但不添加引用 - 如果我检查工具>引用,它不存在,并且需要引用(对Outlook和Word)错误的代码出现“编译错误 - 未定义的用户定义类型”的预期错误。
如果我尝试使用下面的行在即时窗口中添加它,则会显示错误消息“运行时错误'32813':名称与现有模块,项目或对象库冲突。”但是,引用不存在,依赖代码仍然没有运行。
我已经添加了对MS VBA Extensible 5.3的引用。
有什么想法吗?
立即窗口中使用的代码:
ThisWorkbook.VBProject.References.AddFromGuid GUID:="{420B2830-E718-11CF-893D-00A0C9054228}", Major:=1, Minor:=0
完整的代码实现是:
Public Sub AddVBAReference(SoftwareName As String)
'''
Dim AppVersionNum As Double
Dim AppRefGUID As String
Dim MajorVersion As Integer
Dim MinorVersion As Integer
''Determine software version
AppVersionNum = Application.Version
''Assign reference for version
Select Case SoftwareName
Case "Word"
Select Case AppVersionNum
Case 15
AppRefGUID = """{00020905-0000-0000-C000-000000000046}"""
End Select
'Set versions
MajorVersion = 1
MinorVersion = 0
Case "Outlook"
Select Case AppVersionNum
Case 15
AppRefGUID = """{420B2830-E718-11CF-893D-00A0C9054228}"""
End Select
'Set versions
MajorVersion = 1
MinorVersion = 0
Case "FileSystem"
AppRefGUID = """{420B2830-E718-11CF-893D-00A0C9054228}"""
'Set versions
MajorVersion = 1
MinorVersion = 0
Case "VBIDE"
AppRefGUID = """{0002E157-0000-0000-C000-000000000046}"""
'Set versions
MajorVersion = 5
MinorVersion = 3
End Select
''Add reference
On Error Resume Next
'Add VBA reference
ThisWorkbook.VBProject.References.AddFromGuid GUID:=AppRefGUID,
Major:=MajorVersion, Minor:=MinorVersion
On Error GoTo 0
End Sub