Print Bom macro (but can someone help me sort by part number)

Print Bom macro (but can someone help me sort by part number)

mcgyvr
Consultant Consultant
738 Views
7 Replies
Message 1 of 8

Print Bom macro (but can someone help me sort by part number)

mcgyvr
Consultant
Consultant
an ADSK employee was nice enough to post this macro to print the bill of materials, but I want it to output the parts sorted by part number. Can someone help to write the necessary coding. Im not a programmer at all.
Thanks in advance.


Attribute VB_Name = "Module1"
Public Sub BOMPrint()

' Set a reference to the assembly document.
' This assumes an assembly document is active.
Dim oDoc As AssemblyDocument
Set oDoc = ThisApplication.ActiveDocument

Dim FirstLevelOnly As Boolean
If MsgBox("First level only?", vbYesNo) = vbYes Then
FirstLevelOnly = True
Else
FirstLevelOnly = False
End If

' Set a reference to the BOM
Dim oBOM As BOM
Set oBOM = oDoc.ComponentDefinition.BOM

' Set whether first level only or all levels.
If FirstLevelOnly Then
oBOM.StructuredViewFirstLevelOnly = True
Else
oBOM.StructuredViewFirstLevelOnly = False
End If

' Make sure that the structured view is enabled.
oBOM.StructuredViewEnabled = True

'Set a reference to the "Structured" BOMView
Dim oBOMView As BOMView
Set oBOMView = oBOM.BOMViews.Item("Structured")
Dim fname As String
fname = "c:\temp\BOM.txt"

Open fname For Output As #1 ' Open file for output.

Print #1, "Item"; Tab(10); "QTY"; Tab(20); "Part Number"; Tab(35); "Description"
Print #1, "------------------------------------------------------------------------"

'Initialize the tab for ItemNumber
Dim ItemTab As Long
ItemTab = -3

Call QueryBOMRowProperties(oBOMView.BOMRows, ItemTab)

Close #1

Call print_via_notepad(fname)
End Sub

Private Sub QueryBOMRowProperties(oBOMRows As BOMRowsEnumerator, ItemTab As Long)

ItemTab = ItemTab + 3

' Iterate through the contents of the BOM Rows.
Dim i As Long
For i = 1 To oBOMRows.Count
' Get the current row.
Dim oRow As BOMRow
Set oRow = oBOMRows.Item(i)

'Set a reference to the primary ComponentDefinition of the row
Dim oCompDef As ComponentDefinition
Set oCompDef = oRow.ComponentDefinitions.Item(1)

Dim oPartNumProperty As Property
Dim oDescripProperty As Property

If TypeOf oCompDef Is VirtualComponentDefinition Then

'Get the file property that contains the "Part Number"
'The file property is obtained from the virtual component definition
Set oPartNumProperty = oCompDef.PropertySets _
.Item("Design Tracking Properties").Item("Part Number")

'Get the file property that contains the "Description"
Set oDescripProperty = oCompDef.PropertySets _
.Item("Design Tracking Properties").Item("Description")


Print #1, Tab(ItemTab); oRow.ItemNumber; Tab(10); oRow.ItemQuantity; Tab(20); _
oPartNumProperty.Value; Tab(35); oDescripProperty.Value;
Else

'Get the file property that contains the "Part Number"
'The file property is obtained from the parent
'document of the associated ComponentDefinition.
Set oPartNumProperty = oCompDef.Document.PropertySets _
.Item("Design Tracking Properties").Item("Part Number")

'Get the file property that contains the "Description"
Set oDescripProperty = oCompDef.Document.PropertySets _
.Item("Design Tracking Properties").Item("Description")


Print #1, Tab(ItemTab); oRow.ItemNumber; Tab(10); oRow.ItemQuantity; Tab(20); _
oPartNumProperty.Value; Tab(35); oDescripProperty.Value;

'Recursively iterate child rows if present.
If Not oRow.ChildRows Is Nothing Then
Call QueryBOMRowProperties(oRow.ChildRows, ItemTab)
End If

End If
Next

ItemTab = ItemTab - 3
End Sub

Public Sub print_via_notepad(fname As String)
Dim app
app = Shell("notepad " + fname, vbNormalFocus)
AppActivate app, True
End Sub


-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
0 Likes
739 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable
Instead of exporting to notepad, the BOM can be exported to an excel sheet
then you can sort any column you want
This is not very hard for any programmer.
I dont have time now, but I'll recode and post soon I get t ime
CVA

