Message 1 of 7
Export BOM to Excel VBA
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello,
I have found an export BOM VBA rule.
Afterward I have customized it to work for my purpose.
Public Sub BOM_Export()
' Set a reference to the assembly document.
' This assumes an assembly document is active.
Dim oDoc As AssemblyDocument
Set oDoc = ThisApplication.ActiveDocument
Dim oTemplate As String: oTemplate = "I:\Engineering\Formulieren en lijsten\Spare & wear parts lijst\Spare_wear_parts_Engels_Nieuw artikelnummersysteem.xltx"
' Set a reference to the BOM
Dim oBOM As BOM
Set oBOM = oDoc.ComponentDefinition.BOM
' Set whether first level only or all levels.
oBOM.StructuredViewFirstLevelOnly = False
' Make sure that the structured view is enabled.
oBOM.StructuredViewEnabled = True
'Set a reference to the "Structured" BOMView
Dim oBOMView As BOMView
Set oBOMView = ThisApplication.ActiveDocument.ComponentDefinition.BOM.BOMViews.Item("Structured")
' Dim oBOMView As BOMView
Set oBOMView = oBOM.BOMViews.Item("Structured")
Dim oPartNumProperty As String
oPartNumProperty = oDoc.ComponentDefinition.Document.PropertySets( _
"Design Tracking Properties")("Part Number").Value
Dim oPartRevNum As String
oPartRevNum = oDoc.ComponentDefinition.Document.PropertySets( _
"Inventor Summary Information")("Revision Number").Value
Dim oPartTitle As String
oPartTitle = oDoc.ComponentDefinition.Document.PropertySets( _
"Inventor Summary Information")("Title").Value
Call oBOMView.Sort("Item", True)
' set excel app and add worksheet
Dim xlApp As Object
Dim xlwb As Object
Dim xlws As Object
Set xlApp = CreateObject("Excel.Application")
Set xlwb = xlApp.Workbooks.Open(oTemplate)
'Set xlwb = xlApp.workbooks.Add
Set xlws = xlwb.Worksheets(1)
xlApp.Visible = True
' write more stuff
' xlws.Cells(1, 2) = oPartTitle
' xlws.Cells(1, 3) = oPartNumProperty
' xlws.Cells(1, 4) = "Rev: " & oPartRevNum
xlws.Name = "Export list " & oPartNumProperty
'Initialize the tab for ItemNumber
Dim ItemTab As Long
ItemTab = -3
Dim oStartRow As Integer: oStartRow = 2
Call QueryBOMRowProperties(oBOMView.BOMRows, ItemTab, xlApp, xlwb, xlws, oStartRow)
End Sub
Private Sub QueryBOMRowProperties(oBOMRows As BOMRowsEnumerator, ItemTab As Long, ByVal xlApp As Object, ByVal xlwb As Object, ByVal xlws As Object, oStartRow As Integer)
ItemTab = ItemTab + 3
' Iterate through the contents of the BOM Rows.
Dim i As Long
For i = 1 To oBOMRows.Count
' Get the current row.
Dim oRow As BOMRow
Set oRow = oBOMRows.Item(i)
'Set a reference to the primary ComponentDefinition of the row
Dim oCompDef As ComponentDefinition
Set oCompDef = oRow.ComponentDefinitions.Item(1)
Dim oPartNumProperty As Property
Dim oPartTitle As Property
Dim oPartDescription As Property
Dim oPartMaterial As Property
Dim oPartVendor As Property
Dim oPartStatus As Property
Dim oPartArticleNo As Property
Dim oPartSurface As Property
Dim oPartSurfaceValue As Property
Dim oPartSpare As Property
Dim oPartWeld As Property
If TypeOf oCompDef Is VirtualComponentDefinition Then
Set oPartNumProperty = oCompDef.Document.PropertySets _
.Item("Design Tracking Properties").Item("Part Number")
Set oPartTitle = oCompDef.Document.PropertySets _
.Item("Inventor Summary Information").Item("Title")
Set oPartDescription = oCompDef.Document.PropertySets _
.Item("Design Tracking Properties").Item("Description")
Set oPartMaterial = oCompDef.Document.PropertySets _
.Item("Design Tracking Properties").Item("Material")
Set oPartVendor = oCompDef.Document.PropertySets _
.Item("Design Tracking Properties").Item("Vendor")
Set oPartStatus = oCompDef.Document.PropertySets _
.Item("Design Tracking Properties").Item("User Status")
On Error Resume Next
Set oPartArticleNo = oCompDef.Document.PropertySets _
.Item("Inventor User Defined Properties").Item("Article No")
On Error Resume Next
Set oPartSurface = oCompDef.Document.PropertySets _
.Item("Inventor User Defined Properties").Item("Surface Treatment")
On Error Resume Next
Set oPartSurfaceValue = oCompDef.Document.PropertySets _
.Item("Inventor User Defined Properties").Item("Surface Treatment Value")
On Error Resume Next
Set oPartSpare = oCompDef.Document.PropertySets _
.Item("Inventor User Defined Properties").Item("SpareWear")
On Error Resume Next
Set oPartWeld = oCompDef.Document.PropertySets _
.Item("Inventor User Defined Properties").Item("Weld Assembly")
xlws.Cells(oStartRow + b, 1) = oRow.ItemNumber
xlws.Cells(oStartRow + b, 2) = oPartNumProperty.Value
xlws.Cells(oStartRow + b, 3) = oRow.ItemQuantity
xlws.Cells(oStartRow + b, 4) = oPartArticleNo.Value
xlws.Cells(oStartRow + b, 5) = oPartTitle.Value
xlws.Cells(oStartRow + b, 6) = oPartDescription.Value
xlws.Cells(oStartRow + b, 7) = oPartMaterial.Value
xlws.Cells(oStartRow + b, 😎 = oPartSurface.Value
xlws.Cells(oStartRow + b, 9) = oPartSurfaceValue.Value
xlws.Cells(oStartRow + b, 10) = oPartVendor.Value
xlws.Cells(oStartRow + b, 11) = oPartSpare.Value
xlws.Cells(oStartRow + b, 12) = oPartStatus.Value
xlws.Cells(oStartRow + b, 13) = oPartWeld.Value
oStartRow = oStartRow + 1
Else
'Get the file property that contains the "Part Number"
'The file property is obtained from the parent
'document of the associated ComponentDefinition.
' write more stuff
Set oPartNumProperty = oCompDef.Document.PropertySets _
.Item("Design Tracking Properties").Item("Part Number")
Set oPartTitle = oCompDef.Document.PropertySets _
.Item("Inventor Summary Information").Item("Title")
Set oPartDescription = oCompDef.Document.PropertySets _
.Item("Design Tracking Properties").Item("Description")
Set oPartMaterial = oCompDef.Document.PropertySets _
.Item("Design Tracking Properties").Item("Material")
Set oPartVendor = oCompDef.Document.PropertySets _
.Item("Design Tracking Properties").Item("Vendor")
Set oPartStatus = oCompDef.Document.PropertySets _
.Item("Design Tracking Properties").Item("User Status")
On Error Resume Next
Set oPartArticleNo = oCompDef.Document.PropertySets _
.Item("Inventor User Defined Properties").Item("Article No")
On Error Resume Next
Set oPartSurface = oCompDef.Document.PropertySets _
.Item("Inventor User Defined Properties").Item("Surface Treatment")
On Error Resume Next
Set oPartSurfaceValue = oCompDef.Document.PropertySets _
.Item("Inventor User Defined Properties").Item("Surface Treatment Value")
On Error Resume Next
Set oPartSpare = oCompDef.Document.PropertySets _
.Item("Inventor User Defined Properties").Item("SpareWear")
On Error Resume Next
Set oPartWeld = oCompDef.Document.PropertySets _
.Item("Inventor User Defined Properties").Item("Weld Assembly")
xlws.Cells(oStartRow + b, 1) = oRow.ItemNumber
xlws.Cells(oStartRow + b, 2) = oPartNumProperty.Value
xlws.Cells(oStartRow + b, 3) = oRow.ItemQuantity
xlws.Cells(oStartRow + b, 4) = oPartArticleNo.Value
xlws.Cells(oStartRow + b, 5) = oPartTitle.Value
xlws.Cells(oStartRow + b, 6) = oPartDescription.Value
xlws.Cells(oStartRow + b, 7) = oPartMaterial.Value
xlws.Cells(oStartRow + b, 😎 = oPartSurface.Value
xlws.Cells(oStartRow + b, 9) = oPartSurfaceValue.Value
xlws.Cells(oStartRow + b, 10) = oPartVendor.Value
xlws.Cells(oStartRow + b, 11) = oPartSpare.Value
xlws.Cells(oStartRow + b, 12) = oPartStatus.Value
xlws.Cells(oStartRow + b, 13) = oPartWeld.Value
oStartRow = oStartRow + 1
Debug.Print Tab(ItemTab); oRow.ItemNumber; Tab(17); oRow.ItemQuantity; Tab(30); _
oPartNumProperty.Value; Tab(70); oDescripProperty.Value
'Recursively iterate child rows if present.
If Not oRow.ChildRows Is Nothing Then
Call QueryBOMRowProperties(oRow.ChildRows, ItemTab, xlApp, xlwb, xlws, oStartRow)
End If
End If
Next
ItemTab = ItemTab - 3
End Sub
The rule works but there are two problem I couldn't fix.
Problem 1:
Call oBOMView.Sort("Item", True)
This part should sort the BOM list according to Item number.
The output isn't sorted.
Any ideas why this isn't working?
Problem 2:
The property oPartArticleNo doesn't reset so it pops up in mutple lines.
I have tried to do a set after the export Set oPartArticleNo = "" this isn't working.
How should it be?