Set "Bill of Materials" table properties with VBA code

Use this space to ask how to do whatever you're trying to use SolidWorks to do.
Jacomuller
Posts: 51
Joined: Wed Mar 01, 2023 6:55 pm
Answers: 2
x 36
x 5

Set "Bill of Materials" table properties with VBA code

Unread post by Jacomuller »

I am using VBA code to automatically generate shop floor papers. As part of the process I create a Bill of Material Table to list all parts for an assembly.

I have one part used in two different sub assemblies of the main assembly. The problem now is that this parts with the same part number, description and configuration, is listed twice with qty=1 in each. I needs to be listed once with qty=2.

I can rectify that by setting the "Part configuration Grouping" to "Display all configurations of the same part as one items". It does not seems logical but it does work. The question now is: How do I set this in VBA code?
image.png
Regards

Jaco
by RonE » Tue Oct 29, 2024 8:10 am
Try that instead of the failing line:
Dim swBomFeat As BomFeature
Set swBomFeat = swFeat.GetSpecificFeature2
Go to full post
User avatar
RonE
Posts: 32
Joined: Wed Nov 17, 2021 10:02 am
Answers: 4
Location: Germany
x 18
x 33

Re: Set "Bill of Materials" table properties with VBA code

Unread post by RonE »

PartConfigurationGrouping Property (IBomFeature)
-> https://help.solidworks.com/2024/englis ... uping.html
Jacomuller
Posts: 51
Joined: Wed Mar 01, 2023 6:55 pm
Answers: 2
x 36
x 5

Re: Set "Bill of Materials" table properties with VBA code

Unread post by Jacomuller »

Hi @RonE
Thanks for the message. I saw that before but I cannot get it to work. I have actually tried ChatGPT to help :D but I still cannot get it going. This is the code I got from ChatGPT:

Sub SetPartConfigurationGrouping()
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim swAssembly As AssemblyDoc
Dim swBOMTable As BomTableAnnotation
Dim configOption As Integer

' Connect to SolidWorks
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

' Check if the active document is an assembly
If swModel.GetType <> swDocASSEMBLY Then
MsgBox "Please open an assembly document."
Exit Sub
End If

Set swAssembly = swModel

' Find the first BOM table in the assembly
Dim swFeat As Feature
Set swFeat = swModel.FirstFeature

Do While Not swFeat Is Nothing
If swFeat.GetTypeName2 = "BomFeat" Then
Set swBOMTable = swFeat.GetSpecificFeature2
Exit Do
End If
Set swFeat = swFeat.GetNextFeature
Loop

' Check if BOM table is found
If swBOMTable Is Nothing Then
MsgBox "No BOM table found in the assembly."
Exit Sub
End If

' Set the Part Configuration Grouping option
' 0 = Display all configurations of the same part as separate items
' 1 = Display all configurations with the same part number as one item
configOption = 1 ' Change to 0 if you want to display each configuration separately
swBOMTable.PartConfigurationGrouping = configOption

' Update the BOM to reflect the changes
swBOMTable.Update

MsgBox "Part Configuration Grouping option set successfully."
End Sub
I get a error message "Run-time error'13': Type mismatch" at the line "Set swBOMTable = swFeat.GetSpecificFeature2"
User avatar
RonE
Posts: 32
Joined: Wed Nov 17, 2021 10:02 am
Answers: 4
Location: Germany
x 18
x 33

Re: Set "Bill of Materials" table properties with VBA code

Unread post by RonE »

Try that instead of the failing line:
Dim swBomFeat As BomFeature
Set swBomFeat = swFeat.GetSpecificFeature2
Jacomuller
Posts: 51
Joined: Wed Mar 01, 2023 6:55 pm
Answers: 2
x 36
x 5

Re: Set "Bill of Materials" table properties with VBA code

Unread post by Jacomuller »

Hi @RonE

Thanks, it did the job. I am a self taught VBA programmer. Hopefully I will get better at it soon :D. I just need more reference material about the API

Regards
Jaco
Post Reply