下面的代码替换了目标工作簿中的模块。
该代码在当前目录的Updates子文件夹中搜索.bas文件,如果找到,则删除旧版本,然后导入代码。
有时,一个或多个替换模块的名称为xxxxx1,其中xxxxx是.bas文件的名称。它在模块名称后附加一个"1“。没有其他模块具有相同的名称。我已经检查了.bas文件,第一行正确地定义了VB_Name属性VB_Name = "xxxxx“。删除可以正常工作。有时是导入导致了这个问题。
我在删除后添加了一个Application.Wait,但问题仍然存在。
请注意,例程使用其他函数来确定目录路径并查找文件。调试证明它们是有效的。
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发布于 2020-01-28 18:08:12
问题似乎是一个模块并不总是被立即删除。在这种情况下,将添加1。我还没有弄清楚到底发生了什么:我在文件"exporter.bas“中有一个模块,名为"exporter",它有这个问题。我复制了一个文件"yippee.bas“并命名为"yippee”(文件内容与其他文件相同,所以没有“损坏的文件”),没有这个问题。
我发现VbComponents.Remove doesn't always remove module很有帮助。我实现了这个问题的海报的重命名解决方法。
https://stackoverflow.com/questions/50398449
复制相似问题