Export a BoM automatically, periodically to an excel file

I'm trying to export a BoM automatically, periodically from an assembly or drawing into an excel file. The BoM will not be present in the drawing so the properties cannot be grabbed straight from a BoM there.

My current plan is as follows: A VBA macro will be able to export a special BoM whose properties and rows/columns are defined by the VBA macro. The Assembly or .dwg file (that the BoM comes from) will be opened by a Visual Basic Express program and the VBA macro will be commanded to run with this same VBE program. The Visual Basic Express program will be run on a timer from task scheduler so that it is run every night and lunchtime.

I have code that will take the assembly's properties and drop them into an excel file. I need the program to grab the part properties as well as the total number of each part in the assembly and drop those in the excel file instead. Also, I need to make a Visual Basic Express program that will find the correct file, open it, and run the VBA macro. I am very new to all of this, so any help or advise would be very much appreciated!! Thanks!

Thomas Brooks

SolidworksApi macros
Answers 8
SF
SF 2012-06-26 Best Answer

Hi Thomas,

For part which property u want to grab ..i mean it is a custom properties or something else ...and for timer u have to create system services...please eleborate ur problems...

Thanks

SF
SF 2012-06-26

I want to grab 10 custom properties from all of the parts inside of an assembly. Thank you for your help!

SF
SF 2012-06-26

If there is a way to scroll through the parts in the assembly, I could run a loop that selects a part and grabs the properties from that part, then selects the next part and grabs the properties from that part and so on, but I don't know how to move through the parts in an assembly. Also, I would need to include a searcher and counter that searches for duplicates and counts them so that I get a quantity of each part in the assembly.

DG
DG 2012-06-26
SF
SF 2012-06-26

This looks like exactly what I want, but I am new to this language and I can't get it to work. Here are the errors that I'm getting...

comment imagecomment image
SF
SF 2012-06-26

Another solution for the .vb portion of this "system of programs" would be to modify this code to select a specific BoM:

' Author: Luke Malpass

' Website: http://www.angelsix.com

' Date: 01/04/08

'

' Prerequisits: Have a drawing open, and for it to export anything, have at least one table created (such as a BOM)

'

'

Option Explicit

Dim swApp                   As SldWorks.SldWorks

Dim swModel                 As SldWorks.ModelDoc2

Dim swDraw                  As SldWorks.DrawingDoc

Dim swView                  As SldWorks.View

Sub main()

    Dim swTable                 As SldWorks.TableAnnotation

    Dim bRet                    As Boolean

    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    Set swDraw = swModel

   

    ' Get the first view

    Set swView = swDraw.GetFirstView

    Do While Not swView Is Nothing

        ' Show the name of the view

        Debug.Print "  " & swView.Name

       

        ' Get the first table annotation for this view

        Set swTable = swView.GetFirstTableAnnotation

        Do While Not swTable Is Nothing

            ' Use built in function to manually export BOM to text file, but will open fine as XLS with warning

            ' that is is a text file. This function has a few drawbacks...

            swTable.SaveAsText "c:\Standard BOM.xls", vbTab

            ' So create our own manual method instead

            ExportTable swTable

           

            ' Get next table annotation for this view

            Set swTable = swTable.GetNext

        Loop

       

        Set swView = swView.GetNextView

    Loop

End Sub

Sub ExportTable(swTable As SldWorks.TableAnnotation)

    Dim swAnn                   As SldWorks.Annotation

    Dim nNumCol                 As Long

    Dim nNumRow                 As Long

    Dim sRowStr                 As String

    Dim i                       As Long

    Dim j                       As Long

   

    Dim objExcelObject          As Excel.Application

    Dim objBook1                As Excel.Workbook

    Dim objSheet1               As Excel.Worksheet

   

    Set objExcelObject = CreateObject("Excel.Application")

    objExcelObject.Visible = False

    objExcelObject.ScreenUpdating = False

    objExcelObject.Interactive = False

   

    Set objBook1 = objExcelObject.Workbooks.Add()

    Set objSheet1 = objBook1.ActiveSheet

       

    Set swAnn = swTable.GetAnnotation

   

    objBook1.SaveAs "c:\" & swAnn.GetName

    objSheet1.Activate

    'Set worksheet name

    objSheet1.Name = "BOM"

    nNumCol = swTable.ColumnCount

    nNumRow = swTable.RowCount

    ' Only export Bills of Materials (EDIT, OR REMOVE TO PROCESS ALL TABLES)

    If swTable.Type <> SwConst.swTableAnnotationType_e.swTableAnnotation_BillOfMaterials Then Exit Sub

   

    ' Get the table contents

    For i = 0 To nNumRow - 1

        For j = 0 To nNumCol - 1

            objSheet1.Range("A1").Offset(i, j).Value = swTable.DisplayedText(i, j)

            ' Bold top column

            If i = 0 Then objSheet1.Range("A1").Offset(i, j).Font.Bold = True

        Next j

    Next i

       

    objSheet1.Range("A1").Activate

    objSheet1.Range("A1", objSheet1.Range("A1").SpecialCells(xlCellTypeLastCell)).Columns.AutoFit

       

    ' Clean-up

    objExcelObject.ScreenUpdating = True

    objExcelObject.Interactive = True

   

    objExcelObject.ActiveWorkbook.Save

    objExcelObject.Workbooks.Close

    objExcelObject.Quit

   

    Set objSheet1 = Nothing

    Set objBook1 = Nothing

    Set objExcelObject = Nothing

   

   

End Sub

SF
SF 2012-06-27

Hi Thomas,

If u only want to grab only custom properties from assembly and from part then better use document manager API..through this u can get any custom properties from part and from assembly also..without open that..so it will be too fast...i am using this API..

SF
SF 2012-07-02

Is there a way to get the custom properties from all of the parts inside of an assembly so that if someone adds or removes a part from the assembly, the parts that I get the properties from changes too?