--
www.CVAengineering.com

IV11 Pro. sp2 and IV2008
Window XP Pro sp2
Pentium 3.2 Ghz, 3.0 GB of RAM
NVIDIA FX 3400 91.85
SpacePilot V 3.3.6
wrote in message news:5639115@discussion.autodesk.com...
an ADSK employee was nice enough to post this macro to print the bill of
materials, but I want it to output the parts sorted by part number. Can
someone help to write the necessary coding. Im not a programmer at all.
Thanks in advance.


Attribute VB_Name = "Module1"
Public Sub BOMPrint()

' Set a reference to the assembly document.
' This assumes an assembly document is active.
Dim oDoc As AssemblyDocument
Set oDoc = ThisApplication.ActiveDocument

Dim FirstLevelOnly As Boolean
If MsgBox("First level only?", vbYesNo) = vbYes Then
FirstLevelOnly = True
Else
FirstLevelOnly = False
End If

' Set a reference to the BOM
Dim oBOM As BOM
Set oBOM = oDoc.ComponentDefinition.BOM

' Set whether first level only or all levels.
If FirstLevelOnly Then
oBOM.StructuredViewFirstLevelOnly = True
Else
oBOM.StructuredViewFirstLevelOnly = False
End If

' Make sure that the structured view is enabled.
oBOM.StructuredViewEnabled = True

'Set a reference to the "Structured" BOMView
Dim oBOMView As BOMView
Set oBOMView = oBOM.BOMViews.Item("Structured")
Dim fname As String
fname = "c:\temp\BOM.txt"

Open fname For Output As #1 ' Open file for output.

Print #1, "Item"; Tab(10); "QTY"; Tab(20); "Part Number"; Tab(35);
"Description"
Print #1,
"------------------------------------------------------------------------"

'Initialize the tab for ItemNumber
Dim ItemTab As Long
ItemTab = -3

Call QueryBOMRowProperties(oBOMView.BOMRows, ItemTab)

Close #1

Call print_via_notepad(fname)
End Sub

Private Sub QueryBOMRowProperties(oBOMRows As BOMRowsEnumerator, ItemTab As
Long)

ItemTab = ItemTab + 3

' Iterate through the contents of the BOM Rows.
Dim i As Long
For i = 1 To oBOMRows.Count
' Get the current row.
Dim oRow As BOMRow
Set oRow = oBOMRows.Item(i)

'Set a reference to the primary ComponentDefinition of the row
Dim oCompDef As ComponentDefinition
Set oCompDef = oRow.ComponentDefinitions.Item(1)

Dim oPartNumProperty As Property
Dim oDescripProperty As Property

If TypeOf oCompDef Is VirtualComponentDefinition Then

'Get the file property that contains the "Part Number"
'The file property is obtained from the virtual component
definition
Set oPartNumProperty = oCompDef.PropertySets _
.Item("Design Tracking Properties").Item("Part Number")

'Get the file property that contains the "Description"
Set oDescripProperty = oCompDef.PropertySets _
.Item("Design Tracking Properties").Item("Description")


Print #1, Tab(ItemTab); oRow.ItemNumber; Tab(10);
oRow.ItemQuantity; Tab(20); _
oPartNumProperty.Value; Tab(35); oDescripProperty.Value;
Else

'Get the file property that contains the "Part Number"
'The file property is obtained from the parent
'document of the associated ComponentDefinition.
Set oPartNumProperty = oCompDef.Document.PropertySets _
.Item("Design Tracking Properties").Item("Part Number")

'Get the file property that contains the "Description"
Set oDescripProperty = oCompDef.Document.PropertySets _
.Item("Design Tracking Properties").Item("Description")


Print #1, Tab(ItemTab); oRow.ItemNumber; Tab(10);
oRow.ItemQuantity; Tab(20); _
oPartNumProperty.Value; Tab(35); oDescripProperty.Value;

'Recursively iterate child rows if present.
If Not oRow.ChildRows Is Nothing Then
Call QueryBOMRowProperties(oRow.ChildRows, ItemTab)
End If

End If
Next

ItemTab = ItemTab - 3
End Sub

Public Sub print_via_notepad(fname As String)
Dim app
app = Shell("notepad " + fname, vbNormalFocus)
AppActivate app, True
End Sub
0 Likes
Message 3 of 8

