I am trying to rename a module which leads to the run time error. In the below code, 'vbComp.name = moduleName', this line is causing the run time error. When I looked on it more, I found that the same module already exists and because of that it not allowing it to rename. Even though I called Remove reference, it is actually not removing the reference.. I tried Doevents, I called the remove reference code in a separate procedure as I shown below.. Still after executing the code, the module is not removed.
Some additional information. So basically this code is being called in 'a.xla' and the XLAName being passed is a different xla 'b.xla'
Code:Private Sub AddFileToAddIn(XLAName As String, moduleName As String, Filename As String) Dim vbc As VBComponent Dim ActiveProjName As String ActiveProjName = Application.VBE.ActiveVBProject.name Dim vbproj As VBProject Dim vbComp As VBComponent ' Find the project named XLAName For Each vbproj In Application.VBE.VBProjects If vbproj.name = XLAName Then ' Add the file named FileName Set vbComp = vbproj.VBComponents.Import(Filename) If Not vbComp Is Nothing Then ' If we have successfully imported the file ' See if a module with the name - ModuleName - already exists in this project, and if so, delete it RemoveModuleFromAddin vbproj, moduleName '-----> **Not deleting the moduleName** ' Change the new modules name to ModuleName from "module1" vbComp.name = moduleName '---->**Since the moduleName is not deleted and it exists, renaming it is causing the run time error.** End If Exit Sub End If Next vbproj End Sub Private Sub RemoveModuleFromAddin(proj As VBProject, moduleName As String) Dim module As VBComponent For Each module In proj.VBComponents If module.name = moduleName Then proj.VBComponents.Remove module Exit Sub End If Next module End Sub




Reply With Quote
