Hi @ChristiPedersen. You can take a look at this similar example VBA macro code for some additional information and pointers. I left a lot of comments in there, and left some lines of code commented out for now too. The file references are just generic ones for the example too.
Sub ExportBOM()
If ThisApplication.ActiveDocumentType <> kAssemblyDocumentObject Then Exit Sub
Dim oADoc As AssemblyDocument
Set oADoc = ThisApplication.ActiveDocument
Dim bMSFactoryExists As Boolean
bMSFactoryExists = (oADoc.ComponentDefinition.ModelStates.Count > 1)
'If False, there is no ModelState 'factory' and no document reference confusion
'if True, you will need to make sure you are working with the factory version document
'to ensure you are working with the ModelState factory document version:
If bMSFactoryExists And oADoc.ComponentDefinition.IsModelStateMember Then
Set oADoc = oADoc.ComponentDefinition.FactoryDocument
End If
On Error Resume Next
'you can use "On Error GoTo 0" to resume normal error handling (that is a zero at the end)
'that does not take you to the first line of the code, just resets the error handling
'the first ModelState in the ModelStates collection (Item(1)) will be the original 'Master or Primary' ModelState
'you can activate a specific one, then use the document it represents to work with going forward if you want
' Dim oMSs As ModelStates
' Set oMSs = oADoc.ComponentDefinition.ModelStates
' oMSs.Item(1).Activate
' Set oADoc = oMSs.Item(1).FactoryDocument
'then make sure the MemberEditScope is set to the active member (instead of all members) like this
' If oMSs.MemberEditScope <> kEditActiveMember Then
' oMSs.MemberEditScope = kEditActiveMember
' End If
Dim sBOMCustomizationFile As String
sBOMCustomizationFile = "C:\Temp\MyBOMCustomization.xml"
Dim sExportedFileName As String
sExportedFileName = Strings.Replace(oADoc.FullFileName, ".iam", ".xlsx")
Dim oBOM As Inventor.BOM
Set oBOM = oADoc.ComponentDefinition.BOM
Call oBOM.ImportBOMCustomization(sBOMCustomizationFile)
oBOM.StructuredViewEnabled = True
If oBOM.StructuredViewDelimiter <> "," Then oBOM.StructuredViewDelimiter = ","
oBOM.StructuredViewFirstLevelOnly = False
Dim oStView As BOMView
Set oStView = oBOM.BOMViews.Item("Structured")
'you can check this, but can not change its value, because it is ReadOnly
Dim sBOMViewMemberName As String
sBOMViewMemberName = oStView.ModelStateMemberName 'ReadOnly
Call oStView.Sort("Item", True)
Call oStView.Export(sExportedFileName, kMicrosoftExcelFormat)
End Sub
I don't use VBA for hardly anything anymore, in favor of iLogic, because Inventor has been steering away from it for years now too, due to security issues. (Link1, Link2)
Edit: I posted this before I saw your last response.
Wesley Crihfield
(Not an Autodesk Employee)