Remove VBA code

Remove VBA code

RobJV
Collaborator Collaborator
992 Views
4 Replies
Message 1 of 5

Remove VBA code

RobJV
Collaborator
Collaborator

Many years ago, I used to place VBA in my sheet metal template.   I am now working on a project that started with a "copy design" of one of these assemblies with code embedded in the parts. It is quite a nightmare as it takes forever to load among other issues.  I have gone through many of the parts and manually removed module2 which contains the offending code but is there an easier way?

 

Rob

0 Likes
993 Views
4 Replies
Replies (4)
Message 2 of 5

dano0310
Advocate
Advocate

Hi I had the same problem with an autosave macro in my part files.

 

Sub Delete_AutoSave()
    Dim oApp As Application
    Set oApp = ThisApplication
   
    Dim VBProj, VBComp, CodeMod, ProcName, vbext_pk_Proc, StartLine, NumLines
     Set VBProj = oApp.VBAProjects
    
     Dim i, j
     For i = 1 To VBProj.count
        If VBProj(i).ProjectType <> kApplicationVBAProject Then
            For j = 1 To VBProj(i).InventorVBAComponents.count
                If VBProj(i).InventorVBAComponents(j).Name = ("AutoSave1") Then
                    Set VBComp = VBProj(i).InventorVBAComponents("AutoSave1").VBComponent
                   
                    Set CodeMod = VBComp.CodeModule
                   
                    ProcName = "AutoSave"
        With CodeMod
            StartLine = .ProcStartLine(ProcName, vbext_pk_Proc)
            NumLines = .ProcCountLines(ProcName, vbext_pk_Proc)
            .DeleteLines StartLine:=StartLine, count:=NumLines
        End With

           
                End If
            Next j
        End If
    Next i

End Sub

 

The module name was AutoSave1 and the sub was AutoSave from memory.  Give this a try and let me know if it works.  I have another one there which does a similar thing if it doesn't.

Dan

0 Likes
Message 3 of 5

RobJV
Collaborator
Collaborator

Hi Dan,

 

Thanks for the code - I will give it a try when i am back on that project.  Without delving into the code, I assume that I can run that from the parent assembly?

 

Rob

0 Likes
Message 4 of 5

RobJV
Collaborator
Collaborator

I tried it out but did not seem to do anything?

0 Likes
Message 5 of 5

dano0310
Advocate
Advocate

Hi After going back over it the last one only deletes it out of the active file. 

 

Mind the slopy programming I'm an engineer not a programmer.

 

This one should iterate through a few levels of assemblies and delete the "Autosave" sub from the "Autosave1" module

 

Sub Delete_AutoSave_In_All_Docs()
    Dim oApp As Application
    Set oApp = ThisApplication
   
    Dim VBProj, VBComp, CodeMod, ProcName, vbext_pk_Proc, StartLine, NumLines
     Set VBProj = oApp.VBAProjects
    
     Dim k As Integer
     For k = 1 To oApp.FileManager.Files.count
        If oApp.FileManager.Files(k).AvailableDocuments(1).DocumentType = kAssemblyDocumentObject Then
        Dim oFile As Document
        Set oFile = oApp.Documents.Open(oApp.FileManager.Files(k).FullFileName)
       
     Dim i, j
     For i = 1 To VBProj.count
        If VBProj(i).ProjectType <> kApplicationVBAProject Then
            For j = 1 To VBProj(i).InventorVBAComponents.count
                If VBProj(i).InventorVBAComponents(j).Name = ("AutoSave1") Then
                    Set VBComp = VBProj(i).InventorVBAComponents("AutoSave1").VBComponent
                   
                    Set CodeMod = VBComp.CodeModule
                   
                    ProcName = "AutoSave"
                   
                    If CodeMod.countoflines > 10 Then
        With CodeMod
            StartLine = .ProcStartLine(ProcName, vbext_pk_Proc)
            NumLines = .ProcCountLines(ProcName, vbext_pk_Proc)
            .DeleteLines StartLine:=StartLine, count:=NumLines
        End With
                    End If
           
                End If
            Next j
        End If
    Next i
    'oFile.Close False
    End If
    Next k
End Sub

 

Let me know if this one works.

Dan

0 Likes