Mass update of inventor macros

Mass update of inventor macros

jparks_79
Collaborator Collaborator
412 Views
6 Replies
Message 1 of 7

Mass update of inventor macros

jparks_79
Collaborator
Collaborator

Hello all,

 

We have a bit of an issue that we are trying to solve. We have several 1000 ipt where we have the same macro saved for various reasons. We now need to make updates to all those macros. has anyone ran into this issue before? If so what kind of solution did you find?

 

Anyone have any ideas other than brute force?

Thanks!

0 Likes
413 Views
6 Replies
Replies (6)
Message 2 of 7

FINET_Laurent
Advisor
Advisor

Hi @jparks_79,

 

What about external rules ?

 

Kind regards,

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes
Message 3 of 7

WCrihfield
Mentor
Mentor

Are you talking about actual VBA macros that are being stored at document level, or are you talking about internal iLogic rules stored within those documents?  Big difference.  It is relatively easy to find/replace internal iLogic rules from another iLogic rule.  I am not that familiar with finding document level VBA macros, then editing or replacing code in those.  Please clarify?  What kinds of changes do you need to make?  Would deleting the existing code, then replacing it with all new code work OK for you?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 7

jparks_79
Collaborator
Collaborator

Hi,

 

We are looking at document level macros. There are a few ilogic rules in there, but primarily the document level macros are what is causing headaches.

 

There are terms or variable values that need updated. Someone changed the database structure in another application and now we must match the naming. We could either search the document for these terms and replace them, but it would likely be easier just to replace the entire macro. If I am thinking pure brute force(highlight all and paste)

0 Likes
Message 5 of 7

WCrihfield
Mentor
Mentor

That's a though one.  I know how to access all the VBA projects, modules, macros, arguments, and such by code, but I do not really know how to replace a Module or Macro by code, or how to edit the code of a VBA macro from either another VBA macro, or from an iLogic rule.  Yes, we can access the VBA stuff from iLogic rules too, but it would be awkward (if possible) to edit VBA code from iLogic, because iLogic uses vb.net formatting, which is different than VBA.  I may have to do more research / testing to figure something like that out.  Maybe if approached entirely from VBA standpoint, and maybe if we are able to 'Cast' certain generic Object type property values over to recognized VBA objects, it may be possible, but I simply have not dug that deep into those object types yet, so I am not sure yet.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 7

WCrihfield
Mentor
Mentor

OK, so I did seem to discover a way to access the code within one macro, from within another macro, but have not had time to fully explore its various properties and methods yet, and VBA side documentation seems to be difficult to come by for the things I'm finding.

As you may, or may not know, the primary starting point of accessing Inventor's VBA stuff though using Inventor's API, is through the ThisApplication.VBAProjects property.  That property returns an InventorVBAProjects (a type that is still native to Inventor).  But that object (and many if its child objects that are native to Inventor) also offer a property for accessing the non-native object that it represents, as a generic Object, not as that other Type.  However, if going at this entirely from VBA, there is a reference you can turn on, which will enable recognition of the proper object Types, and allow you to dig deeper into those non-native objects, with some Intellisense help.  That reference is "Microsoft Visual Basic for Applications Extensibility 5.3".  With the code screen open for your new macro that you will be using to access your old macros, to go the Tools tab, then References.  Then scroll to that reference and make sure the check box next to it is checked, then click OK, to get back to your code screen.

Below is a starter VBA macro code that I was exploring with.  You can play around with this some more if you want.  I was able to extract a couple lines of code from a little test macro within another open Inventor document's VBA project, and show it in a MsgBox, just as a test, and it worked OK.  I also see other methods in there that sound interesting.  It may just take some good old trial & error testing and further exploration.

Sub EditOtherVBAMacro()
    Dim sModuleName As String
    sModuleName = "Module1"
    Dim sMacroName As String
    sMacroName = "MyMacro1"
    Dim oProjects As InventorVBAProjects
    Set oProjects = ThisApplication.VBAProjects
    Dim oProject As InventorVBAProject
    For Each oProject In oProjects
        If oProject.ProjectType = kDocumentVBAProject Then
            'must have reference turned on for "Microsoft Visual Basic for Applications Extensibility 5.3"
            Dim oVBProj As VBProject '(VBA only Type, recognized by reference)
            Set oVBProj = oProject.VBProject
            'Components can be standard Module, Class Module, or UserForm
            Dim oVBComps As VBComponents
            Set oVBComps = oVBProj.VBComponents
            Dim oVBComp As VBComponent
            For Each oVBComp In oVBComps
                If oVBComp.Type = vbext_ct_StdModule And oVBComp.Name = sModuleName Then
                    Dim oCodeModule As CodeModule
                    Set oCodeModule = oVBComp.CodeModule
                    'there are multiple methods & properties available under this object type
                    'we may be able to use these to edit the code as needed
                    
                    'Dim o2LinesOfCodeFromOtherMacro As String
                    'o2LinesOfCodeFromOtherMacro = oCodeModule.Lines(2, 2)
                    'Call MsgBox(o2LinesOfCodeFromOtherMacro, , "")
                    
                    'Call oCodeModule.DeleteLines(1, oCodeModule.CountOfLines)
                    'Call oCodeModule.AddFromFile(sFileName)
                    'oCodeModule.AddFromString(sMyReplacementCodeString)
                End If
            Next 'oVBComp
        End If
    Next 'oProject
End Sub

I did manage to find some relative documentation for some of these non-native objects, but from the .Net side of the equation, not the VBA side.

https://learn.microsoft.com/en-us/dotnet/api/microsoft.vbe.interop?view=office-pia 

This link below also seemed informational, so you may want to take a look at that too.

https://www.engram9.info/excel-2002-vba-xml-asp/vbproject-object-vbcomponent-object-and-reference-ob... 

Once you have that reference turned on, you can select some of the non-native object properties, then right-click, and choose Define.  That usually shows the Object Browser (if it was not already visible), and displays information about that object/property.  However, at the top left corner of the Object Browser, it is best if you use the drop-down list, and set that to "VBIDE" first.  Then the Classes & Members lists below will be filled with stuff defined under that resource.  You can see all those object types, can click on them, and see the members of each type that way.  Wish I could be of more help, but I've got to get some other stuff done right now.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 7

jparks_79
Collaborator
Collaborator

Hi Wesley,

 

You have given me more than a great starting point. But I will need a couple days to try what you are suggesting below. Thank you for the support!

 

Jeremy

0 Likes