5 Replies Latest reply on Apr 20, 2016 8:31 AM by Tim Lewis

    Why won't the API create a BOM?

    Tim Lewis

      I'm using the code below to try to create a BOM using the API:


      Sub main()

          Dim swBOMTable As SldWorks.BomTableAnnotation, BOMTemplate As String

          BOMTemplate = "\\Server\path\to\bom-standard.sldbomtbt"

          Set swBOMTable = Application.SldWorks.ActiveDoc.Extension.InsertBomTable(BOMTemplate, 0, 0, _

                                                                                                     swBomType_e.swBomType_TopLevelOnly, "")

      End Sub


      However, when line #5 executes, the swBOMTable is empty and no BOM is created.  This is somewhat mystifying to say the least because this is essentially the code that was created with the macro recorder when I recorded myself adding a BOM to an open SW assembly.  It seems like this should be fairly straightforward.  I believe the above code above is the recommended means of creating a BOM with the API according to a couple different answers I've found (Example #1, Example #2).  I can't figure out what I'm doing differently to cause it not to work in my case.

        • Re: Why won't the API create a BOM?
          Kevin Chisholm

          You need to select a view and get selected view...


          You only need to change the BOMTemplate in this example and it should work:

          2015 SOLIDWORKS API Help - Insert BOM Table Example (VBA)

            • Re: Why won't the API create a BOM?
              Tim Lewis

              That is inserting a BOM into a drawing document (i.e. a .slddrw file).  I want to insert a BOM into an assembly file (i.e. a .sldasm file).  The code I included above uses this method: InsertBomTable.  I just realized that it is technically obsolete, but the current version (InsertBomTable2) does the same thing.  Additionally, the example given for using the current method throws an error on the line after the BOM was supposed to be created because the BOM failed to be created and swBOMAnnotation is empty.

                • Re: Why won't the API create a BOM?
                  Kevin Chisholm

                  This works for me... InsertBomTable3 exists also!


                  Option Explicit

                  Sub main()


                      Dim swApp                   As SldWorks.SldWorks

                      Dim swModel                 As SldWorks.ModelDoc2

                      Dim swModelDocExt           As SldWorks.ModelDocExtension

                      Dim swBOMAnnotation         As SldWorks.BomTableAnnotation


                      Dim BomType                 As Long

                      Dim Configuration           As String

                      Dim TemplateName            As String

                      Dim nErrors                 As Long

                      Dim nWarnings               As Long


                      Set swApp = Application.SldWorks

                     ' Open assembly document

                      Set swModel = swApp.ActiveDoc

                      Set swModelDocExt = swModel.Extension


                      ' Insert BOM table

                      TemplateName = "\\Server\path\to\bom-standard.sldbomtbt"

                      BomType = swBomType_TopLevelOnly 'For Top Level

                      'BomType = swBomType_PartsOnly 'For Parts Only

                      Configuration = ""


                      Set swBOMAnnotation = swModelDocExt.InsertBomTable3(TemplateName, 0, 0, BomType, Configuration, False, swNumberingType_Detailed, True)


                  End Sub

                  • Re: Why won't the API create a BOM?
                    John Alexander



                    This is just an anecdote, I can't see what is wrong with the code that you posted, but maybe it is useful information. The following is from a macro that I use for exporting BOMs to excel from assembly models.


                    Set swBomTable = swModel.Extension.InsertBomTable2(pathToBomTemplate, 0, 0, swBomType_e.swBomType_Indented, "Default", False)


                    I don't recall if passing an empty string argument for the configuration worked for me. This might be why I chose to use "Default" in that example; I would certainly prefer not to force the assembly to contain a specific named configuration in order for the macro to work.


                    Edit: Actually, in the link that you posted, the (unmarked) correct answer was that the OP had not specified a configuration.


                    Macro: Generate BOM, Export to Excel

                      • Re: Why won't the API create a BOM?
                        Tim Lewis

                        Evidently, that's correct.  Specifying the configuration creates the correct table.


                        I should note that there was initially a typo in my template path which is what was causing the table to be empty initially.  Correcting that, but leaving the configuration specified as an empty string results in an empty table being created.