Export BOM to Excel with a VBA Macro

Hi all.

I got a VBA program to export my bill of materials in a .xls sheet

Everything goes well except for one option that I can't change.

I would like the program to choose the current configuration.

Currently it is looking for the configuration named "Default"

Configuration = "Default"

If anyone knows the answer:)

 

****************************

Insert BOM Table and Extract Data Example (VBA)
This example shows how to insert a BOM table and the extract the data from it.

'-------------------------------------------------------
' Preconditions:
' 1. Verify that the specified drawing document to open
' exists.
' 2. Open the Immediate window.
'
' Postconditions:
' 1. Opens the specified drawing document.
' 2. Selects a drawing view.
' 3. Inserts a BOM table at the point where the drawing
' was selected.
' 4. Examine the drawing and Immediate window.
'
' NOTE: Because the drawing is used elsewhere, do not
' Save changes.
'-------------------------------------------------------

Option Explicit

Sub main()

Dim xlApp As Excel.Application
Set xlApp = New Excel.Application
Dim wbk As Excel.Workbook
Dim sht As Excel.Worksheet

With xlApp
. Visible = True
Set wbk = . Workbooks.Add
Set sht = wbk. ActiveSheet
End With

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swBOMAnnotation As SldWorks.BomTableAnnotation
Dim swBOMFeature As SldWorks.BomFeature
Dim boolstatus As Boolean
Dim BomType As Long
Dim Configuration As String
Dim TemplateName As String

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModelDocExt = swModel.Extension

TemplateName = "M:\DATABASE\TEMPLATES\05-Model of nomenclature\GP_ASM_Nomenclature BOS.sldbomtbt"
BomType = swBomType_Indented
Configuration = "Default"
Set swBOMAnnotation = swModelDocExt.InsertBomTable3(TemplateName, 0, 0, BomType, Configuration, False, swNumberingType_Detailed, True)
Set swBOMFeature = swBOMAnnotation.BomFeature

swModel.ForceRebuild3 True

Dim NumCol As Long
Dim NumRow As Long
Dim I As Long
Sun J As Long

NumCol = swBOMAnnotation.ColumnCount
NumRow = swBOMAnnotation.RowCount

For I = 0 To NumRow
For J = 0 TB NumCol
Sht. Cells(I + 1, J + 1). Value = swBOMAnnotation.Text(I, J)
Next J
Next I

boolstatus = swModelDocExt.SelectByID2(swBOMFeature.GetFeature.Description, "BOMFEATURE", 0, 0, 0, True, 0, Nothing, 0)
swModel.EditDelete

swModel.ForceRebuild3 True

Dim config As SldWorks.Configuration
Dim cusPropMgr As SldWorks.CustomPropertyManager
Dim lRetVal As Long
Dim ValOut As String
Dim ResolvedValOut As String
Dim wasResolved As Boolean
Dim nNbrProps As Long
Dim vPropNames As Variant
Dim vPropTypes As Variant
Dim vPropValues As Variant
Dim resolved As Variant
Dim custPropType As Long
Dim K As Long
Dim NameProperty As String

Set config = swModel.GetActiveConfiguration
Set cusPropMgr = config. CustomPropertyManager

nNbrProps = cusPropMgr.Count
vPropNames = cusPropMgr.GetNames
For K = 0 To nNbrProps - 1
cusPropMgr.Get2 vPropNames(K), ValOut, ResolvedValOut
custPropType = cusPropMgr.GetType2(vPropNames(K))
If vPropNames(K) = "CARTOONIST" Then
PropertyName = ResolvedValOut
End If
Next K

Dim Path As String
path = "C:\temp\BOS.xlsx"

With xlApp
wbk. SaveAs path
wbk. Close
. Quit
End With

End Sub


insert-bom-asm_-_03.swp

 Hello dlebrun_1,

Obviously, your code has two big blocks.

The first (lines 22 to 53) creates the nomenclature for a given configuration; under Excel proper. The second one (lines 55 to 100) gets the name of the cartoonist in the active configuration (but does not do anything with it, which is peculiar).

The first block, unlike the second, does not use the active configuration but the one called "Default". This is line 49: Configuration = "Default". It is then better to define the variable "Configuration" which contains the name of the configuration by calling the configuration object. I then suggest that you replace this line by taking the example of the following method (link to the Solidworks 2019 help page):

Sub essai_8946585465656()
    
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim Configuration As String
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    Configuration = swApp.GetActiveConfigurationName(swModel.GetPathName)
    MsgBox Configuration
    
End Sub

 

1 Like

Hello

"The second one (line 55 to 100) gets the name of the cartoonist in the active configuration (but doesn't do anything with it, which is peculiar)": and yes it's beautiful!!! I recognize in this code a part of a macro that I made following a particular request but which was obviously taken out of context and reworked by someone who didn't really understand what he was doing. To meet the current need, it is possible to delete a good number of rows and it is indeed enough to hang on to the active configuration using the "GetActiveConfigurationName" function as suggested by mgauroy.

