如何加载 VBA 库引用并在同一过程中使用它?
How can I load a VBA library reference and use it in the same procedure?
我有一个用于 Excel 的 VBA 脚本,它在活动工作簿中添加了一个小过程。最终版本将添加一个自动保存工作簿备份副本的程序。
该代码需要 Microsoft Visual Basic for Applications Extensibility 5.3 库,我可以手动添加该库,但我感兴趣的是有一个脚本可以将该库添加到 Excel 然后使用它。
这是将库引用添加到 Excel 的代码。有效。
On Error Resume Next ' If library already referenced, ignore the error
ThisWorkbook.VBProject.References.AddFromGuid _
GUID:="{0002E157-0000-0000-C000-000000000046}", _
Major:=5, Minor:=3
On Error GoTo 0 ' Resume normal error handling
下面是使用此库将 VBA 过程添加到活动工作簿中的代码。它有效,但前提是首先将所需的库引用添加到 Excel:
Sub AddProcedureToModule()
' This script adds a sample VBA procedure to the active workbook
' Add Library Reference: Microsoft Visual Basic for Applications
' Extensibility 5.3
On Error Resume Next ' If library already referenced, ignore the error
ThisWorkbook.VBProject.References.AddFromGuid _
GUID:="{0002E157-0000-0000-C000-000000000046}", _
Major:=5, Minor:=3
On Error GoTo 0 ' Resume normal error handling
Dim VBProj As VBIDE.VBProject ' requires Ref: MS VB for Apps Extensibility 5.3
Dim VBComp As VBIDE.VBComponent ' requires Ref: MS...5.3
Dim CodeMod As VBIDE.CodeModule ' requires Ref: MS...5.3
Dim LineNum As Long
Const DQUOTE = """" ' one " character
Set VBProj = ActiveWorkbook.VBProject ' requires Ref: MS...5.3
Set VBComp = VBProj.VBComponents("Module1") ' requires Ref: MS...5.3
Set CodeMod = VBComp.CodeModule ' requires Ref: MS...5.3
' Insert code into workbook
With CodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, "Public Sub SayHello()"
LineNum = LineNum + 1
.InsertLines LineNum, " MsgBox " & DQUOTE & "Hello World" & DQUOTE
LineNum = LineNum + 1
.InsertLines LineNum, "End Sub"
End With
End Sub
当我尝试将两者结合在一个过程中时,问题就出现了。 Excel 抛出编译错误 "User defined type not defined"。如果我理解正确的话,我的代码使用了一种叫做早期绑定的东西。 Excel 在执行任何代码之前查找库,但找不到。
答案可能是调整我的代码以使用后期绑定,这样 Excel 在执行部分脚本并且库可用之前不会查找该库。
使用 this post 作为指南,我修改了部分代码,如下所示:
Dim VBProj As Object
Dim VBComp As Object
Dim CodeMod As Object
Dim LineNum As Long
Const DQUOTE = """" ' one " character
Set VBProj = CreateObject("ActiveWorkbook.VBProject")
Set VBProj = ActiveWorkbook.VBProject
Set VBComp = CreateObject("VBProj.VBComponents(""Module1"")")
Set CodeMod = CreateObject("VBComp.CodeModule")
Excel 引发错误 "Run-time error '429': ActiveX component can't create object"。
知道如何修改此代码以利用后期绑定,或者以其他方式加载库引用和 运行 相同 procedure/module 中的其余代码吗?
您的原始代码中存在错误前提:
Set VBProj = ActiveWorkbook.VBProject ' requires Ref: MS...5.3
Set VBComp = VBProj.VBComponents("Module1") ' requires Ref: MS...5.3
Set CodeMod = VBComp.CodeModule ' requires Ref: MS...5.3
此代码本身不需要任何引用。需要引用的是上面的变量声明:
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
一旦将它们替换为 As Object
,您就可以 运行 其余代码,而无需添加引用。确保您在顶部有 Option Explicit
以捕获您可能正在使用的任何现在未声明的常量。
但是我建议您考虑一种不同的方法,例如将要放入文件的代码移动到文件可以引用的添加中。自动向文件添加 VBA 代码是一个安全问题,因为它是传播恶意软件的常见方式,它可能会关闭防病毒软件,并且需要在用户界面中设置 Trust access to the VBA project object model 设置(我个人永远不会设置)。
我有一个用于 Excel 的 VBA 脚本,它在活动工作簿中添加了一个小过程。最终版本将添加一个自动保存工作簿备份副本的程序。
该代码需要 Microsoft Visual Basic for Applications Extensibility 5.3 库,我可以手动添加该库,但我感兴趣的是有一个脚本可以将该库添加到 Excel 然后使用它。
这是将库引用添加到 Excel 的代码。有效。
On Error Resume Next ' If library already referenced, ignore the error
ThisWorkbook.VBProject.References.AddFromGuid _
GUID:="{0002E157-0000-0000-C000-000000000046}", _
Major:=5, Minor:=3
On Error GoTo 0 ' Resume normal error handling
下面是使用此库将 VBA 过程添加到活动工作簿中的代码。它有效,但前提是首先将所需的库引用添加到 Excel:
Sub AddProcedureToModule()
' This script adds a sample VBA procedure to the active workbook
' Add Library Reference: Microsoft Visual Basic for Applications
' Extensibility 5.3
On Error Resume Next ' If library already referenced, ignore the error
ThisWorkbook.VBProject.References.AddFromGuid _
GUID:="{0002E157-0000-0000-C000-000000000046}", _
Major:=5, Minor:=3
On Error GoTo 0 ' Resume normal error handling
Dim VBProj As VBIDE.VBProject ' requires Ref: MS VB for Apps Extensibility 5.3
Dim VBComp As VBIDE.VBComponent ' requires Ref: MS...5.3
Dim CodeMod As VBIDE.CodeModule ' requires Ref: MS...5.3
Dim LineNum As Long
Const DQUOTE = """" ' one " character
Set VBProj = ActiveWorkbook.VBProject ' requires Ref: MS...5.3
Set VBComp = VBProj.VBComponents("Module1") ' requires Ref: MS...5.3
Set CodeMod = VBComp.CodeModule ' requires Ref: MS...5.3
' Insert code into workbook
With CodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, "Public Sub SayHello()"
LineNum = LineNum + 1
.InsertLines LineNum, " MsgBox " & DQUOTE & "Hello World" & DQUOTE
LineNum = LineNum + 1
.InsertLines LineNum, "End Sub"
End With
End Sub
当我尝试将两者结合在一个过程中时,问题就出现了。 Excel 抛出编译错误 "User defined type not defined"。如果我理解正确的话,我的代码使用了一种叫做早期绑定的东西。 Excel 在执行任何代码之前查找库,但找不到。
答案可能是调整我的代码以使用后期绑定,这样 Excel 在执行部分脚本并且库可用之前不会查找该库。
使用 this post 作为指南,我修改了部分代码,如下所示:
Dim VBProj As Object
Dim VBComp As Object
Dim CodeMod As Object
Dim LineNum As Long
Const DQUOTE = """" ' one " character
Set VBProj = CreateObject("ActiveWorkbook.VBProject")
Set VBProj = ActiveWorkbook.VBProject
Set VBComp = CreateObject("VBProj.VBComponents(""Module1"")")
Set CodeMod = CreateObject("VBComp.CodeModule")
Excel 引发错误 "Run-time error '429': ActiveX component can't create object"。
知道如何修改此代码以利用后期绑定,或者以其他方式加载库引用和 运行 相同 procedure/module 中的其余代码吗?
您的原始代码中存在错误前提:
Set VBProj = ActiveWorkbook.VBProject ' requires Ref: MS...5.3 Set VBComp = VBProj.VBComponents("Module1") ' requires Ref: MS...5.3 Set CodeMod = VBComp.CodeModule ' requires Ref: MS...5.3
此代码本身不需要任何引用。需要引用的是上面的变量声明:
Dim VBProj As VBIDE.VBProject Dim VBComp As VBIDE.VBComponent Dim CodeMod As VBIDE.CodeModule
一旦将它们替换为 As Object
,您就可以 运行 其余代码,而无需添加引用。确保您在顶部有 Option Explicit
以捕获您可能正在使用的任何现在未声明的常量。
但是我建议您考虑一种不同的方法,例如将要放入文件的代码移动到文件可以引用的添加中。自动向文件添加 VBA 代码是一个安全问题,因为它是传播恶意软件的常见方式,它可能会关闭防病毒软件,并且需要在用户界面中设置 Trust access to the VBA project object model 设置(我个人永远不会设置)。