BOM export with Bomview.export shows quantitys for all Modelstates the assembly has
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello
we have a VBA makro witch exports the BOM to a CSV file
that is working fine, but
in the CSV file are now as much colums for quantitys as Modelstates are in the Assembly.
if i export it manually from Inventor then there ist only one colum of quantitys
that behavior came with the change from inventor 2020 to 2022
for tipps how to solve this i woult be very thankful
here is the Makro:
Sub Stüli_Exportieren_CSV_inv2022()
' Set a reference to the assembly document.
Dim oAsmDoc As AssemblyDocument
Set oAsmDoc = ThisApplication.ActiveDocument
'abfragen ob ein Assembly geöffnet ist
If ThisApplication.ActiveDocumentType = kAssemblyDocumentObject Then
' es sollen keine abfragen kommen
ThisApplication.SilentOperation = True
' prüfen ob in BG geschrieben werden kann
If GetAttr(oAsmDoc.File.FullFileName) And vbReadOnly Then
MsgBox "3D-Modell ist schreibgeschützt" & vbCrLf & vbCrLf & "Stückliste KANN NICHT geändert werden."
GoTo Fettich
End If
On Error GoTo allgFehler
' get AssemblyComponentDefinition
Dim AssemblyDef As AssemblyComponentDefinition
Set AssemblyDef = oAsmDoc.ComponentDefinition
' get Manager of Representations zum Prüfen ob Hauptansicht oder modelstatus
Dim dViewRepitm As ModelStates
Set dViewRepitm = AssemblyDef.ModelStates
' get active Representation modelstatus welche ansicht ist aktiv
Dim dViewRep As ModelState
Set dViewRep = AssemblyDef.ModelStates.ActiveModelState
'BOM = Stüli definieren
Dim oBOM As BOM
Set oBOM = oAsmDoc.ComponentDefinition.BOM
' ermittlung des Dateinamens
Dim fso As Object
Set fso = CreateObject("Scripting.FilesystemObject")
Dim FullFileName As String
Dim FileName As String
Dim CSVPfad As String
FullFileName = ThisApplication.ActiveDocument.FullFileName
pfad = fso.getfilename(FullFileName)
FileName = Left(pfad, 10) ' & ".pdf"
'fragen ob filename einen Inhalt hat
If FileName = "" Then
MsgBox "Der Assembly Dateinamen konnte NICHT ermittelt werden!"
GoTo Fettich
End If
' username ermitteln
Dim Opp As GeneralOptions
Set Opp = ThisApplication.GeneralOptions
Dim U_Name As String
U_Name = Opp.UserName
'ausgabe pfad und name
CSVPfad = "C:\Temp\Structured-BOM__" & FileName & "__" & Format(DateTime.Date, "YYYY-MM-DD") & "__" & U_Name & ".csv"
' abfragen ob CSV-Datei vorhanden ist und gelöscht werden soll
If fso.fileExists(CSVPfad) Then
Dim Antwort As VbMsgBoxResult
Dim Meldung As String
Meldung = "Soll die vorhandene CSV-Datei Überschrieben werden? "
Antwort = MsgBox(Meldung, vbYesNo + vbQuestion, "Speichern der CSV-Datei")
If Antwort = vbYes Then
' abfragen ob csv schreibgeschützt ist
On Error GoTo CSVFehler
'die vorhandene datei löschen
Kill (CSVPfad)
Else
GoTo Fettich
End If
End If
'Dim oBOMView As Object
Dim oStructuredBOMView As BOMView
If dViewRep.Name = "Hauptansicht" Then
' Set whether first level only or all levels.
oBOM.StructuredViewFirstLevelOnly = False
'Wenn die Struktur-Stückliste nicht aktiviert ist, dann wird das jetzt erledigt
If oBOM.StructuredViewEnabled = False Then
oBOM.StructuredViewEnabled = True
End If
'Strukturiert Ansicht aktiv setzten
Set oStructuredBOMView = oBOM.BOMViews.Item("Strukturiert")
'sortierung der STüli nach objekt
Call oStructuredBOMView.Sort("Objekt", True)
' BOM wird exportiert
'Beispiel Call oStructuredBOMView.Export("C:\Temp\StructuredBOM.csv", kTextFileCommaDelimitedFormat)
Call oStructuredBOMView.Export(CSVPfad, kTextFileCommaDelimitedFormat)
Else
' aktuelle Modellstatus in akt speichern
Dim akt As ModelState
Set akt = dViewRepitm.Item(dViewRep.Name) 'AssemblyDef.ModelStates.ActiveModelState
' get the first Representation view
Set dViewRep = dViewRepitm.Item(1)
' activate the first view
dViewRep.Activate
' Set whether first level only or all levels.
oBOM.StructuredViewFirstLevelOnly = False
'Wenn die Struktur-Stückliste nicht aktiviert ist, dann wird das jetzt erledigt
If oBOM.StructuredViewEnabled = False Then
oBOM.StructuredViewEnabled = True
End If
'Strukturiert Ansicht aktiv setzten
'Dim oStructuredBOMView As BOMView 'Dim oBOMView As Object
Set oStructuredBOMView = oBOM.BOMViews.Item("Strukturiert")
Call oStructuredBOMView.Sort("Objekt", True)
' BOM wird exportiert Beispiel Call oStructuredBOMView.Export("C:\Temp\StructuredBOM.csv", kTextFileCommaDelimitedFormat)
Call oStructuredBOMView.Export(CSVPfad, kTextFileCommaDelimitedFormat)
' ab hier wird die modellstatus wieder auf die vorher aktive gesetzt
Set dViewRep = dViewRepitm.Item(akt.Name)
dViewRep.Activate
End If
'dokuemnt wird gespeichert damit wieder sofort die stüli geöffnet werden kann
oAsmDoc.Save
Else
MsgBox ("Es ist keine Baugruppe im Inventor geöffnet")
End If
' abfrage ob gleich die Excel vorlage und die CSV datei geöffnet werdne sollen
Dim ExcelAntwort As VbMsgBoxResult
Dim ExcelMeldung As String
ExcelMeldung = "Möchten Sie die Excel Vorlage und die CSV-DaTei gleich öffnen?"
ExcelAntwort = MsgBox(ExcelMeldung, vbYesNo + vbQuestion, " Die Stükliste wurde erfolgreich in die CSV-Datei exportiert")
If ExcelAntwort = vbYes Then
On Error GoTo CSVFehler ' abfragen ob csv schreibgeschützt ist
'öffnen der exportierten csv datei mit libre office
Dim oCSVFile As Variant
'oCSVFile = Shell("C:\Program Files\LibreOffice\program\scalc.exe C:\Temp\Structured-BOM__" & FileName & "__" & Format(DateTime.Date, "YYYY-MM-DD") & "__" & U_Name & ".csv", vbNormalFocus)
oCSVFile = Shell("C:\Program Files\LibreOffice\program\scalc.exe """ & CSVPfad & """", vbNormalFocus)
'definieren der Vorlage für shell befehl
Dim Stuelivorlage As String
'Stuelivorlage = "K:\_Goepfert-Aktuell\Verwaltung - Organisation - Allgemeines\B__Stücklisten - Orga - Export\Vorlage für 3d Export.xltx"
Stuelivorlage = "C:\Vault\Templates\Vorlage für 3d Export.xltx"
' zuweisen des NEuen namen für ,,Vorlage für 3d Export.xltx,,
Dim StrukBOM As String
StrukBOM = "C:\Temp\Structured-BOM__" & FileName & "__" & Format(DateTime.Date, "YYYY-MM-DD") & "__" & U_Name & ".xlsx"
'kopieren der vorlage ,,Vorlage für 3d Export.xltx,, zu dateinamen Structuret-BOM-...
neuevorlage = fso.CopyFile(Stuelivorlage, StrukBOM)
Dim File_Attr As Long
File_Attr = GetAttr(StrukBOM)
If File_Attr = 1 Or File_Attr = 33 Then
SetAttr StrukBOM, VbFileAttribute.vbNormal
End If
' abfrage ob schon die 6stellige stüli nummer bekannt ist
' dann massegbox abfrage nach der nummer und Variable zuweisen
' dann die Variable dem Dateinamen zuweisen
'öffnen der Kopierten Vorlage mit libre office
Dim oVorlageFile As Variant
'oVorlageFile = Shell("C:\Program Files\LibreOffice\program\scalc.exe """ & Stuelivorlage & """", vbNormalFocus)
oVorlageFile = Shell("C:\Program Files\LibreOffice\program\scalc.exe -o """ & StrukBOM & """", vbNormalFocus)
End If
ThisApplication.SilentOperation = False
Exit Sub
CSVFehler:
MsgBox (" Es ist ein Fehler aufgetreten. " & vbCr & vbCr & CSVPfad & vbCr & vbCr & " ist geöffnet! " & vbCr & vbCr & " " & Err.Description)
ThisApplication.SilentOperation = False
Exit Sub
allgFehler:
MsgBox (" Es ist ein Fehler aufgetreten. " & vbCr & vbCr & CSVPfad & vbCr & vbCr & " ist die Datei nicht ausgecheckt " & vbCr & " Bitte Stüli überprüfen " & vbCr & vbCr & " " & Err.Description)
ThisApplication.SilentOperation = False
Exit Sub
Fettich:
ThisApplication.SilentOperation = False
End Sub