Loading addins when starting inventor with VBA

Loading addins when starting inventor with VBA

Thomas.Long
Advocate Advocate
1,695 Views
11 Replies
Message 1 of 12

Loading addins when starting inventor with VBA

Thomas.Long
Advocate
Advocate

I am attempting to load inventor with VBA. While the program will start and load files it will not load all of my addins. This is specific to when starting it with VBA. If I do it manually by clicking on the icon, it loads everything normally. While I can simply let everyone know that they need to load Inventor manually, in order to avoid errors (one our save functions includes a sequential number generator on save, so it becomes impossible to save without the addins) I would like to know what is causing the issue and resolve it if possible.

'Opens an existing file or creates a new one. Returns the file reference
'Pass it the template you wish to use for a new document. Acceptable template files listed below
' Assembly (Imperial).iam, ASTM Angle.ipt, ASTM Channel.ipt, ASTM Miscellaneous Channel.ipt, ASTM Rectangular Tube.ipt, ASTM Wide Flange Beam.ipt, _
' Copper Bar Part (Imperial).ipt, Grating Material Template (Imperial).ipt, Non-Metallic Sheet Outsourced.ipt, Part - Other (Imperial).ipt, _
' Roll Fromed Coil.ipt, Sheet Metal Part (Imperial).ipt, Weldment (Imperial).iam
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Public Function OpenInventorFile(FileName As String, NewFile As Boolean) As Variant

    On Error Resume Next
    
    Dim InventorObjects(1) As Object
    Dim Doc As Object
    Dim Inventor As Object
    Dim iLogic As Object
    
    Dim Location As String
    Dim TemplateName As String
    
    Dim DocType As DocumentTypeEnum
    
    'Get open instance of inventor or open one if nothing is found
    Set Inventor = GetObject(, "Inventor.Application")
    If Err Then
        Err.Clear
        Set Inventor = CreateObject("Inventor.Application")
    End If

    Inventor.Visible = True
    
    'If this is an old file simply open it
    If NewFile = False Then
        Set Doc = Inventor.Documents.Open(FileName)
    
    'If it is a new file then determine the template and document and add a new file
    Else
        Location = "C:\Work\Designs\Templates\2013\Inventor Templates\Imperial\"
        TemplateName = Location & FileName
        
        If FileName = "Assembly (Imperial).iam" Or FileName = "Weldment (Imperial).iam" Then
            DocType = kAssemblyDocumentObject
        Else
            DocType = kPartDocumentObject
        End If
        
        Set Doc = Inventor.Documents.Add(DocType, TemplateName, True)
    End If
    
    
    Set InventorObjects(0) = Inventor
    Set InventorObjects(1) = Doc
    OpenInventorFile = InventorObjects

End Function
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
0 Likes
Accepted solutions (1)
1,696 Views
11 Replies
Replies (11)
Message 2 of 12

MechMachineMan
Advisor
Advisor

Add a wait loop to ensure inventor is ready after loading it before making subsequent calls to it.

 

Do Until invApp.Ready = True
    'Application.Wait(1)
Loop

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 3 of 12

Thomas.Long
Advocate
Advocate

Thanks MechMachineMan, long time no see!

 

That sort of did the trick. Now the addins are all loading, though for some reason the number generator program doesn't work for that initial save call, but as soon as I end the code and press the save button it works now. Odd to say the least, but I've never seen the program in question. At least when it calls the program it successfully loads all the addins now.

 

It's like it waits for the program to be ready, loads all the addins but doesn't initialize them until after my code finishes, because now it works right after the code is completed but not during.

0 Likes
Message 4 of 12

MechMachineMan
Advisor
Advisor
Accepted solution

Try putting this in your code and see what it says.

 

For j = 1 in invApp.ApplicationAddins.Count
     oAddin = invApp.ApplicationAddins.Item(j)
     
     oStr = oStr & j & ") " & oAddin.DisplayName & " - " & oAddin.Activated 
Next

MsgBox(oStr)

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 5 of 12

Thomas.Long
Advocate
Advocate

Thanks! That worked, I just took out the message box so I wasn't getting a bunch of blank message boxes but it generates the file names again. Thank you!

0 Likes
Message 6 of 12

MechMachineMan
Advisor
Advisor

Hmm that's weird.

 

That message box was supposed to just tell you if they were activated.

 

When you ran it, did it say they were all true the first time around?

 

Perhaps just accessing the addin objects was enough to make them "initialized" within inventor.


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 7 of 12

Thomas.Long
Advocate
Advocate

It didn't say anything at all, it didn't even give multiple message boxes the first time I ran it, it just gave a single empty message box. I know we have at least a dozen add ins listed that we have to load, so idk why it worked. I could tell it wasn't actually activating anything or checking if anything was activating, but hey if it works, I'll take it.

 

Your guess is as good as mine, I guess I could modify it instead to check that each add in is activated to ensure that its not just a fluke, but I ran it a few times. Anyways, at least its working for now.

0 Likes
Message 8 of 12

MechMachineMan
Advisor
Advisor

So you converted it to VBA, put it in after the ready wait loop, and it still showed nothing?

 

That's very interesting. I would be curious to see whether it's a bug, or where the .ApplicationAddins.Count is actually returning 0. -- Could you humour me and test this with a MsgBox(invApp.ApplicationAddins.Count) before running the addin check loop and after the wait loop?

 

It's also possible that simply adding an additional short wait after the Inventor.Ready is true would fix it (and this checking the activated status of the addins is just delaying it enough to let it load fully).

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 9 of 12

Thomas.Long
Advocate
Advocate

Let me see if I can get back to you tomorrow, k? Something kind of urgent has come up.

0 Likes
Message 10 of 12

Thomas.Long
Advocate
Advocate

Ok, I got done with the urgent thing and still had time to get back to this. It kicked out a 49 for the count, but still gave a blank message box for the one at the end. 

0 Likes
Message 11 of 12

MechMachineMan
Advisor
Advisor

Hmm i'm guessing it must be wrapped in an "On Error Resume Next" Error handling line?

The For j = 1 in ApplicationAddins is obviously a bug of my typing and should be "to" instead.


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 12 of 12

Thomas.Long
Advocate
Advocate

Yeah I had to fix to the loop before it would let me run it. Let me turn off the error handler and see what it gives me then

Edit: Nevermind, the program wont even run without  the error handler because it relies on it to get past getting the object  if it doesn't exist

0 Likes