【问题标题】:Excel VBA - Determine if a Module is included in a projectExcel VBA - 确定模块是否包含在项目中
【发布时间】:2019-03-25 19:27:37
【问题描述】:

我目前正在处理一个无法验证是否已安装所有模块的项目。越来越多的模块用于我使用的程序的常用功能。我在网上尝试了一些我无法使用的解决方案,因为我不熟悉Activeworkbook.VBProject.VBComponents() 方法。

有人提到我应该检查工具参考 Microsoft Visual Basic For Applications Extensibility 我检查了没有结果。任何帮助,将不胜感激。 :)

参考资料:

https://www.mrexcel.com/forum/excel-questions/284317-vba-function-check-if-particular-macro-exists.html

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

在运行代码时,我没有收到任何非常有帮助的错误,但我自己的错误报告错误说我的模块不存在。

【问题讨论】:

  • 这很好地概述了此类任务所涉及的内容:cpearson.com/excel/vbe.aspx您是否在 Excel 选项中设置了“信任对 VBProject 的访问”复选框?
  • 请注意,您所能做的就是验证 VBA 项目是否包含以您要查找的内容命名的模块 - 这并不一定意味着您想要的实际模块包含您的内容需要它。考虑在ThisWorkbook 中拥有所需模块的副本,如果目标项目中存在该模块,则使用VBIDE API 将其替换为该模块,如果不存在则添加它。现在,考虑MsgBox Err.Description,而不是实际上抑制可能有用的错误的手工错误消息。 实际错误是什么?
  • Mathieu 你能详细说明MsgBox Err.Description 部分吗?我对此不熟悉。我相信即使没有我的自定义错误消息,我也没有错误。我也无法弄清楚 VBIDE API,但是从某个位置加载模块的代码的想法会非常有用。

标签: excel vba vbe


【解决方案1】:

编辑:更新为将工作簿添加为第二个参数

试试这个:

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

【讨论】:

  • 所以我尝试了这个,模块没有被写入对象,里面什么都没有。我使用它的模块名称被命名为 DEV_BE,运行时它说它仍然不存在。仍然无法理解我可能会犯的错误。
  • 注释掉“on error resume next”——你得到一个错误吗?如果是,那是什么?
  • 然后我收到一条错误消息,指出“运行时错误'9':下标超出范围。我的变量当前将 name =“DEV_BE”作为字符串,is_module_loaded = false 作为 bool,module = nothing as对象。DEV_BE 是我环境中模块的名称。
  • 听起来该函数按预期工作,所以可能是您如何调用它的问题?或者它正在查看错误的工作簿?将工作簿添加为第二个参数可能会更好,这样您就可以指定查看的确切位置,而不是默认活动工作簿。
  • 嗯,我会玩弄,我没有考虑将它指定到工作簿,但我不明白为什么我不能。很快就会回来报告
【解决方案2】:

所以我相信我已经找到了解决方案。

感谢:Tim Williams、Mathieu Guindon 和 Joe Phi(请参阅链接)以获得解决方案的指导

参考:(https://stackoverflow.com/a/46727898/10297459)

注意的问题:最初蒂姆提到不设置工作簿可能会让我引用正确的工作簿,这是主要问题,因为我打开了它试图引用的其他工作簿。

    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

【讨论】:

    猜你喜欢
    • 2015-04-24
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 2018-08-08
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    相关资源
    最近更新 更多