我有一个 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 抛出编译错误“未定义用户定义类型”。如果我理解正确,我的代码使用了一种叫做早期绑定的东西。Excel 在执行任何代码之前查找该库,但找不到它。
答案可能是调整我的代码以使用后期绑定,以便 Excel 在执行部分脚本并且该库可用之前不会查找该库。
使用这篇文章作为指导,我修改了部分代码,如下所示:
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 抛出错误“运行时错误 '429':ActiveX 组件无法创建对象”。
知道如何修改此代码以利用后期绑定,或者以其他方式加载库引用并在同一过程/模块中运行其余代码吗?