I managed to solve it Krieg, I was referring to oWB when it should be oWB2.
See code.
May I ask you to take a look at this post:
https://forums.autodesk.com/t5/inventor-customization/select-features-and-perform-right-mouse-button...
I would love to know a solution to my second question and I suspect you are seeing through that.
A little explanation:
When you are in a part mode you can choose 'Expand browser' to expand certain nodes and show the sketches and their dimensions which in turn can be changed. With Laurent's solution also the feature dimensions became visible like bends, flanges, radi etc. and can be changed as desired. I would like to have that also available when in edit mode while in an assembly. Do you know how I can get hold of those browser nodes in edit mode and make the dimensions visible in the same way?
' Export_BOM
' Yes, it's VBA Code.
' I think you have to add the a reference to Excel.
' In VBA Editor goto menu "Tools" --> "References".
' Search for the entry "Microsoft Excel xx.0 Object Library" and activate it.
'Modify the strings to your language version
Private Const sBOMStructureType As String = "BOM Structure" ' Name of the column (column header) in BOM for structure type (Normal, Purchased, Inseparable)
Private Const sNormal As String = "Normal"
Private Const sPurchased As String = "Purchased"
Private Const sInseparable As String = "Inseparable"
Private Const sPath As String = "C:\Temp\" 'Path to save the exported BOM's to
Private Const sFilename As String = "PartsOnly_" 'Static part of filename
'End Private Const (This text is only to show the underscore after 'PartsOnly'
Private Sub BOMExport()
'Makro assumes:
'- An assemblydocument is open
'- The part type column is NOT excluded in parts only view
'--------------------------------------------------------------------------------------------------
' Set timer
Dim Start As Double
Start = Timer
Dim TimeDateStamp As String
TimeDateStamp = Format(Date, "yyyy-mm-dd") & "_" & Format(Time, "hh:mm:ss")
TimeDateStamp = Replace(TimeDateStamp, ":", "")
'--------------------------------------------------------------------------------------------------
'GoTo Skip
Dim oApp As Inventor.Application
Set oApp = ThisApplication
Dim oAssDoc As AssemblyDocument
Set oAssDoc = oApp.ActiveDocument
Dim oBOM As BOM
Set oBOM = oAssDoc.ComponentDefinition.BOM
oBOM.PartsOnlyViewEnabled = True
Dim oBOMview As BOMView
For Each oBOMview In oBOM.BOMViews
'If oBOMview.ViewType = BOMViewTypeEnum.kPartsOnlyBOMViewType Then
If oBOMview.ViewType = kPartsOnlyBOMViewType Then
'If oBOMview.ViewType = kStructuredBOMViewType Then
Exit For
End If
Next
If oBOMview Is Nothing Then
MsgBox ("Can't get Parts only BOM view")
Exit Sub
End If
'' Check if file exist
'If Dir(sPath & sFilename & sNormal & ".xlsx") <> "" Then
' Call MsgBox("File " & sPath & sFilename & sNormal & ".xlsx already exist.", vbCritical, "ExportBOM")
' Exit Sub
'ElseIf Dir(sPath & sFilename & sPurchased & ".xlsx") <> "" Then
' Call MsgBox("File " & sPath & sFilename & sNormal & ".xlsx already exist.", vbCritical, "ExportBOM")
' Exit Sub
'ElseIf Dir(sPath & sFilename & sInseparable & ".xlsx") <> "" Then
' Call MsgBox("File " & sPath & sFilename & sNormal & ".xlsx already exist.", vbCritical, "ExportBOM")
' Exit Sub
'End If
Dim strBOM1 As String
strBOM1 = sPath & sFilename & sNormal & " - " & TimeDateStamp & ".xlsx"
Dim strBOM2 As String
strBOM2 = sPath & sFilename & sPurchased & " - " & TimeDateStamp & ".xlsx"
Call oBOMview.Export(strBOM1, kMicrosoftExcelFormat, sNormal)
Call oBOMview.Export(strBOM2, kMicrosoftExcelFormat, sPurchased)
'Call oBOMview.Export(sPath & sFilename & sInseparable & ".xlsx", kMicrosoftExcelFormat, sInseparable)
Skip:
Dim oExcelApp As Excel.Application
Set oExcelApp = GetObject("", "Excel.Application")
If oExcelApp Is Nothing Then
MsgBox ("Can't get Excel")
Exit Sub
End If
Dim oWB As Workbook
Set oWB = oExcelApp.Workbooks.Open(strBOM1)
If Not oWB Is Nothing Then
Set oWB = Filter(oExcelApp, oWB, sNormal)
If Not oWB Is Nothing Then
Call List_Objects(oExcelApp, oWB)
oWB.Save
End If
End If
Dim oWB2 As Workbook
Set oWB2 = oExcelApp.Workbooks.Open(strBOM2)
If Not oWB2 Is Nothing Then
Set oWB2 = Filter(oExcelApp, oWB2, sPurchased)
If Not oWB2 Is Nothing Then
Call List_Objects(oExcelApp, oWB2)
oWB2.Save
End If
End If
'Dim oWB3 As Workbook
'Set oWB3 = oExcelApp.Workbooks.Open(sPath & sFilename & sInseparable & ".xlsx")
'If Not oWB3 Is Nothing Then
' Set oWB3 = Filter(oExcelApp, oWB3, sInseparable)
' If Not oWB3 Is Nothing Then
' oWB3.Save
' End If
'End If
Dim Result As VbMsgBoxResult
Result = MsgBox("Export done. & vbnewline & Elapsed time: " & Timer - Start & " Seconds" & vbNewLine & "View files in Excel?", vbYesNo, "ExportBOM")
If Result = vbYes Then
oExcelApp.Visible = True
Else
For Each oWB In oExcelApp.Workbooks
oWB.Close (False)
Next
oExcelApp.Quit
End If
End Sub
Private Function Filter(ByVal oExcelApp As Excel.Application, ByVal oWB As Workbook, ByVal sName As String) As Workbook
Dim lLastRow As Long
Dim t As Long
'lLastRow = oExcelApp.Cells(oWB.ActiveSheet.Rows.Count, 1).End(xlUp).Rows.Row
' Alternativ way to find last row
lLastRow = oWB.ActiveSheet.UsedRange.Rows.Count
'Debug.Print "lLastRow: " & lLastRow
Dim s As Long
s = FindColumn(oExcelApp, oWB)
If s = 0 Then
Call MsgBox("Column BOM Structure Type missing.", vbCritical, "ExportBOM")
Filter = Nothing
Exit Function
End If
For t = lLastRow To 2 Step -1
If Not oExcelApp.Cells(t, s).Value = sName Then
oWB.ActiveSheet.Rows(t).Delete Shift:=xlUp
End If
Next t
Set Filter = oWB
End Function
Private Function FindColumn(ByVal oExcelApp As Excel.Application, ByVal oWB As Workbook) As Long
Dim lLastColumn As Long
lLastColumn = oExcelApp.Cells(1, oWB.ActiveSheet.Columns.Count).End(xlToLeft).Columns.Column
Dim s As Long
Dim rCells As Range
For s = lLastColumn To 1 Step -1
Set rCells = oExcelApp.Cells(1, s)
If rCells.Value = sBOMStructureType Then
FindColumn = s
Exit For
End If
Next s
End Function
Private Sub List_Objects(ByVal oExcelApp As Excel.Application, ByVal oWB As Workbook) ' Create Table
Dim oWS As WorkSheet
Set oWS = oWB.ActiveSheet
' AutoFit Columns
oWS.Cells.EntireColumn.AutoFit
' Create Table
Dim LR As Long
Dim LC As Long
LR = oWS.Cells(Rows.Count, 1).End(xlUp).Row
LC = oWS.Cells(1, Columns.Count).End(xlToLeft).Column
Dim Rng As Range
Set Rng = oWS.Cells(1, 1).Resize(LR, LC)
oWS.ListObjects.Add xlSrcRange, xllistobjecthasheaders:=xlYes, Destination:=Rng
oWS.ListObjects(1).Name = "Table"
oWS.ListObjects(1).TableStyle = "TableStyleLight21"
' Freeze 1st row
oExcelApp.Application.ScreenUpdating = True
oWS.Cells(2, 1).Select
oExcelApp.ActiveWindow.FreezePanes = True
End Sub