9 Replies Latest reply on Jun 15, 2018 4:02 AM by Robert Voogt

    Check if description is present in BOM

    Robert Voogt

      Hi all,

       

      Is it possible to have a macro check if a BOM column is completely filled? 1 column has as property "Manuals description" but some parts are missing theirs.

      I would like the macro to check if a field in that column is empty and simply add the part number it to an existing excel file. This way, every time I open a drawing, it will grow the list of unnamed parts.

        • Re: Check if description is present in BOM
          Solid Air

          Yes that is possible

            • Re: Check if description is present in BOM
              Robert Voogt

              Will you point me in the right direction?

                • Re: Check if description is present in BOM
                  Solid Air

                  Below is code to display column in Immediate window in VBA editor window.  You must pre-select BOM in graphics window otherwise you will get a Type mismatch error.  You will need to set DescColumn variable to equal your description column (column A in BOM = 0).  You can then modify the code to create your excel list.

                   

                  Dim swApp As SldWorks.SldWorks
                  Dim swModel As SldWorks.ModelDoc2
                  Dim swSelMgr As SldWorks.SelectionMgr
                  Dim swTable As SldWorks.TableAnnotation
                  'Must pre-select table in graphics window
                  Sub main()
                      Dim TableRowCount As Long
                      Dim TableRow As Long
                      Dim DescColumn As Long
                      
                      Set swApp = Application.SldWorks
                      Set swModel = swApp.ActiveDoc
                      Set swSelMgr = swModel.SelectionManager
                      Set swTable = swSelMgr.GetSelectedObject6(1, -1)
                  
                      TableRowCount = swTable.RowCount
                  
                      DescColumn = 2 '(0=A, 1=B, etc.)
                  
                      For TableRow = 1 To TableRowCount  '0=header row
                  
                          Debug.Print swTable.Text2(TableRow, DescColumn, False)
                  
                      Next
                  
                  End Sub
                  
                    • Re: Check if description is present in BOM
                      Robert Voogt

                      I can get the tablerowcount to show the number of rows.

                       

                      How can I let it see if (for example) that item 4 has an empty description (column 3) and thus debug.print the field of the part number in column 1?

                        • Re: Check if description is present in BOM
                          Daniel Kuhn

                          Hi Robert,

                          you just need one "if" statement:

                           

                          Dim swApp As SldWorks.SldWorks
                          Dim swModel As SldWorks.ModelDoc2
                          Dim swSelMgr As SldWorks.SelectionMgr
                          Dim swTable As SldWorks.TableAnnotation
                          'Must pre-select table in graphics window
                          Sub main()
                              Dim TableRowCount As Integer
                              Dim TableRow As Integer
                              Dim descColumn As Integer
                              Dim partNocol As Integer
                               
                              Set swApp = Application.SldWorks
                              Set swModel = swApp.ActiveDoc
                              Set swSelMgr = swModel.SelectionManager
                              Set swTable = swSelMgr.GetSelectedObject6(1, -1)
                          
                              TableRowCount = swTable.RowCount
                             
                              ' Info: rows and culmns start with "0"
                              descColumn = 3 '(0=A, 1=B, etc.)
                              partNocol = 1
                          
                              For TableRow = 1 To TableRowCount - 1 '0=header row
                                 
                                  If swTable.Text2(TableRow, descColumn, False) = "" Then
                                      Debug.Print swTable.Text2(TableRow, partNocol, False)
                                  End If
                          
                              Next
                          
                          End Sub
                          

                           

                          Best regards

                          Daniel

                            • Re: Check if description is present in BOM
                              Robert Voogt

                              I get the correct tablerowcount, so I know it is linked to the correct bom.

                               

                              But I get an error here:

                               

                              If swTableAnn.Text2(TableRow, descColumn, False) = "" Then

                               

                              "Object doesn't support this property or method."

                               

                              (I renamed swTable to swTableAnn because I was using that dim already)

                              • Re: Check if description is present in BOM
                                Robert Voogt

                                When writing to a file I get Bad Mode error.

                                 

                                    TableRowCount = swTableAnn.RowCount
                                     
                                    ' Info: rows and culmns start with "0"
                                    descColumn = 3 '(0=A, 1=B, etc.)
                                    partNocol = 1
                                  
                                    For TableRow = 1 To TableRowCount - 1 '0=header row
                                        If swTableAnn.Text(TableRow, descColumn) = "" Then ' Text2 call is for SW2018: swTableAnn.Text2(TableRow, descColumn, False)
                                            Debug.Print swTableAnn.Text(TableRow, partNocol)
                                    
                                    
                                Dim fso As Object
                                
                                
                                Set fso = CreateObject("Scripting.FileSystemObject")
                                
                                
                                Dim oFile As Object
                                
                                
                                Set oFile = fso.OpenTextFile("D:\Spare Part Generator\Missing descriptions\Missing Descriptions.txt")
                                
                                
                                oFile.WriteLine "File name - " & swTableAnn.Text(TableRow, partNocol)
                                
                                
                                oFile.Close
                                
                                
                                Set fso = Nothing
                                
                                
                                Set oFile = Nothing
                                
                                
                                            End If
                                    Next
                                

                                 

                                Using this code:

                                Set oFile = fso.OpenTextFile("D:\Spare Part Generator\Missing descriptions\Missing Descriptions.txt", ForAppending, True)
                                

                                Gives an error on the "ForAppending"

                      • Re: Check if description is present in BOM
                        Deepak Gupta

                        Have you thought of doing this from atop level assembly rather than drawing?