VbComponents.Remove并不总是删除模块 [英] VbComponents.Remove doesn't always remove module
问题描述
我正在尝试使用Chip Pearson的代码来覆盖现有的VBA代码模块,并从另一个项目导入。原始代码此处。
I'm trying to use Chip Pearson's code for overwriting an existing VBA code module with an import from another project. Original code here.
我正在看的特定部分是:
The particular section I'm looking at is:
With ToVBProject.VBComponents
.Remove .Item(ModuleName)
End With
但这个 VBComponents.Remove $ c $一旦VBA执行停止,c>调用有时只能实际生效 - 也就是说,所有语句完成后,删除操作才会生效,或者代码点击一个断点,然后停止调试。这是一个问题,因为导入新模块或使用新模块替换现有模块代码的以下代码:
But this VBComponents.Remove
call will sometimes only actually take effect once VBA execution has stopped--that is, the delete operation doesn't take effect until all statements are finished, or if the code hits a breakpoint and then I stop debugging. This is a problem because of the following code for either importing a new module or replacing the existing module's code with the new module:
Set VBComp = Nothing
Set VBComp = ToVBProject.VBComponents(CompName)
If VBComp Is Nothing Then
ToVBProject.VBComponents.import filename:=FName
Else
If VBComp.Type = vbext_ct_Document Then
'delete the module's code,
'import a temp module,
'copy over the temp module code,
'delete the temp module
End If
End If
模块删除尚未生效,所以在调试器知道的情况下, VBComp
不是 Nothing
所以 .import
不会被调用。
The module deletion hasn't taken effect yet, so VBComp
is not Nothing
, as far as the debugger knows. So the .import
won't be called.
即使我发表了如果VBComp.Type = vbext_ct_document then
和 end if
,以便新模块的代码将覆盖现有的代码,无论什么 VBComp.Type
它是,一旦代码完成执行,该模块将仍然最终被删除,并且不会导入将替换它。
Even if I comment out the if VBComp.Type = vbext_ct_document then
and end if
so that the new module's code will overwrite the existing one no matter what VBComp.Type
it is, the module will still end up getting deleted once the code finishes executing, and no import will happen to replace it.
奇怪的是,这不会发生在所有模块;一些实际上在 VBComponents.Remove
调用后实时删除。
What's odd is that this doesn't happen with all modules; some actually get deleted in real time after the VBComponents.Remove
call.
我看过几个不同的帖子这在各种论坛上,没有令人满意的解决方案。现在我正在使用一个解决方法来更改 .Remove
调用:
I've seen several different posts about this on various forums, and no satisfactory solution. For now I'm using a workaround of changing the .Remove
call to:
With ToVBProject.VBComponents
.Item(ModuleName).name = ModuleName & "_remove"
.Remove .Item(ModuleName & "_remove")
End With
,以便通过更改名称, ModuleName
似乎不再存在,因此 .import
调用将发生。这当然假设没有名为 ModuleName&
so that by changing the name, ModuleName
appears to no longer exist and therefore the .import
call will occur. This assumes, of course, that no module named ModuleName & "_remove"
actually exists.
有没有更好的解决方案?
Is there a better solution?
推荐答案
我尝试重命名,发现它造成了表单模块和ThisWorkbook的问题。所以我略微修改了重命名非文档模块。这似乎是干净的。
I tried the renaming and found that it caused problems with the sheet modules and ThisWorkbook. So I modified it slightly to rename only the non document modules. This seems to work cleanly.
If .Item(ModuleName).Type <> vbext_ct_Document Then
.Item(ModuleName).Name = ModuleName & "_OLD"
.Remove .Item(ModuleName & "_OLD")
Else
.Remove .Item(ModuleName)
End If
这篇关于VbComponents.Remove并不总是删除模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!