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 have ran into things like this before. The 'DoEvents' command often helps. Have you given that a shot?
Other times, I have placed the command in a do while loop and used a boolean to continually check if the command in question has succeeded. Including the DoEvents command in the loop is sometimes needed.
Just remember to put something in the loop so that after so many cycles it will give up. Getting stuck in an infinite loop can be pesky.
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