Anonymous
Not applicable
By the way, Sean Dotson has posted a tutorial on his website to show how to
export to an excel sheet
it'd be a good practice for you to learn API programming
CVA

--
www.CVAengineering.com

IV11 Pro. sp2 and IV2008
Window XP Pro sp2
Pentium 3.2 Ghz, 3.0 GB of RAM
NVIDIA FX 3400 91.85
SpacePilot V 3.3.6
"CVAengineering" wrote in message
news:5639284@discussion.autodesk.com...
Instead of exporting to notepad, the BOM can be exported to an excel sheet
then you can sort any column you want
This is not very hard for any programmer.
I dont have time now, but I'll recode and post soon I get t ime
CVA

--
www.CVAengineering.com

IV11 Pro. sp2 and IV2008
Window XP Pro sp2
Pentium 3.2 Ghz, 3.0 GB of RAM
NVIDIA FX 3400 91.85
SpacePilot V 3.3.6
wrote in message news:5639115@discussion.autodesk.com...
an ADSK employee was nice enough to post this macro to print the bill of
materials, but I want it to output the parts sorted by part number. Can
someone help to write the necessary coding. Im not a programmer at all.
Thanks in advance.


Attribute VB_Name = "Module1"
Public Sub BOMPrint()

' Set a reference to the assembly document.
' This assumes an assembly document is active.
Dim oDoc As AssemblyDocument
Set oDoc = ThisApplication.ActiveDocument

Dim FirstLevelOnly As Boolean
If MsgBox("First level only?", vbYesNo) = vbYes Then
FirstLevelOnly = True
Else
FirstLevelOnly = False
End If

' Set a reference to the BOM
Dim oBOM As BOM
Set oBOM = oDoc.ComponentDefinition.BOM

' Set whether first level only or all levels.
If FirstLevelOnly Then
oBOM.StructuredViewFirstLevelOnly = True
Else
oBOM.StructuredViewFirstLevelOnly = False
End If

' Make sure that the structured view is enabled.
oBOM.StructuredViewEnabled = True

'Set a reference to the "Structured" BOMView
Dim oBOMView As BOMView
Set oBOMView = oBOM.BOMViews.Item("Structured")
Dim fname As String
fname = "c:\temp\BOM.txt"

Open fname For Output As #1 ' Open file for output.

Print #1, "Item"; Tab(10); "QTY"; Tab(20); "Part Number"; Tab(35);
"Description"
Print #1,
"------------------------------------------------------------------------"

'Initialize the tab for ItemNumber
Dim ItemTab As Long
ItemTab = -3

Call QueryBOMRowProperties(oBOMView.BOMRows, ItemTab)

Close #1

Call print_via_notepad(fname)
End Sub

Private Sub QueryBOMRowProperties(oBOMRows As BOMRowsEnumerator, ItemTab As
Long)

ItemTab = ItemTab + 3

' Iterate through the contents of the BOM Rows.
Dim i As Long
For i = 1 To oBOMRows.Count
' Get the current row.
Dim oRow As BOMRow
Set oRow = oBOMRows.Item(i)

'Set a reference to the primary ComponentDefinition of the row
Dim oCompDef As ComponentDefinition
Set oCompDef = oRow.ComponentDefinitions.Item(1)

Dim oPartNumProperty As Property
Dim oDescripProperty As Property

If TypeOf oCompDef Is VirtualComponentDefinition Then

'Get the file property that contains the "Part Number"
'The file property is obtained from the virtual component
definition
Set oPartNumProperty = oCompDef.PropertySets _
.Item("Design Tracking Properties").Item("Part Number")

'Get the file property that contains the "Description"
Set oDescripProperty = oCompDef.PropertySets _
.Item("Design Tracking Properties").Item("Description")


Print #1, Tab(ItemTab); oRow.ItemNumber; Tab(10);
oRow.ItemQuantity; Tab(20); _
oPartNumProperty.Value; Tab(35); oDescripProperty.Value;
Else

'Get the file property that contains the "Part Number"
'The file property is obtained from the parent
'document of the associated ComponentDefinition.
Set oPartNumProperty = oCompDef.Document.PropertySets _
.Item("Design Tracking Properties").Item("Part Number")

'Get the file property that contains the "Description"
Set oDescripProperty = oCompDef.Document.PropertySets _
.Item("Design Tracking Properties").Item("Description")


Print #1, Tab(ItemTab); oRow.ItemNumber; Tab(10);
oRow.ItemQuantity; Tab(20); _
oPartNumProperty.Value; Tab(35); oDescripProperty.Value;

