Excel VBA - 确定项目中是否包含模块
Excel VBA - Determine if a Module is included in a project
我目前正在处理一个无法验证是否已安装所有模块的项目。越来越多的模块被用于我使用的程序的通用功能。我在网上尝试了一些解决方案,但由于我不熟悉 Activeworkbook.VBProject.VBComponents()
方法,所以无法使用。
有人提到我应该检查工具参考
Microsoft Visual Basic For Applications Extensibility 我检查过没有结果。任何帮助,将不胜感激。 :)
参考文献:
https://www.devhut.net/2010/12/09/ms-access-vba-determine-if-a-module-exists/
这是我的代码:
Option Explicit
Public Function Is_Module_Loaded(name As String) As Boolean
Dim Module As Object
Dim Module_Name As String
Module_Name = name
Is_Module_Loaded = False
On Error GoTo errload
Set Module = ActiveWorkbook.VBProject.VBComponents(Module_Name).CodeModule
Is_Module_Loaded = True
If (0 <> 0) Then
errload:
MsgBox ("MODULE: " & Module_Name & " is not installed please add")
Stop
End If
End Function
当 运行 代码时,我没有收到任何非常有用的错误,除了我自己的代码,它报告错误说我的模块不存在。
编辑:更新为将工作簿添加为第二个参数
试试这个:
Sub tester()
Debug.Print Is_Module_Loaded(ThisWorkbook, "Module4")
Debug.Print Is_Module_Loaded(ActiveWorkbook, "Module4")
End sub
Public Function Is_Module_Loaded(wb as Workbook, name As String) As Boolean
Dim Module As Object
On Error Resume Next
Set Module = wb.VBProject.VBComponents(name).CodeModule
On Error GoTo 0
Is_Module_Loaded = Not Module Is Nothing
If Not Is_Module_Loaded Then
MsgBox ("MODULE: " & name & " is not installed in '" & _
wb.Name & "' please add")
End If
End Function
所以我相信我找到了解决办法。
致谢:Tim Williams、Mathieu Guindon 和 Joe Phi(参见 link)解决方案的指导
参考: ()
注意到的问题:原来的 Tim 提到不设置工作簿可能会让我引用正确的工作簿,这是主要问题,因为我打开了其他工作簿,它是尝试参考。
Option Explicit
Public Function Is_Module_Loaded(name As String, Optional wb As Workbook) As Boolean
'!!!need to reference: microsoft visual basic for applications extensibility 5.3
Dim j As Long
Dim vbcomp As VBComponent
Dim modules As Collection
Set modules = New Collection
Is_Module_Loaded = False
'check if value is set
If wb Is Nothing Then
Set wb = ThisWorkbook
End If
If (name = "") Then
GoTo errorname
End If
'collect names of files
For Each vbcomp In ThisWorkbook.VBProject.VBComponents
If ((vbcomp.Type = vbext_ct_StdModule) Or (vbcomp.Type = vbext_ct_ClassModule)) Then
modules.Add vbcomp.name
End If
Next vbcomp
'Compair the file your looking for to the collection
For j = 1 To modules.Count
If (name = modules.Item(j)) Then
Is_Module_Loaded = True
End If
Next j
j = 0
'if Is_module_loaded not true
If (Is_Module_Loaded = False) Then
GoTo notfound
End If
'if error
If (0 <> 0) Then
errorname:
MsgBox ("Function BootStrap.Is_Module_Loaded Was not passed a Name of Module")
Stop
End If
If (0 <> 0) Then
notfound:
MsgBox ("MODULE: " & name & " is not installed please add")
Stop
End If
End Function
我目前正在处理一个无法验证是否已安装所有模块的项目。越来越多的模块被用于我使用的程序的通用功能。我在网上尝试了一些解决方案,但由于我不熟悉 Activeworkbook.VBProject.VBComponents()
方法,所以无法使用。
有人提到我应该检查工具参考 Microsoft Visual Basic For Applications Extensibility 我检查过没有结果。任何帮助,将不胜感激。 :)
参考文献:
https://www.devhut.net/2010/12/09/ms-access-vba-determine-if-a-module-exists/
这是我的代码:
Option Explicit
Public Function Is_Module_Loaded(name As String) As Boolean
Dim Module As Object
Dim Module_Name As String
Module_Name = name
Is_Module_Loaded = False
On Error GoTo errload
Set Module = ActiveWorkbook.VBProject.VBComponents(Module_Name).CodeModule
Is_Module_Loaded = True
If (0 <> 0) Then
errload:
MsgBox ("MODULE: " & Module_Name & " is not installed please add")
Stop
End If
End Function
当 运行 代码时,我没有收到任何非常有用的错误,除了我自己的代码,它报告错误说我的模块不存在。
编辑:更新为将工作簿添加为第二个参数
试试这个:
Sub tester()
Debug.Print Is_Module_Loaded(ThisWorkbook, "Module4")
Debug.Print Is_Module_Loaded(ActiveWorkbook, "Module4")
End sub
Public Function Is_Module_Loaded(wb as Workbook, name As String) As Boolean
Dim Module As Object
On Error Resume Next
Set Module = wb.VBProject.VBComponents(name).CodeModule
On Error GoTo 0
Is_Module_Loaded = Not Module Is Nothing
If Not Is_Module_Loaded Then
MsgBox ("MODULE: " & name & " is not installed in '" & _
wb.Name & "' please add")
End If
End Function
所以我相信我找到了解决办法。
致谢:Tim Williams、Mathieu Guindon 和 Joe Phi(参见 link)解决方案的指导
参考: ()
注意到的问题:原来的 Tim 提到不设置工作簿可能会让我引用正确的工作簿,这是主要问题,因为我打开了其他工作簿,它是尝试参考。
Option Explicit
Public Function Is_Module_Loaded(name As String, Optional wb As Workbook) As Boolean
'!!!need to reference: microsoft visual basic for applications extensibility 5.3
Dim j As Long
Dim vbcomp As VBComponent
Dim modules As Collection
Set modules = New Collection
Is_Module_Loaded = False
'check if value is set
If wb Is Nothing Then
Set wb = ThisWorkbook
End If
If (name = "") Then
GoTo errorname
End If
'collect names of files
For Each vbcomp In ThisWorkbook.VBProject.VBComponents
If ((vbcomp.Type = vbext_ct_StdModule) Or (vbcomp.Type = vbext_ct_ClassModule)) Then
modules.Add vbcomp.name
End If
Next vbcomp
'Compair the file your looking for to the collection
For j = 1 To modules.Count
If (name = modules.Item(j)) Then
Is_Module_Loaded = True
End If
Next j
j = 0
'if Is_module_loaded not true
If (Is_Module_Loaded = False) Then
GoTo notfound
End If
'if error
If (0 <> 0) Then
errorname:
MsgBox ("Function BootStrap.Is_Module_Loaded Was not passed a Name of Module")
Stop
End If
If (0 <> 0) Then
notfound:
MsgBox ("MODULE: " & name & " is not installed please add")
Stop
End If
End Function