首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >导入.bas模块会更改模块的名称

导入.bas模块会更改模块的名称
EN

Stack Overflow用户
提问于 2018-05-18 02:40:58
回答 1查看 642关注 0票数 1

下面的代码替换了目标工作簿中的模块。

该代码在当前目录的Updates子文件夹中搜索.bas文件,如果找到,则删除旧版本,然后导入代码。

有时,一个或多个替换模块的名称为xxxxx1,其中xxxxx是.bas文件的名称。它在模块名称后附加一个"1“。没有其他模块具有相同的名称。我已经检查了.bas文件,第一行正确地定义了VB_Name属性VB_Name = "xxxxx“。删除可以正常工作。有时是导入导致了这个问题。

我在删除后添加了一个Application.Wait,但问题仍然存在。

请注意,例程使用其他函数来确定目录路径并查找文件。调试证明它们是有效的。

代码语言:javascript
复制
Dim iFilesNum As Integer
Dim iCount As Integer
Dim recMyFiles() As FoundFileInfo
Dim blFilesFound As Boolean
Dim directoryPath As String
Dim wbTarget As Workbook
Dim vbMod As Object
Dim myFile As String
Dim txtLine As String

Set wbTarget = ActiveWorkbook

directoryPath = getDirectoryPath("Updates")
If Dir(directoryPath, vbDirectory) = "" Then
    varX = MsgBox("Updates directory not found.  To apply updates you must first " & _
        "unload the updates from the file that was emailed to you.", vbOKOnly, "IPM by Merlin")
    Exit Sub
End If

blFilesFound = FindFiles(directoryPath, recMyFiles, iFilesNum, "*.bas", False)
If blFilesFound Then
    For iCount = 1 To iFilesNum
        With recMyFiles(iCount)
            .sName = Replace(.sName, ".bas", "")
            Set vbMod = Application.VBE.ActiveVBProject.vbComponents   'First we need to remove the existing module
            vbMod.Remove VBComponent:=vbMod.Item(.sName)
            Application.Wait (Now + #12:00:01 AM#)
            wbTarget.VBProject.vbComponents.Import directoryPath & "\" & .sName & ".bas"   'import the new module.
        End If
        Kill directoryPath & "\" & .sName & ".bas"
        End With
    Next

Else
    varX = MsgBox("No updates files found in the Updates directory.", vbInformation, "IPM by Merlin")

End If
EN

回答 1

Stack Overflow用户

发布于 2020-01-28 18:08:12

问题似乎是一个模块并不总是被立即删除。在这种情况下,将添加1。我还没有弄清楚到底发生了什么:我在文件"exporter.bas“中有一个模块,名为"exporter",它有这个问题。我复制了一个文件"yippee.bas“并命名为"yippee”(文件内容与其他文件相同,所以没有“损坏的文件”),没有这个问题。

我发现VbComponents.Remove doesn't always remove module很有帮助。我实现了这个问题的海报的重命名解决方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50398449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档