'Recursively iterate child rows if present.
If Not oRow.ChildRows Is Nothing Then
Call QueryBOMRowProperties(oRow.ChildRows, ItemTab)
End If

End If
Next

ItemTab = ItemTab - 3
End Sub

Public Sub print_via_notepad(fname As String)
Dim app
app = Shell("notepad " + fname, vbNormalFocus)
AppActivate app, True
End Sub
0 Likes
Message 4 of 8

mcgyvr
Consultant
Consultant
Thanks but I DO NOT want it to excel. (INV already does that)
I simply want to press a button and have it open notepad and sort by part number automatically, then I just click print. Its 2 button clicks (and a fast response)

IMO its too many steps to export to excel then open excel manually then sort then print.

But I will look into Seans tutorials to start learning the coding.

I like how this code works I just want it sorted by part number.

Frankly I don't know why INV doesnt have this as a standard button already in the Bom screen. I print the BOM every day to manually enter it into our MRP program.
Why you have to use a MS program to export to just to print a BOM is beyond me.

If I could change this code to work without notepad/excel or any other program thats even better.
I just want one button to press to print a bom. I can live with it quickly opening notepad automatically. Message was edited by: mcgyvr


-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
0 Likes
Message 5 of 8

Anonymous
Not applicable
I thought that would be an easy way to sort the part number
However, if you dont watn to export to excel then I guess you can save all
part numbers in an text array and sort them before print out to notepad
Anyone here might have a better idea?
CVA

--
www.CVAengineering.com

IV11 Pro. sp2 and IV2008
Window XP Pro sp2
Pentium 3.2 Ghz, 3.0 GB of RAM
NVIDIA FX 3400 91.85
SpacePilot V 3.3.6
wrote in message news:5639316@discussion.autodesk.com...
Thanks but I DO NOT want it to excel. (INV already does that)
I simply want to press a button and have it open notepad and sort by part
number automatically, then I just click print. Its 2 button clicks (and a
fast response)

IMO its too many steps to export to excel then open excel manually then sort
then print.

But I will look into Seans tutorials to start learning the coding.

I like how this code works I just want it sorted by part number.

Frankly I don't know why INV doesnt have this as a standard button already
in the Bom screen. I print the BOM every day to manually enter it into our
MRP program.
Why you have to use a MS program to export to just to print a BOM is beyond
me.

If I could change this code to work without notepad/excel or any other
program thats even better.
I just want one button to press to print a bom. I can live with it quickly
opening notepad automatically.

Message was edited by: mcgyvr
0 Likes
Message 6 of 8

Anonymous
Not applicable
I am not sure if this will help or not, but within the BOM you can sort by part number or any other field in the BOM by clicking the field header. You can even sort by multiple fields by holding the shift key and clicking the field header. Once they are sorted in the BOM, you can click the renumber icon (1,3 with the down arrow) and this will renumber the item numbers. Now each time you bring the BOM up it will still be sorted and if you place a PL in an idw, it will be in the same order as the BOM. The only drawback is it would need to be resorted if new parts are ordered.

Pete
0 Likes
Message 7 of 8

mcgyvr
Consultant
Consultant
pkquat
I am aware of that however this macro doesn't care what order the bom is in. I believe it must look at the order in the model browser only. Since sorting like you said has no effect on the output from this macro.

and I would prefer not to have to go into the BOM screen at all. I already sort in the parts list (drawing file) when placing the parts list


I guess this is harder than it seems to me. (I dont code obviously)
I was expecting someone to say sure just paste this into the code right above this Public Sub print_via_notepad(fname As String)

paste me!!

Public Function FormatPartsList(oPartsList As PartsList) As PartsList

On Error Resume Next
oPartsList.Sort "SUB-ASSEMBLY", False, "MATERIAL NAME", True, "PART NUMBER", True
oPartsList.Renumber
If Err Then
Err.Clear
MsgBox "Could not sort Parts List", , "Error"
End If

Set FormatPartsList = oPartsList

End Function


-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
0 Likes
Message 8 of 8

Anonymous
Not applicable
Strange. I am still new to macros, so am not a wizard with the code. A quick scan appeared to reference the actual BOM so I would have hoped it would be referencing the BOM in the presentation order. It would seem logical to do so. Hopefully Sanjay or someone will have an answer.

Pete
0 Likes