I would stick with iLogic if I were you. It is based on VB.NET. VB.NET and VBA are both based on Visual Basic coding language, but the VB.NET system is newer, more dynamic, has more documentation available, and is less restrictive. It's up to you though, and what fits you and your style best.
I think those Excel COM engine settings mainly effect when you use iLogic's built-in GoExcel functionality. Since we are not using any of that, it may not effect this. This code interacts directly with Excel using vb.net code, instead of the GoExcel stuff.
Here is an updated version of the code, in which I have incorporated some additional code to ensure it uses a new sheet (tab) in the Excel document each time. I put our earlier code into the middle of this new code, mostly as its own Sub. I designed the sub to accept the DrawingDocument, the Excel file name, and the Excel sheet name as input variables, so I don't have to redefine them within the sub. I wanted the drawing document defined outside of the sub, so I could use that variable to rename the Excel sheet (tab) after the sub runs.
We may have to do something different with the 'Repeat' loop, depending on how you are using it. Are you trying to export multiple PartsLists from the same drawing, or is this loop primarily just to find the PartsList? If you are attempting to export multiple PartsLists from the same drawing, then we may need to include drawing sheet name to keep it unique, and move the loop to just outside of where the Sub is ran, so it will repeat the whole sub, not just the export process, because a new Excel sheet (with a new name) will need to be created for each loop. If the loop is only to find the PartsList, I would suggest eliminating this loop and simply looping through each sheet (near the beginning), checking the PartsList.Count of each until you find the first one, then resume the rest of the code.
Here's what I've got so far:
AddReference "Microsoft.Office.Interop.Excel.dll"
Imports Microsoft.Office.Interop.Excel
Sub Main
'Specify the Excel file's (Workbook) full path & file name (with extension)
'It is calling a Windows OpenFileDialog to allow the user to do this
Dim oDesktop As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory)
Dim oFileName As String = oDesktop & "\EAP Cutlist Master.xlsx"
Dim oExcelApp As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application
oExcelApp.DisplayAlerts = False
oExcelApp.Visible = False
'Attempt to open the specified Workbook (file) using the supplied file name
Dim oWB As Workbook
Try
oWB = oExcelApp.Workbooks.Open(oFileName)
Catch oEx As Exception
MsgBox("The attempt to open the Excel file named '" & oFileName & "' failed." & vbCrLf & _
"The error message for this failure is as follows:" & vbCrLf & _
oEx.Message & vbCrLf & vbCrLf & _
"Its 'StackTrace is as follows:" & vbCrLf & _
oEx.StackTrace & vbCrLf & vbCrLf & _
"Its Source is as follows:" & vbCrLf & _
oEx.Source, vbOKOnly + vbCritical, "Couldn't Open File")
Exit Sub
End Try
'Add a new Worksheet (tab) to the open Workbook
'all input options are optional, so its name will be automated
'(its name can be changed later, if needed)
Dim oWS As Worksheet = oWB.Worksheets.Add()
Dim oWSName As String = oWS.Name
'<<<< HERE'S WHERE WE'RE RUNNING YOUR CODE (MOSTLY AS A SUB) >>>
'We're defining the document outside of the sub, so we can use its name after the sub
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Drawing Documents.",vbOKOnly, "WRONG DOCUMENT TYPE")
Exit Sub
End If
Dim oDDoc As DrawingDocument = ThisDrawing.Document
'we're giving it the name of the new sheet (tab) for use in the Export settings
ExportBOM(oDDoc, oFileName, oWSName)
'Now we can attempt to rename the tab in Excel (if the name isn't already being used)
Dim oSheetName As String = IO.Path.GetFileNameWithoutExtension(oDDoc.FullFileName)
Try
oWS.Name = oSheetName
oWB.Save
oWB.Close
oExcelApp.Quit
Catch
MsgBox("Unable to change the Excel sheet name to:" & vbCrLf & _
oSheetName & vbCrLf & _
"Either the name isn't usable or it is already being used." & vbCrLf & _
"If multiple PartsLists are being exported from the same drawing document," & vbCrLf & _
"you may need to add the sheet name (or similar) to the name of the Excel worksheet to make it unique." & vbCrLf & _
"Making the Excel document visible, so the sheet name can be manually edited.", vbOKOnly, " ")
oExcelApp.Visible = True
End Try
'we shouldn't need this due to Export settings
'oWS.Columns.AutoFit
End Sub
Sub ExportBOM(oDrDoc As DrawingDocument, oExcelFile As String, oTableName As String)
Dim oUParams As UserParameters = oDrDoc.Parameters.UserParameters
Dim oUParam As UserParameter
Try
oUParam = oUParams.Item("lSheet")
Catch
oUParam = oUParams.AddByValue("lSheet", "", UnitsTypeEnum.kTextUnits)
MultiValue.SetList("lSheet", "MT", "MESS")
End Try
Dim oSheetNames As New ArrayList
Dim oSheet As Inventor.Sheet
For Each oSheet In oDrDoc.Sheets
oSheetNames.Add(oSheet.Name)
Next
MultiValue.List("lSheet") = oSheetNames
Repeat :
Dim oSheetName As String= InputListBox("Select which sheet to get BOM", MultiValue.List("lSheet"), Parameter("ISheet"), Title := "Selct BOM", ListName := "Sheet List")
'get the path and name of the drawing file
'Dim oPathAndName As String = Left(oDDoc.FullDocumentName,Len(oDDoc.FullDocumentName) - 4)
oSheet = oDrDoc.Sheets(oSheetName) ' sheet by name
If oSheet.PartsLists.Count > 0 Then
Dim oPartslist As PartsList = oSheet.PartsLists(1)
Dim oDesktop As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory)
Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
'oOptions.Value("Template") = oDesktop & "\EAP Cutlist Master.xlsx"
oOptions.Add("ExportedColumns", "ITEM;EXTRUSION;DESCRIPTION;LENGTH;ITEM QTY;CATEGORY;FINISH")
oOptions.Add("StartingCell", "A1")
'This is where we're specifying the name of the new sheet (tab) we created
oOptions.Add("TableName", oTableName)
oOptions.Add("IncludeTitle", False)
oOptions.Add("AutoFitColumnWidth", True)
oPartslist.Export(oExcelFile, PartsListFileFormatEnum.kMicrosoftExcel, oOptions)
Else
oRetry = MsgBox("That sheet doesn't contain any PartsLists." & vbCrLf & _
"Would you Like To Try another Sheet ? ", vbYesNo + vbQuestion, " NO P - LIST FOUND ")
If oRetry = True Then GoTo Repeat
End If
End Sub
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.
Wesley Crihfield

(Not an Autodesk Employee)