Ryan Foss

Create and Save BOM Table as xls

Discussion created by Ryan Foss on Jul 31, 2020
Latest reply on Aug 4, 2020 by Artem Taturevych

I have been working on a VBA macro to Generate a BOM table in a drawing and then Save it out to an .xls.


I am not a coder but have gotten close through internet searches.


The missing piece in the middle of the code seems to be that I need to programatically Select the BOM Table to activate the PropertyManager.   


Please Help


Im running SW2019.


Macro code below:








Dim swApp As SldWorks.SldWorks

Dim swModel As SldWorks.ModelDoc2

Dim swSelMgr As SldWorks.SelectionMgr

Dim swFeatMgr As SldWorks.FeatureManager

Dim swView As SldWorks.View

Dim swBomAnn As BomTableAnnotation

Dim swBomFeat As SldWorks.BomFeature

Dim AnchorType As Long

Dim BomType As Long

Dim Configuration As String

Dim TableTemplate As String

Dim Names As Variant

Dim Visible As Variant

Dim boolstatus As Boolean


Dim swSM As ISelectionMgr

Dim swTable As SldWorks.ITableAnnotation

Dim Status As Boolean


Option Explicit


Sub main()


'Place BOM on Sheet

    Set swApp = Application.SldWorks

    swApp.Visible = True

    Set swModel = swApp.ActiveDoc

    Set swSelMgr = swModel.SelectionManager

    Set swFeatMgr = swModel.FeatureManager

    ' Get selected drawing view

    Set swView = swModel.GetFirstView  ' swSelMgr.GetSelectedObject6(1, 0)

    Set swView = swView.GetNextView

    AnchorType = swBOMConfigurationAnchor_TopLeft ' swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_BottomLeft

    BomType = swBomType_e.swBomType_TopLevelOnly

    Configuration = ""

    TableTemplate = "C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS (2)\lang\english\bom-standard.sldbomtbt"

    ' Insert BOM table

    Set swBomAnn = swView.InsertBomTable2(True, 0.4, 0.3, AnchorType, BomType, Configuration, TableTemplate)

    swModel.ClearSelection2 True

    ' Because BOM type is swBomType_TopLevelOnly,

    ' then work with BomFeature to get and set configurations

    Set swBomFeat = swBomAnn.BomFeature

    Names = swBomFeat.GetConfigurations(False, Visible)

    Visible(0) = True

    boolstatus = swBomFeat.SetConfigurations(True, Visible, Names)

    ' Update FeatureManager design tree







'Click the move-table icon in the upper-left corner of the BOM table to open the table's PropertyManager page.





'save BOM Table to xls

    Set swModel = swApp.ActiveDoc

    'Dim swSM As ISelectionMgr

    Set swSM = swModel.SelectionManager

    Set swTable = swSM.GetSelectedObject6(1, -1)

    swModel.ClearSelection2 (True)


    Dim swSpecTable As IBomTableAnnotation

    Set swSpecTable = swTable


    ' Save the selected BOM table to Microsoft Excel, including hidden cells

    ' and images

    Status = swSpecTable.SaveAsExcel("c:\temp\BOMTable.xls", False, False)


End Sub