Inventor Fails to load properly when opened by vba

Inventor Fails to load properly when opened by vba

MechMachineMan
Advisor Advisor
1,792 Views
9 Replies
Message 1 of 10

Inventor Fails to load properly when opened by vba

MechMachineMan
Advisor
Advisor

Sometime when I open inventor with VBA, it fails to properly load the environments, however, other times it works just fine.

 

Attached is a picture of what it looks like, and the snipped of code I'm using to open inventor.

 

Any help would be much appreciated!

 

Sub Main()	
Dim oActiveDoc As Document oActiveDoc = ThisApplication.ActiveDocument Dim oSS As SelectSet oSS = oActiveDoc.SelectSet If oSS.Count = 0 Exit Sub End If Dim oNewInv As Object oNewInv = GetObject("", "Inventor.Application") oNewInv.Visible = True If Err.Number <> 0 MsgBox("Error Opening Inv") Exit Sub End If oNewInv.DesignProjectManager.DesignProjects.ItemByName("Default").Activate If Err.Number <> 0 MsgBox("Error Setting project to default") Exit Sub End If oNewAsm = oNewInv.Documents.Add(DocumentTypeEnum.kAssemblyDocumentObject,,True)

'At this point, actual code adds occurrences based on a select set in the active document (not the new one)

End Sub

 

*see how there is no ribbon environments opened for assembly, and the model browser is not properly loaded.

 

FaultyLoad.JPG


--------------------------------------
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
Accepted solutions (1)
1,793 Views
9 Replies
Replies (9)
Message 2 of 10

Owner2229
Advisor
Advisor

Hey, I've already seen it in some of your codes, you're calling GetObject("", "Inventor.Application") instead of CreateObject("Inventor.Application").

 

Also, this doesn't look like VBA to me, so I'm just gonna assume it's VB.Net. I'm using this method to open Inventor as it is more reliable:

 

Public Function Inventor_StartApp(Optional Visible As Boolean = True) As Inventor.Application
	Dim iApp As Inventor.Application = Nothing
	Try
		Dim iAppType As Type = GetTypeFromProgID("Inventor.Application")
		iApp = CType(CreateInstance(iAppType), Inventor.Application)
		iApp.Visible = Visible
	Catch ex As Exception
		MsgBox("Creation of new Inventor instance failed - " & ex.Message)
	End Try
	Return iApp
End Function
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 3 of 10

MechMachineMan
Advisor
Advisor
Do you know if there is other different behaviour between the 2 methods, or
why one would cause it to load correctly?

As far as any documentation I've come across, nowhere really goes in depth
and they all say they are essentially the same function.

--------------------------------------
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 4 of 10

Owner2229
Advisor
Advisor

From Microsoft:

"Use the GetObject function when there is a current instance of the object or if you want to create the object with a file already loaded. If there is no current instance, and you do not want the object started with a file loaded, use the CreateObject function."

 

So, the GetObject will only pop up new Inventor if there already is one running (aka. the Inventor.exe is loaded).

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 5 of 10

MechMachineMan
Advisor
Advisor

@Owner2229 wrote:

 

So, the GetObject will only pop up new Inventor if there already is one running (aka. the Inventor.exe is loaded).


 

Hmm. I don't think this is quite correct.

 

From my past experience, 

 

GetObject("", "Inventor.Application")  and

CreateObject("Inventor.Application") 

function the same.

 

GetObject(,"Inventor.Application") 'NO empty brackets as first arg,

actually grabs the active instance.

 

GetObjectContext.CreateInstance() appeared to do the same thing, so I always just used GetObject() because it's easier.

 

 

 

HOWEVER, I came across this link this morning that says each one is better in certain situations, and it depends on your needs. However, CreateInstance seems to generally be the safest because it grabs more of the COM routines from what the target application sends it, whereas Get/CreateObject often opens it as light as possible.

 

See link: http://www.informit.com/articles/article.aspx?p=24108&seqNum=7


--------------------------------------
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 6 of 10

Owner2229
Advisor
Advisor

Hmm, interesting article. Anyway, you can use it like this:

 

Dim iApp As Object = GetObject / CreateObject / CreateInstance
If iApp Is Nothing Then
     Threading.Thread.Sleep(100) 'give it some time
     iApp = GetObject / CreateObject / CreateInstance
End If
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 7 of 10

MechMachineMan
Advisor
Advisor

Also, the issue isn't that it is failing to load the application completely. As the picture in the original post shows, that is the window that is opened. It is just that it is not properly loading the environments, and will open the window without the proper ribbons shown.


--------------------------------------
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 8 of 10

Owner2229
Advisor
Advisor

That never happened to me. It seems to be a bug in Inventor.

Do you wait for the Inventor to fully load before you send it a command?

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 9 of 10

MechMachineMan
Advisor
Advisor
No? Just use it as declaring the object and do not utilize a loop to wait
for ready.

--------------------------------------
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 10 of 10

MechMachineMan
Advisor
Advisor
Accepted solution

I ran across issues with this again when I was trying to run a rule to disable event tracking VBA components immediately after opening... 

 

Looks like the code was trying to send commands before inventor could receive them, which wasn't causing errors, but also wasn't causing inventor to run the commands.

 

The fix was to add a wait loop to ensure inventor was ready. Seems to be working just find with this in now!

 

 

 

oInvApp = GetObject / CreateObject / Create Instance

Do Until oInvApp.Ready = True
    System.Threading.Threads.Sleep(10)
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
0 Likes