Hi @apl.coemter
For exporting the BOM of your currently Active Model State use the following code:
' Define the active document
Dim oDoc As Document
oDoc = ThisApplication.ActiveDocument
' Access the Bill of Materials and tweak ad lib
Dim oBOM As BOM
oBOM = oDoc.ComponentDefinition.BOM
oBOM.StructuredViewFirstLevelOnly = True
oBOM.StructuredViewEnabled = True
' Access BOM view structured
Dim oBOMView As BOMView
oBOMView = oBOM.BOMViews.Item("Structured")
' This gets name of the active model state
' It is for filtering later in the For Loop
Dim oColName As String
oColName = "QTY (" & oBOMView.ModelStateMemberName & ")"
' Export BOM to file
' Modelstate names can have symbols that file names would not allow
' Change to your liking
oBOMView.Export("BOM_XYZ.xls", kMicrosoftExcelFormat)
' This navigates to the new file
Dim oFile As String
oFile = ThisDoc.Path & "\" & "BOM_XYZ.xls"
' Define Excel objects
Dim excelApp As Object = CreateObject("Excel.Application")
excelApp.Visible = False
excelApp.DisplayAlerts = False
' Checks if file exists
If Dir(oFile) <> "" Then
Dim oWB As Object = excelApp.Workbooks.Open(oFile)
Dim oWS As Object = oWB.Worksheets(1)
' Loop through columns in reverse
' That is because the active model state might not be the first
' If you deleted collumns before the active comes up
' The indexing would not work correctly
' This mitigates that problem
For iCol = oWS.UsedRange.Columns.Count To 1 Step -1
Dim oCellValue As String = oWS.Cells(1, iCol).Value
If oCellValue Is Nothing Or oCellValue = "" Then Continue For
' Here we make use of the Active Model state name created earlier
If oCellValue.Contains(oColName) Then
' This gives the collumn of the quantities in the Active model state
' the name you want, in my case QTY
oWS.Cells(1, iCol).Value = "QTY"
' This next segment deletes all the other model state quantities
ElseIf oCellValue.Contains("QTY") Then
oWS.Columns(iCol).EntireColumn.Delete
End If
Next
' Autofit columns and save
oWS.Columns.AutoFit()
oWB.Save()
oWB.Close()
End If
excelApp.Quit()
excelApp = Nothing
I got inspired by the approach of @Curtis_Waguespack who was filtering the columns and used knowledge of @WCrihfield regarding BOMView.ModelStateMemberName from the following post:
Solved: How can I restrict iLogic BoM Export to a particular Model State? Now I get data from all of...
This code is a simplified part of a bigger code as I like to export in batches of various formats in a separate folder for further processing.
Model States are great, have awesome functionalities for generating configurations via spreadsheets and with a bit of coding you can generate all .idws, .pdfs and other formats with just one click of a button when everything is set up nicely.
@llorden4 Please take a look at the code above, it gives the result you would normally get through manual export of BOM.