Export BOM code

Export BOM code

JasonMayes
Advocate Advocate
2,943 Views
2 Replies
Message 1 of 3

Export BOM code

JasonMayes
Advocate
Advocate

I have been looking all over the forums for guidance but seem to have fallen short. I have some code to export an assembly BOM to a text tab delimited file and it also saves it to a network folder but a weird thing keeps happening...

 

The info keeps reorganizing from what it should be see attached. I also need to name the file name that same as the assembly file name.

 

File that is named "BOM-StructuredAllLevels" is from the macro and the file named "File Name needs to be here" was created from Bill of Materials, export Bill of Materials, structured and export all levels, browse to the network drive, name file and change typr to text (tab delimited), and save.

 

macro code...

 

Public Sub BOMExport()
    ' Set a reference to the assembly document.
    ' This assumes an assembly document is active.
    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument

    ' Set a reference to the BOM
    Dim oBOM As BOM
    Set oBOM = oDoc.ComponentDefinition.BOM
    
    ' Set the structured view to 'all levels'
    oBOM.StructuredViewFirstLevelOnly = False

    ' Make sure that the structured view is enabled.
    oBOM.StructuredViewEnabled = True

    ' Set a reference to the "Structured" BOMView
    Dim oStructuredBOMView As BOMView
    Set oStructuredBOMView = oBOM.BOMViews.Item("Structured")
    
    ' Export the BOM view to a Text File Tab Delimited file
    oStructuredBOMView.Export "I:\Inventor BOMs\BOM-StructuredAllLevels.txt", kTextFileTabDelimitedFormat

End Sub
0 Likes
2,944 Views
2 Replies
Replies (2)
Message 2 of 3

MechMachineMan
Advisor
Advisor

1. It is commonly known that Exporting BOM does not retain the data in the order that it is set in the BOM. The workaround is to manually sort the data.

 

2. Look harder on the forums and you will find code to resort the columns WITHIN EXCEL.

 

3. Process the file in excel, then save it out from excel as a csv.


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

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @JasonMayes,

 

Inventor API will not support reordering or reorganizing columns. Columns are always ordered by column names.

 

After reorganizing or reordering columns through manually (User Interface) and exporting BOM to a text file, columns will appear in desired position. But, it can not be achieved via Inventor API.

 

Public Sub BOMExport()
    ' Set a reference to the assembly document.
    ' This assumes an assembly document is active.
    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument

    ' Set a reference to the BOM
    Dim oBOM As BOM
    Set oBOM = oDoc.ComponentDefinition.BOM
    
    ' Set the structured view to 'all levels'
    oBOM.StructuredViewFirstLevelOnly = False

    ' Make sure that the structured view is enabled.
    oBOM.StructuredViewEnabled = True

    ' Set a reference to the "Structured" BOMView
    Dim oStructuredBOMView As BOMView
    Set oStructuredBOMView = oBOM.BOMViews.Item("Structured")
    
    Dim name As String
    name = Replace(oDoc.DisplayName, ".iam", "")
    
    ' Export the BOM view to a Text File Tab Delimited file
    oStructuredBOMView.Export "I:\Inventor BOMs\" & name & ".txt", kTextFileTabDelimitedFormat

End Sub

The VBA code saves text file as assembly file name.

 

Please free to contact if there is any doubt.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes