Page 1 of 1
Set "Bill of Materials" table properties with VBA code
Posted: Sun Oct 27, 2024 6:06 pm
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?
Regards
Jaco
Re: Set "Bill of Materials" table properties with VBA code
Posted: Mon Oct 28, 2024 7:34 am
by RonE
Re: Set "Bill of Materials" table properties with VBA code
Posted: Mon Oct 28, 2024 7:19 pm
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
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"
Re: Set "Bill of Materials" table properties with VBA code
Posted: Tue Oct 29, 2024 8:10 am
by RonE
Try that instead of the failing line:
Dim swBomFeat As BomFeature
Set swBomFeat = swFeat.GetSpecificFeature2
Re: Set "Bill of Materials" table properties with VBA code
Posted: Tue Oct 29, 2024 5:43 pm
by Jacomuller
Hi
@RonE
Thanks, it did the job. I am a self taught VBA programmer. Hopefully I will get better at it soon
. I just need more reference material about the API
Regards
Jaco