Reordering Assembly Structured BOM by specified order VBA macro

Reordering Assembly Structured BOM by specified order VBA macro

patrick_dallinger
Explorer Explorer
193 Views
1 Reply
Message 1 of 2

Reordering Assembly Structured BOM by specified order VBA macro

patrick_dallinger
Explorer
Explorer

Hello everyone,

 

ive been struggling the last couple of days to get this to work.

Basically the goal is:

 

Assembly with multiple parts in it, every part has a user defined property "PartNumer"

Now Sort the Assembly Structured BOM by a previusly defined String Array So like

 

String Array: Index-PartNumber: 1-"F890", 2-"J234", 3-"G242", 4-"R454"

Unsorted AssemblyBOM: BomNumber-PartNumber: 65-"G242", 12-"R454", 23-"F890", 12-"J234"

 

Sorted AssemblyBOM = String Array

 

Using the BOMView.Renumber Method you can sort the whole bom which doesnt help me in this case.

So i thought id just pass in a single Row into BOMRowsToRenumber

so that it basically assings the correct index from the Array to the correct Part 

Which i have been trying out today but every time i change the code to fix errors / compile errors some other error pops up

 

Hope someone can help me out on this, Thanks in advance! ^^

 

(Inventor 2024/2025)

0 Likes
194 Views
1 Reply
Reply (1)
Message 2 of 2

mateusz_baczewski
Advocate
Advocate

@patrick_dallinger 

Hi,

 

I'm sending you the code that changes the order in the BOM based on a list. I hope you understand how it works. The code was written using test data.

You definitely need to check the names of oBom.BOMViews.Item("Structured") and oBomStructured.Sort("Position"), as I have a different language version than English. "Position" is the first column in the BOM.

 

 

 

Sub main
	
	Dim oDoc As AssemblyDocument = ThisDoc.Document
	Dim oDocCompDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
	
	Dim oBom As BOM = oDocCompDef.BOM
	oBom.StructuredViewEnabled = True
	oBom.PartsOnlyViewEnabled = True
	
	Dim stringArray As String() = {"Test1", "Test3", "Test2" }

	Dim oBomStructured As BOMView = oBom.BOMViews.Item("Structured")
	Dim iteration As Integer = 1
	
	For Each element In stringArray
		
		For Each bomRow1 As BOMRow In oBomStructured.BOMRows
			If bomRow1.ComponentOccurrences.Item(1).Name.Contains(element)
				bomRow1.ItemNumber = iteration
			End If
		Next
		iteration+=1
	Next
	
	
	oBomStructured.Sort("Position")
	
End Sub

 

 

If you found it helpful, a "Like" would be much appreciated!
If this post solved your problem, please mark it as "Solution.".