4 Replies Latest reply on Mar 29, 2017 10:22 AM by Raymond Yau

    Macro - How to insert BOM into SLDASM file?

    Raymond Yau

      Hi

       

      I have multiple assembly files I want to generate BOMs for. Instead of creating the BOMs in a drawing file (which hasn't been created yet), I would like to generate a BOM within the assembly file instead using a macro. I can then use #Task to batch this macro and export all the BOMs into an excel file too.

       

      Below is a bit of code I customised to generate a BOM from an active assembly document.

       

      For some reason I'm getting '91' error highlighted in the screenshot below. What could be causing this?

       

      Any advice would be much appreciated.

       

      Option Explicit

       

      Sub main()

          Dim swApp                   As SldWorks.SldWorks

          Dim swModel                 As SldWorks.AssemblyDoc

          Dim swModelDocExt           As SldWorks.ModelDocExtension

          Dim swBOMAnnotation         As SldWorks.BomTableAnnotation

          Dim swBOMFeature            As SldWorks.BomFeature

          Dim swNote                  As SldWorks.Note

          Dim boolstatus              As Boolean

          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 = "C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\lang\english\bom-kelvin_test_template.sldbomtbt"

          BomType = swBomType_PartsOnly

          Configuration = "Default"

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

       

          Set swBOMFeature = swBOMAnnotation.BomFeature      'problem with this line*

        

          End Sub

       

       

      Sources used:

      2013 SOLIDWORKS API Help - Insert and Show BOM Table in Assembly Example (VBA)

        • Re: Macro - How to insert BOM into SLDASM file?
          Cad Admin

          Raymond,

           

          Macro works fine for me.  However I got it to throw the same error, by having "configuration" set wrong.  I ran it on a assembly that didn't have a "default" and it threw the error.  Worked fine on a assembly that has a "default" configuration.

           

          Also if the Template name or file location is wrong...same error.

           

          You need to add some checks on the variables to make sure they satisfy the macro...ie, Assembly must contain a "default" configuration or prompt & end macro...

            • Re: Macro - How to insert BOM into SLDASM file?
              Raymond Yau

              Hi,

               

              Thanks for your response, I can now understand why this doesn't work now. The configuration name in all my assembly files are not set as 'Default'. They all have custom specific names.

               

              I changed the 'Configurations line' so that it would grab the active configuration name, but its giving me another error. Any ideas why?

               

              New code below:

               

              Option Explicit

               

              Sub main()

               

                  Dim swApp                   As SldWorks.SldWorks

                  Dim swModel                 As SldWorks.AssemblyDoc

                  Dim swModelDocExt           As SldWorks.ModelDocExtension

                  Dim swBOMAnnotation         As SldWorks.BomTableAnnotation

                  Dim swBOMFeature            As SldWorks.BomFeature

                  Dim swNote                  As SldWorks.Note

                  Dim boolstatus              As Boolean

                  Dim BomType                 As Long

                  Dim Configuration           As String

                  Dim TemplateName            As String

                  Dim nErrors                 As Long

                  Dim nWarnings               As Long

                  Dim swConfig As SldWorks.Configuration

               

                  Set swApp = Application.SldWorks

               

                 ' Open assembly document

                  Set swModel = swApp.ActiveDoc

                  Set swModelDocExt = swModel.Extension

               

                  ' Insert BOM table

                  TemplateName = "C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\lang\english\bom-kelvin_test_template.sldbomtbt"

                  BomType = swBomType_PartsOnly

               

                  Set swConfig = swModel.GetActiveConfiguration 'New code not working*

               

                  Configuration = swConfig.Name 'New code not working*

               

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

                  Set swBOMFeature = swBOMAnnotation.BomFeature

                 

                  End Sub