【发布时间】:2018-08-08 19:17:52
【问题描述】:
我的任务是创建一个基于 VBA 的系统,该系统允许我将部门特定的文档注释代码插入基于 VBA 的程序中,然后稍后提取这些日期。该程序通过将 2 个 VBA 模块临时插入到目标项目,然后运行包含的功能。这在它自己的功能上正确,并且使用 VBA 插入/提取 cmets。
但是,我发现自己无法从基于访问的插入项目中将模块插入基于 excel 的项目中。我一直在使用此功能将模块导入目标访问项目:
Public Function InsertVADER(strTestPath As String, ProgramType As String) As Boolean
'//Insert VADER into the target program
On Error GoTo errjordan
Dim obj As AccessObject '//Create instance of Access Application object.
If ProgramType = "Access" Then
''//Transfer Modules to target project.
For Each obj In CurrentProject.AllModules
DoCmd.TransferDatabase acExport, "Microsoft Access", strTestPath, acModule, obj.Name, obj.Name & "_TMP", False
Next obj
'//Set and open target project
Set appAccess = CreateObject("Access.Application")
appAccess.OpenCurrentDatabase strTestPath, False
'//SEt to visible. If the project has an auto exec that will usurp this project. You will
appAccess.Visible = True
'//Open the vader module. If there is an auto run macro this will cause it to show
appAccess.DoCmd.OpenModule ("VADER_TMP")
ElseIf ProgramType = "Excel" Then
'//Run Excel routine
For Each obj In CurrentProject.AllModules
'DoCmd.TransferDatabase acExport, "Microsoft Excel", strTestPath, acModule, obj.Name, obj.Name & "_TMP", False
Next obj
End If
'//Indicate function sucess
InsertVADER = True
Exit Function 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
errjordan:
If Err.Number = 2501 Then
MsgBox "Project cannot be locked for viewing. Please unlock and save project before using this tool"
Err.Clear
InsertVADER = False
ElseIf Err.Number = 29045 Or Err.Number = 7866 Then
MsgBox "This file is not compatible with VADER. Please convert the project to a useable format before using this tool."
Err.Clear
InsertVADER = False
Else
Err.Raise Err.Number
End If
End Function
StrtestPath 传递目标项目的文件路径,programtype 指定我选择的项目类型。两者都设置在外部项目中。
是否有基于 VBA 的解决方案可以让我:
- 将 CurrentProject.allModules 定义的模块从 access 导入到目标 Excel 项目中
- 运行后从目标 Excel 项目中删除模块
【问题讨论】:
-
您可以使用VBA Extensibility修改Excel工作簿中的VBA代码。
-
@chrisneilsen 这正是我需要做的。感谢您的提示!