在 VBA (Word) 中更新模块

Updating Modules in VBA (Word)

提问人:gregor 提问时间:11/9/2023 最后编辑:braXgregor 更新时间:11/9/2023 访问量:47

问:

例如,我在 Normal 中有 Module1、Module2 和一些 UserForms。我也有模块和表单保存到文件,Module1.bas 和 Module2.bas 等。在运行宏(Macro1 中的过程)期间,我检查 Normal 中的 Module1 版本是否与文件 Module1.bas 中的版本相同,如果不同,我想删除 Module1 和 Module2。问题是我无法删除 Module1,因为我从该模块调用了过程。当宏停止工作时,将删除 Module1。

Private Function RemoveModulesAndForms() As Boolean

Dim component As Object
Dim componentName As String
Dim isComponentRemoved As Boolean
Dim notRemoved() As Variant
Dim i As Integer
Dim isAllRemoved As Boolean
Dim prompt As String
Dim caption_ As String

i = -1
isAllRemoved = True
For Each component In Application.Templates("Normal.dotm").VBProject.VBComponents
    componentName = component.name
    DoEvents
    If componentName <> "ThisDocument" Then
        Application.Templates("Normal.dotm").VBProject.VBComponents.Remove component
        
        isComponentRemoved = Not CheckIfComponentExist(componentName)

        If isComponentRemoved = False Then
            i = i + 1
            ReDim Preserve notRemoved(i)
            notRemoved(i) = componentName
            isAllRemoved = False
        End If
    End If
Next

prompt = "Components not removed: " + Join(notRemoved, ",")
If isAllRemoved = False Then
    MsgBox prompt, vbCritical
End If
RemoveModulesAndForms = isAllRemoved
End Function

有什么建议吗?

VBA MS-Word

评论

0赞 freeflow 11/9/2023
将执行检查的代码放在模块 3 中?在调用其他位置的代码之前,应先调用模块 3 中的代码,可能使用自动运行,以便它是打开文档时运行的第一个代码。
0赞 gregor 11/13/2023
我在 Module1 中有宏的版本,当我在 Module3 中放置检查版本时,我将不得不比较 Normal 中 Module1 的版本和文件 Module1 的版本,所以我将不得不调用 Module1,所以它不可能删除 Module1,也许调用 Module1 在内存中(对该模块的一些引用?

答: 暂无答案