Autorun VBA when opening file

Autorun VBA when opening file

ChristianAndersenIsmyname
Advocate Advocate
1,248 Views
7 Replies
Message 1 of 8

Autorun VBA when opening file

ChristianAndersenIsmyname
Advocate
Advocate

Hi, is it possible to autorun this VBA code I've found from a 8 year old forum post whenever I open a part/assembly?

 

Public Sub TurnAllOff()
    ' Make sure a part of assembly is active.
    If ThisApplication.ActiveDocumentType = kAssemblyDocumentObject Or _
            ThisApplication.ActiveDocumentType = kPartDocumentObject Then
       Dim doc As Document
       Set doc = ThisApplication.ActiveDocument
       
       Dim objVis As ObjectVisibility
       Set objVis = doc.ObjectVisibility
       
       objVis.AllWorkFeatures = False
       objVis.Sketches = False
       objVis.Sketches3D = False
    Else
        MsgBox "A part of assembly must be active."
    End If
End Sub

 

0 Likes
Accepted solutions (2)
1,249 Views
7 Replies
  • VBA
Replies (7)
Message 2 of 8

ckeveryga
Advocate
Advocate
Accepted solution

You can add an iLogic event trigger where a certain rule will run "After Open Document". Then, in that rule add this code to run your macro:

InventorVb.RunMacro("projectName", "moduleName", "macroName")

 

Message 3 of 8

WCrihfield
Mentor
Mentor

Yes, it is possible. There are a few ways to do it, but probably the simplest way would be to create a simple external iLogic rule and use the following line within it:

 

InventorVb.RunMacro("projectName", "moduleName", "macroName")

 

where you have customized it with your VBA project name, module name, and macro name.

Then open the Event Triggers dialog, go to the Parts tab, drag this new rule over under the "After Open Document" event.  Then do the same thing on the Assemblies tab.

Oops. Looks like I took a little too long typing.

Hey, great minds think alike. 😁

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 8

ChristianAndersenIsmyname
Advocate
Advocate

I'm sure this is easy enough to do, but I still can't get it to work.

But I'm getting error:

"RunMacro: No VBA component or module named "ApplicationProject" was found in the VBA project named "ApplicationProject"."

ChristianAndersenIsmyname_0-1594729849178.png

What's the correct name of the project?

InventorVb.RunMacro("ApplicationProject", "ApplicationProject", "TurnAllOff")
0 Likes
Message 5 of 8

ckeveryga
Advocate
Advocate

The second argument is your sub name, so if within your module you have 

 

Sub Main()

 

you would instead write 

 

InventorVb.RunMacro("ApplicationProject", "Main", "CheckForIDWs")

Edit: My macro name is "CheckForIDWs"

 

0 Likes
Message 6 of 8

WCrihfield
Mentor
Mentor
InventorVb.RunMacro("ApplicationProject", "Module1", "TurnAllOff")

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 8

ChristianAndersenIsmyname
Advocate
Advocate

Don't get it to work, but I havent had time to try it out very much either.

I'm marking as solved.

0 Likes
Message 8 of 8

WCrihfield
Mentor
Mentor
Accepted solution

To help simplify things, I have also converted the code to iLogic, so you can just run it straight from the external iLogic rule, instead of trying to point it to the external VBA macro.

See below:

If oDocType <> DocumentTypeEnum.kAssemblyDocumentObject And _
	oDocType <> kPartDocumentObject Then
	MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Part or Assembly Documents.",vbOKOnly, "WRONG DOCUMENT TYPE")
	Return
End If
Dim oOVis As ObjectVisibility = ThisApplication.ActiveDocument.ObjectVisibility
oOVis.AllWorkFeatures = False
oOVis.Sketches = False
oOVis.Sketches3D = False

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes