Replacing Code Modules using VBA

hicksi

Board Regular
Joined
Mar 5, 2012
Messages
214
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
I am trying to create a remote UPGRADE process that will replace a module programmatically in certain situations.
I am getting various issues which appear to be a process not completing before progressing.
Ie, I have been able to RENAME the module, IMPORT the new version, and then REMOVE the renamed version.
And that works great in debug mode, but when I run it live, I get a "duplicate definition" error (which I presume is because the removed and replaced version both contain the definition of an ENUM.
Is there a generally-known delay or some form of test to ensure that one process completes before continuing?
(No, DOEVENTS doesn't work, but should I try having 2 or 3 DOEVENTS to force completion)
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
try this bud I just made some research may be it can help but if the issues of duration is a bit low or long try the second soulution. Thanks
solution 1:
VBA Code:
Name "Module1", "Module1Temp"

 ' Wait for 1 second to allow time for the rename to complete 
Application.Wait Now + TimeValue("0:00:01") 

' Import the new version of the module 
Application.VBE.ActiveVBProject.VBComponents.Import "C:\Path\To\New\Module1.bas"

 ' Remove the renamed module 
Application.VBE.ActiveVBProject.VBComponents.Remove VBComponent:=Application.VBE.ActiveVBProject.VBComponents("Module1Temp")

solution 2:

VBA Code:
' Rename the module Name 
"Module1", "Module1Temp" 

' Import the new version of the module 
Do Until ModuleExists("Module1")    
On Error Resume Next    
Application.VBE.ActiveVBProject.VBComponents.Import "C:\Path\To\New\Module1.bas"    
If Err.Number = 0 Then Exit Do    
Err.Clear    
Application.Wait Now + TimeValue("0:00:01") ' wait for 1 second before retrying Loop 

' Remove the renamed module 
Do Until Not ModuleExists("Module1Temp")    
On Error Resume Next
    Application.VBE.ActiveVBProject.VBComponents.Remove VBComponent:=Application.VBE.ActiveVBProject.VBComponents("Module1Temp")  
  If Err.Number = 0 Then Exit Do   
 Err.Clear    Application.Wait Now + TimeValue("0:00:01") ' wait for 1 second before retrying Loop 

' Function to check if a module exists Function ModuleExists(ModuleName As String) As Boolean    
Dim i As Long    
For i = 1 To Application.VBE.ActiveVBProject.VBComponents.Count       
 If Application.VBE.ActiveVBProject.VBComponents(i).Name = ModuleName Then            
ModuleExists = True            
Exit Function       
 End If    
Next i    
ModuleExists = False
 End Function
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top