Kind regards

1 Like

mgauroy 

Thank you for your help. I followed your advice, now everything works.

D.Roger

I got this program from the Forum and I modified it quickly without completely purging it. Unfortunately, I'm not very good at VBA as you have noticed;) 

Here is the result after modification:

Option Explicit

Sub main()

Dim xlApp As Excel.Application
Set xlApp = New Excel.Application
Dim wbk As Excel.Workbook
Dim sht As Excel.Worksheet

With xlApp
    . Visible = True
    Set wbk = . Workbooks.Add
    Set sht = wbk. ActiveSheet
End With

Dim swApp                   As SldWorks.SldWorks
Dim swModel                 As SldWorks.ModelDoc2
Dim swModelDocExt           As SldWorks.ModelDocExtension
Dim swBOMAnnotation         As SldWorks.BomTableAnnotation
Dim swBOMFeature            As SldWorks.BomFeature
Dim boolstatus              As Boolean
Dim BomType                 As Long
Dim Configuration           As String
Dim TemplateName            As String

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModelDocExt = swModel.Extension

TemplateName = "M:\DATABASE\TEMPLATES\05-Model of nomenclature\GP_ASM_Nomenclature BOS.sldbomtbt"
BomType = swBomType_Indented
Configuration = swApp.GetActiveConfigurationName(swModel.GetPathName)
MsgBox Configuration
Set swBOMAnnotation = swModelDocExt.InsertBomTable3(TemplateName, 0, 0, BomType, Configuration, False, swNumberingType_Detailed, True)
Set swBOMFeature = swBOMAnnotation.BomFeature

swModel.ForceRebuild3 True

Dim NumCol As Long
Dim NumRow As Long
Dim I As Long
Sun J As Long

NumCol = swBOMAnnotation.ColumnCount
NumRow = swBOMAnnotation.RowCount

For I = 0 To NumRow
    For J = 0 TB NumCol
        Sht. Cells(I + 1, J + 1). Value = swBOMAnnotation.Text(I, J)
    Next J
Next I

boolstatus = swModelDocExt.SelectByID2(swBOMFeature.GetFeature.Description, "BOMFEATURE", 0, 0, 0, True, 0, Nothing, 0)
swModel.EditDelete

Dim Path As String
path = "C:\temp\BOS.xlsx"

With xlApp
    wbk. SaveAs path
    wbk. Close
    . Quit
End With

End Sub

1 Like

Hello

I would also be interested in discovering the possibilities of this macro but I am stuck from the 1st line: "type defined not defined".

This macro can be launched from SolidWorks, not from Excel 365.

Thank you for your feedback, have a good day


pb_macro.png

Open your macro with the editor and check that you have added the Microsoft Excel reference in the References Tool menu:

 

And also, in general it is better to open a new topic with the link than to dig up an old one

2 Likes

Thank you very much, it works! I'll be able to see what can be done with it in SW or Excel.

It seemed to me that my question was in the continuity of the subject, which is why I did not open a new subject in its own right.

Thanks again

1 Like

Hello, I tried the macro but I get errors.

How to introduce this macro in solidworks, are there any changes to be made,

Thank you for your help

Hello

What kind of errors? a screenshot of the messages would be not bad ...

A definite change to make is to change the path of the BOM template (TemplateName)

Kind regards

2 Likes

Hello everyone, I'm coming 2/3 years later I know :grin:
I'm almost in the same situation so these Q&A served me well!

A small peculiarity of my problem is that I also have to export many custom properties such as the name of the engineer, the part number or the plan number...
If I understood correctly it depends on the template called, so how do you modify such a template? Or is there another way to do it?

Thanks in advance

The VBA program calls a SolidWorks BOM template file. Customize this template with the info you want to add to it.

2 Likes

It works thanks, but the problem is that I don't know how to modify such a file (.sldbomtbt). Would you know how to do it?

Hello
Just edit a table by adding the columns you want, then click on the cross that appears to move the table and then select save as.
By default it will save as a BOM template.

2 Likes

Thank you very much, everything works as I wanted it to!

Hello

Regarding your main need to export your BOMs to Excel, we have developed a tool integrated into SOLIDWORKS that allows you to create a BOM from an assembly or a folder and use the properties as an extraction filter, choose the extraction level of the sub-assemblies, concatenate several properties in a column.

It is also possible to customize the export in text, Excel, SOLIDWORKS table, PDF or XML formats.

This tool is SmartBom: SmartBom

Kind regards

Hello

I tried this macro to see.

The macro works if I launch it from an assembly but not from a drawing.
This may be its normal functioning.

It would be possible to have a variant of this one to retrieve the BOM that is on a drawing (I add data in a column of my BOM and it's more practical on the MEP than in the assembly)??

Would it also be possible to create the Excel file from an existing template and save it under another name? (to have other pre-existing sheets in the Excel and possibly put other macros there too)

Thank you in advance

Hello
I think everything is doable, just need to compile SW and Excel code.
I think I've already answered this type of question on another subject, I'd have to do some research.