Thanks a lot WCrihfield.
I can't get it to work.
Tried several things, but no luck.
It crashes in 'Private Sub CreateTable'.
See image of the error message.
Could it be something to do with Excel's language/format?
I use a Dutch language version.
Changed the '1' to 'A' but it's not it.
See code:
AddReference "Microsoft.Office.Interop.Excel.dll"
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Excel
Sub Main()
' Try to Renumbers Item number:
' - For Structured BOM only FirstLevel is working now
' - For PartsOnly BOM it' working
' - Now two(2) List are created, it's more efficient to create the two Lists in one(1) itteration
iLogicVb.RunExternalRule("Renumber_and_Sort_BOM_Item_Numbers")
'**************************************************************************************
'You can change the output path by editing CSVpath below
'CSVpath = ("C:\Inventor\") 'If you change the path, remember to keep a \ at the end
'CSVpath = ThisDoc.Path + "\"
'**************************************************************************************
'Adapted from Inventor API Samples by Clint Brown @ClintBrown3D
'iLogic code Originally posted at https://clintbrown.co.uk/bom-export-with-ilogic
oDoc = ThisDoc.ModelDocument
'Ensure that we are in an Assembly file - Exit if not
If oDoc.DocumentType = kPartDocumentObject Then
MessageBox.Show("You need to be in an Assembly to Export a BOM", "@ClintBrown3D iLogic")
Return
End If
oDoc = ThisApplication.ActiveDocument
Dim oBOM As BOM
oBOM = oDoc.ComponentDefinition.BOM
'**************************************************************************************
'You can change the output path by editing CSVpath below - by default the path is the same as the assembly file
'CSVpath = ("C:\Inventor\") 'If you change the path, remember to keep a \ at the end
CSVpath = ThisDoc.Path + "\"
MessageBox.Show(CSVpath)
'**************************************************************************************
'Get user input for Export Type:
Dim MyArrayList As New ArrayList
MyArrayList.Add("")
MyArrayList.Add("Structured - All Levels")
MyArrayList.Add("")
MyArrayList.Add("Structured - Single Level")
MyArrayList.Add("")
MyArrayList.Add("Parts Only - (Shows components in a flat list)")
ClintsBoMExporter = InputListBox("Choose a BoM type to Export: " & ClintBrown3D , MyArrayList, d0, Title := "@ClintBrown3D: BoM Export ", ListName := "BoM Type")
If ClintsBoMExporter = "Structured - All Levels" Then :GoTo GoAllLevelsExport : End If
If ClintsBoMExporter = "Structured - Single Level" Then :GoTo GoSingleLevelsExport : End If
If ClintsBoMExporter = "Parts Only - (Shows components in a flat list)" Then : GoTo GoPartExport : End If
If ClintsBoMExporter = "" Then : Return : End If
'STRUCTURED BoM ALL LEVELS:
GoAllLevelsExport:
' the structured view to 'all levels'
oBOM.StructuredViewFirstLevelOnly = False
' Make sure that the structured view is enabled.
oBOM.StructuredViewEnabled = True
Dim oStructuredBOMView As BOMView
oStructuredBOMView = oBOM.BOMViews.Item("Structured")
' Export the BOM view to an Excel file
Dim BoM As String
BoM = CSVpath + ThisDoc.FileName(False) + " STRUCTURED BoM ALL LEVELS.xlsx"
oStructuredBOMView.Export(CSVpath + ThisDoc.FileName(False) + " STRUCTURED BoM ALL LEVELS.xlsx", kMicrosoftExcelFormat)
'oStructuredBOMView.Export(BoM, kMicrosoftExcelFormat)
GoTo GoLaunch:
'STRUCTURED BoM Single Level
GoSingleLevelsExport:
oBOM.StructuredViewFirstLevelOnly = True
oBOM.StructuredViewEnabled = True
oStructuredBOMView = oBOM.BOMViews.Item("Structured")
BoM = CSVpath + ThisDoc.FileName(False) + " STRUCTURED BoM Single Level.xlsx"
'oStructuredBOMView.Export(CSVpath + ThisDoc.FileName(False) + " STRUCTURED BoM Single Level.xlsx", kMicrosoftExcelFormat)
oStructuredBOMView.Export(BoM, kMicrosoftExcelFormat)
GoTo GoLaunch:
'PARTS ONLY BoM
GoPartExport:
oBOM.PartsOnlyViewEnabled = True
Dim oPartsOnlyBOMView As BOMView
oPartsOnlyBOMView = oBOM.BOMViews.Item("Parts Only")
BoM = CSVpath + ThisDoc.FileName(False) + " PARTS ONLY BoM.xlsx"
oPartsOnlyBOMView.Export(CSVpath + ThisDoc.FileName(False) + " PARTS ONLY BoM.xlsx", kMicrosoftExcelFormat)
'oStructuredBOMView.Export(BoM, kMicrosoftExcelFormat)
GoTo GoLaunch:
'Get user input - do you want to see the BoM?
GoLaunch :
' i = MessageBox.Show("Preview the BOM?", "@ClintBrown3D iLogic", MessageBoxButtons.YesNo)
' If i = vbYes Then : launchviewer = 1 : Else : launchviewer = 0 : End If
' 'If launchviewer = 1 Then ThisDoc.Launch(CSVpath + ThisDoc.FileName(False) + ".xlsx")
' If launchviewer = 1 Then ThisDoc.Launch(CSVpath + ThisDoc.FileName(False) + " STRUCTURED BoM ALL LEVELS.xlsx")
' Define the file to create/open
'myXLS_File = "C:\Temp\Best_Excel_File_Ever.xls"
myXLS_File = (CSVpath + ThisDoc.FileName(False) + " STRUCTURED BoM ALL LEVELS.xlsx")
' Get the Inventor user name From the Inventor Options
myName= ThisApplication.GeneralOptions.UserName
' Define Excel Application object
'excelApp = CreateObject("Excel.Application")
Dim ExcelApp As Microsoft.Office.Interop.Excel.Application = GetExcel
' Set Excel to run visibly, change to false if you want to run it invisibly
excelApp.Visible = True
' Suppress prompts (such as the compatibility checker)
excelApp.DisplayAlerts = False
' Check for existing file
If Dir(myXLS_File) <> "" Then
' Workbook exists, open it
excelWorkbook = excelApp.Workbooks.Open(myXLS_File)
ExcelSheet = excelWorkbook.Worksheets(1)
Else
' Create a new spreadsheet from template
'excelWorkbook = excelApp.Workbooks.Add (Template: = "C:\Temp\Best_Excel_Template_Ever.xlt")
End If
'Dim ExcelApp As Microsoft.Office.Interop.Excel.Application = GetExcel
Dim oWB1 As Workbook = excelApp.ActiveWorkbook
CreateTable(excelApp, oWB1, "TableStyleLight12")
' Insert data into Excel.
' With excelApp
' .Range("A1").Select
' .ActiveCell.FormulaR1C1 = "Hello, " & myName
' End With
' Set all of the columns to autofit
excelApp.Columns.AutoFit
' Save the file
'excelWorkbook.SaveAs (myXLS_File)
'' Close the workbook and the Excel Application
'' Uncomment if you want to close the xls file at the end
'excelWorkbook.Close
'excelApp.Quit
'excelApp = Nothing
End Sub
Private Sub CreateTable(ByVal oExcelApp As Excel.Application, ByVal oWB As Workbook, ByVal TableStyle As String)
Dim oWs As WorkSheet = oWB.ActiveSheet
' Get Last Row and Last Column as numbers
Dim LR As Long ' = oWs.Cells(oWs.Rows.Count, "A").End(xlUp).row
Dim LC As Long ' = oWs.Cells(1, oWs.Columns.Count).End(xlToLeft).column
LR = oWs.Rows(oWs.Rows.Count).Row
LC = oWs.Columns(oWs.Columns.Count).Column
' 'Sub LastRowInOneColumn()
''Find the last used row in a Column: column A in this example
' Dim LR As Long
' With ActiveSheet
' LR = .Cells(.Rows.Count, "A").End(xlUp).Row
' End With
' 'MsgBox LastRow
''End Sub
''Sub LastColumnInOneRow()
''Find the last used column in a Row: row 1 in this example
' Dim LC As Integer
' With ActiveSheet
' LC = .Cells(1, .Columns.Count).End(xlToLeft).Column
' End With
' 'MsgBox LastCol
''End Sub
' Create Table
Dim rng As Range = oWs.Cells(1, 1).Resize(LR, LC)
oWs.ListObjects.Add(xlSrcRange, xllistobjecthasheaders:=xlYes, Destination:=rng)
oWs.ListObjects(1).Name = "Table"
oWs.ListObjects(1).TableStyle = TableStyle '"TableStyleLight21"
oWs = Nothing
oWB = Nothing
oExcelApp = Nothing
End Sub
Function GetExcel() As Microsoft.Office.Interop.Excel.Application
Dim oXL As Microsoft.Office.Interop.Excel.Application
Try
'try to find an already running instance of the Excel Application
oXL = GetObject( "Excel.Application")
Catch
'it wasn't found open, so create an instance of it (start the application)
oXL = CreateObject("Excel.Application")
'oXL = New Microsoft.Office.Interop.Excel.Application
Catch
MsgBox("Failed to Get/Create an instance of the Excel Application. Exiting.", , "")
Exit Function
End Try
Return oXL
End Function