Creating new inventor file from excel vba

Creating new inventor file from excel vba

Thomas.Long
Advocate Advocate
3,009 Views
5 Replies
Message 1 of 6

Creating new inventor file from excel vba

Thomas.Long
Advocate
Advocate

I have a function I've been writing that is supposed to take a boolean that specifies whether the file is a new file to be opened from a template or is an existing file and then either the template to be used or the file name and location.

 

Unfortunately when I try to ask for a new file its telling me that upon returning the file that I'm having a type mismatch. What's odd is that I'm setting it to the exact same variable (type object) as whenever I open an existing file and they're both returning a document type. Any idea what's going on?

 

'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
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Private Function OpenInventorFile(FileName As String, NewFile As Boolean) As Object

    'On Error Resume Next
    
    Dim Doc As Object
    Dim Inventor As Object
    Dim InvVBAProject As Object
    Dim InvVBAModule As Object
    Dim InvVBAProcedureToRun As Object
    
    Dim Location As String
    Dim TemplateName As String
    
    
    '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 NewFile = False Then
        Set Doc = Inventor.Documents.Open(FileName)
    Else
        Location = "C:\Work\Designs\Templates\2013\Inventor Templates\Imperial\"
        TemplateName = Location & FileName
        Set Doc = Inventor.Documents.Add(TemplateName)

    End If
    
    Set InvVBAProject = Inventor.VBAProjects.Item(1)
    Set InvVBAModule = InvVBAProject.INventorVBAComponents.Item(2)
    Set InvVBAProcedureToRun = InvVBAModule.InventorVBAMembers.Item(1)
    InvVBAProcedureToRun.Execute
    
    OpenInventorFile = Doc

End Function
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
0 Likes
Accepted solutions (1)
3,010 Views
5 Replies
Replies (5)
Message 2 of 6

MechMachineMan
Advisor
Advisor

Hi. Please review this link and it will all be clear.

 

http://help.autodesk.com/view/INVNTOR/2018/ENU/?guid=GUID-66A32241-D9C0-4E9D-B6D1-F4A9DA1F69F9

 

Hint: (You are not using a certain function call properly, and this is why you are getting an error)


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

Thomas.Long
Advocate
Advocate

So the error is because the function is supposed to return a document type rather than an object type? I'm a little curious, sorry, I've been using object types for holders as documents back in autocad, especially whenever I write out to an excel workbook so I thought that would be the most logical go to.

 

This program won't only be used by me so I'm leery of using the inventor reference library in order to include types. Will the document type still work without the inventor library reference? Or is there a way to add the library reference in the program? Or do I just need to make sure every time someone new joins the group or something that they enable the library references?

 

Edit: Nevermind, I think I see what you're saying. I need to clarify that this is in excel vba, so I'm not sure does it still use the inventor method or the excel method? Because they have vastly different .add methods apparently. I'll try running it with a documenttypeenum though and run it with the inventor method instead of standard vba .add method.

0 Likes
Message 4 of 6

MechMachineMan
Advisor
Advisor
Accepted solution

Your edit there is correct.

 

And no, they shouldn't have different methods. The information for the methods and properties as intellisense gathers it is found in an xml file that is stored alongside the Inventor .dlls, and shouldn't be specific to the language.

I have seen some languages perform the method calls different, but the arguments the calls take should generally be the same order/format...

 

 

Capture.PNG


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

Thomas.Long
Advocate
Advocate

Hmmm odd, I just tried typing in DocumentsTypeEnum and it kept trying to correct away from it. I'll double check but it looks like I might just have to enable the library reference for everyone. Yeah it allows the DocumentTypeEnum after I enable the reference.

 

This is what I was using for my documents.Add method from before. It specified that it was for word but I figured it was an office product so it probably used the same stuff as excel.

https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.documents.add.aspx

 

My last thought is whether its easiest to just specify doctype kUnknownDocumentObject for everything or if weldments count as kAssemblyDocumentObjects.

 

 

Edit: Okay, it's almost working now. The program is opening a new assembly correctly, but it's returning the error "Object Variable or Wtih block variable not set.

 

It looks like its stepping into a project I didn't mean it to. I actually cribbed a chunk of this code from someone else. I don't suppose you could tell me what those last 4 lines do and mean? I'm guessing that its somehow specifying a vba macro in inventor to start up, but the name of the one it started up was called Place_Part(FileName)

 

2nd Edit: The error was because I didn't have a set statement on the return statement.

0 Likes
Message 6 of 6

MechMachineMan
Advisor
Advisor

This is what I was using for my documents.Add method from before. It specified that it was for word but I figured it was an office product so it probably used the same stuff as excel.

https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.documents.add.aspx

  


 

That's a good try; but the wrong approach. Sometimes you might LUCK OUT if the programmer programmed the generic objects to function the same as other applications, but it generally won't work.

 

It's recommended you always use the API resources for the application you are using, and not trying to "borrow" generic objects from other applications. In this case, ANY TIME you are working with a property/method/child component of anything from inventor, you should be using the local programming help file, or the online programming help.

 

Also, you DON'T need to add the references to inventor, and can use it as late binding. However, it will be slightly slower (not usually too noticeable, unless performing a large number of operations). BUT if you use it as late binding, you need to specify the enums as an integer, as what early binding does is allow for the innate conversion of those literal enums to their integer values.


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