Removing & Importing a macro module

G

Glen Mettler

I have several macros in Module3. Several workbooks use the module. When i
make changes to the code, I copy the module to a folder for import into the
other workbooks. The "update" module resides in Module1. The problem is
this - when I select "Update Macro" (from a user menu) the Module 1 macros
run. It is supposed to delete Module3 and then Import the new module (file
name *.bas) that I have put in the current directory with the workbook.
However, it often does not delete Module3 but then adds Module31. When I
run Auto_Open I get and "Ambiuous" error message because Module3 is still
there. Here is the code I got from somebody in the newsgroup many months
ago.

How can I ensure that Module3 is deleted/removed and that the import is
named Module3 and not Module31?

==================
Sub UpdateCode()
FName = Dir("*.bas")

If Len(FName) = 0 Then
MsgBox "No Code files in the Directory"
Exit Sub
Else
Application.ScreenUpdating = False
Application.EnableEvents = False
Call DeleteModule
Call ImportModule (FName)
CodeFile = True
Call auto_open
End If

Application.ScreenUpdating = True
Application.EnableEvents = True
If CodeFile Then
MsgBox "Code has been Updated"
Kill "*.bas"
Else
MsgBox "Code has NOT been updated - file not found"
End If

End Sub
Sub ImportModule(Modname As Variant)
ActiveWorkbook.VBProject.VBComponents.Import Modname
Application.Visible = True


End Sub
Sub DeleteModule()
Dim VBComp As VBComponent
On Error Resume Next
'remove module if named Module3 or Module31
Set VBComp = ActiveWorkbook.VBProject.VBComponents("Module3")
ThisWorkbook.VBProject.VBComponents.Remove VBComp
Set VBComp = ActiveWorkbook.VBProject.VBComponents("Module31")
ThisWorkbook.VBProject.VBComponents.Remove VBComp

End Sub
 
B

Bob Phillips

It works okay for me, but I did notice one thing, you start by using
ActiveWorkbook,

Set VBComp = ActiveWorkbook.VBProject.VBComponents("Module3")

and then switch to ThisWorkbook.

ThisWorkbook.VBProject.VBComponents.Remove VBComp

In mys test, they are the same, but probably not in your scenario. Seems
they should both be Activeworkbook to me.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
G

Glen Mettler

Thanks Bob,
That was part of the problem. Now I am getting Module311 after deletes and
imports

Glen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top