10 Replies Latest reply on Jan 18, 2017 11:23 AM by David Mandl

    Help with API Documentation / VBA Macros

    Ken Hayes

      Can someone point me to resources that more clearly explain the methods / functions to use VBA to read Assembly BOMs, get the Item list, then get the BOM data, including Item ID, Description, Cost (if available), Bom relationship (parent assembly and child), quantity per piece, Revision of assembly, and Item Number on drawing.  I know there are several methods like iComponent2, iModel2, etc.  Just trying to understand how to navigate the SolidWorks utilities for VBA to extract to text.  Thanks!

        • Re: Help with API Documentation / VBA Macros
          Deepak Gupta

          Check the macros by Keith Rice on his API training/consulting site: SolidWorks API Video Tutorials and I'm sure you would the one that suits the needs.

          • Re: Help with API Documentation / VBA Macros
            Scott Stuart

            You can read the contents of the BomTableAnnotation with BomTableAnnotation.Text(row, column). Once you get the BomTableAnnothation object you can loop through the rows and columns and get whatever info you want (use BomTableAnnotation.RowCount and .ColumnCount to get the number of rows and columns). I would recommend inserting a new table with a known template and not read an existing table since it might not have been created with the template you think it was.

              • Re: Help with API Documentation / VBA Macros
                Ken Hayes

                So, I would combine reading the SldWorks.Component2 object to drop through each lower level structure, then within each sub-assembly, use the BomTableAnnotation.Text(row, column) to pull the data?


                Thanks, I think this helps quite a bit!

                  • Re: Help with API Documentation / VBA Macros
                    Scott Stuart

                    If you use an indented BOM there should be no need to do anything with the components. All the component and sub-component info will be in the top level BOM.

                      • Re: Help with API Documentation / VBA Macros
                        Ken Hayes

                        Thanks, I have found many examples that appear to get me started, but I am consistently running into the Error 13 "Type Mismatch" error.

                        I am starting from a SLDASM file with an indented BOM table.  This is a shorter example, with the line generating the error highlighted:


                        'Loops though tables on active sheet to find BOM

                        'Prints text to Immediate window


                        Dim swApp As SldWorks.SldWorks

                        Dim DrawDoc As SldWorks.DrawingDoc

                        Dim TableAnn As SldWorks.TableAnnotation

                        Dim BOM As SldWorks.BomTableAnnotation '.TableAnnotation ' This can be either type

                        Dim View As SldWorks.View


                        Sub main()


                        Set swApp = Application.SldWorks

                        Set DrawDoc = swApp.ActiveDoc  <-----------THIS IS THE ERROR LINE

                        Set View = DrawDoc.GetFirstView ' this is the sheet

                        Set TableAnn = View.GetFirstTableAnnotation



                        Do While Not TableAnn Is Nothing

                            If TableAnn.Type = 2 Then '2 is the bom table type

                                Set BOM = TableAnn

                                Exit Do

                            End If

                            Set TableAnn = TableAnn.GetNext




                        If BOM Is Nothing Then

                            Debug.Print ("BOM was not found")


                            Dim Txt As String

                            Dim Row As Long

                            Dim Col As Long

                            For Row = 0 To BOM.RowCount - 1

                            Txt = ""

                                For Col = 0 To BOM.ColumnCount - 1

                                    Txt = Txt & BOM.Text(Row, Col) & vbTab

                                Next Col

                                Debug.Print Txt

                            Next Row

                        End If

                        Debug.Print "Done"

                        End Sub


                        Virtually every sample I try gives me this error in basically the same place.  Any help would be appreciated.

                          • Re: Help with API Documentation / VBA Macros
                            Simon Turner

                            "I am starting from a SLDASM file" is inconsistent with the line "Dim DrawDoc As SldWorks.DrawingDoc"

                            If it's an assembly, you need an AssemblyDoc object (or a ModelDoc2 object).

                              • Re: Help with API Documentation / VBA Macros
                                Ken Hayes

                                Thank you all, everything mentioned has helped move me along.  I really appreciate it.  A little background on my issue, that I dive deeper into SW might be making my task more difficult.

                                We are an ERP developer, and have signed up as a SolidWorks Research Partner.  My task is to look into writing an interface between SW and my ERP to bring in Item lists and BOMS from SW.


                                I am now finding the different file types involved with SW, and using the Food Processor SLDASM as an example, began to work from there asking these questions.  Come to finf out, a couplf of our customers actually use SLDDWG Drawing file types. and I am struggling to understand the relationship between the different file types, as well as any interactivity between them - ASMs, DWGs, Parts, Components.


                                I am a 'database guy' - if there were consistent file structures with a single schema that held this data, I'm your guy. 


                                I guess at this point, I think my only, next (and hopefully final) question here is where would someone like me (lots of experience with Database and VBA) begin for some primer or tutorial of the file structures within SW, their interoperability, and what methods are available for each.


                                Again, I appreciate your help, it helped me make some great strides and produce the basis of some usable output, but I need to step back to the basic of SW from here.


                                Ken Hayes

                                  • Re: Help with API Documentation / VBA Macros
                                    Scott Stuart

                                    I would suggest you run through a few basic tutorials on using solidworks. You don't need to be a designer to follow the tutorials, but if you went through the process of designing a couple parts, making an assembly from them, then making a drawing of the assembly that includes a bill of materials, then you would get a better grasp of the fundamentals. You'll get a much better understanding than you would from just muddling through the API help. You can find tutorials in the Solidworks help menu, at my.solidworks.com, on youtube, and various other places.


                                    But, in the long run you are going to need an even more in-depth understanding. Aside from the basic part/assembly/drawing file structure, you're going to need to understand more advanced topics like configurations, multi-body parts and weldments if you are going to develop a good ERP integration add-in.