3 Replies Latest reply on Oct 27, 2015 10:15 AM by Amen Allah Jlili

    Save BOM

    Hrituc Alexandru


      I need a macro buton to save the bom (.xls) to a specific location...


        • Re: Save BOM
          Amen Allah Jlili

          Do you need the macro that saves a bom as xls or do you want to know how to add the macro button to a macro that you have?

          This might help:

          2013 SOLIDWORKS API Help - Assign SolidWorks Macro to Button

          • Re: Save BOM
            Amen Allah Jlili

            There are many macros out there that save BOM tables as an Excel file. You can do that by right clicking on the bom and choosing save as which what I would recommend.


            The main problem with macros that try to achieve what you want is:


            1. The Save As dialog as SolidWorks is not equipped with a dialog box that returns the filename path (Unlike Excel). You can use Win32 Save As File Save As Dialog or you can use someone's patented Save As Dialog.

            2. AnnotationTable interface does not have a specific method to save as Excel file. The only method I found is .SAVEASTXT which can use tab as a separator (It's more fit for  but again when you save the file using this method as an xls, Excel would still prompt a message saying that this file was created as text file.



            Here's a macro code I wrote that uses MICROSOFT EXCEL OBJECTS 12.0 (ADD THE REFERENCE FROM TOOLS > REFERENCES):



            Dim swApp As Object

            Dim swModel As SldWorks.ModelDoc2

            Dim swDrawingDoc As SldWorks.DrawingDoc

            Dim swSelectionMgr As SldWorks.SelectionMgr

            Dim swAnnotationTable As SldWorks.TableAnnotation

            Dim FilePath As String

            Dim ExcelApp As Excel.Application

            Dim Worksheet As Excel.Worksheet

            Dim Workbook As Excel.Workbook





            Sub main()

            Set swApp = Application.SldWorks

            Set swModel = swApp.ActiveDoc

            If swModel.GetType <> SwConst.swDocDRAWING Then

            swApp.SendMsgToUser ("No drawing doc in the active window")

            Exit Sub


            Set swSelectionMgr = swModel.SelectionManager

            Set swAnnotationTable = swSelectionMgr.GetSelectedObject(1)

            On Error GoTo Handler:

            Set ExcelApp = New Excel.Application



            Set Workbook = ExcelApp.Workbooks.Add()

            Set Worksheet = Workbook.Worksheets(1)

            For i = 0 To swAnnotationTable.RowCount - 1

            For j = 0 To swAnnotationTable.ColumnCount - 1

            Worksheet.Cells(i + 1, j + 1).Value = swAnnotationTable.DisplayedText(i, j)

            Next j

            Next i


            Dim FilePath As String:

            ExcelApp.Visible = True

            FilePath = ExcelApp.GetSaveAsFilename(fileFilter:="Excel files (*.xls), *.xls)")

            ExcelApp.Visible = False

            If FilePath <> "False" Then ' ExcelApp.GetSaveAsFileName returns false if file dialog cancel button is clicked

            Workbook.SaveAs (FilePath)

            End If

            Workbook.Close (False)


            End If

            Exit Sub


            swApp.SendMsgToUser (Err.Description)

            End Sub