Code:
Option ExplicitPrivate Sub WriteBG()
'Die Spalte "Dateiname" ist zwingend erfoderlich, kann nach
'dem Ausführen des Makros aber wieder entfernt werden
'Es fehlt eine Routine zum Unterdrücken doppelter Einträge pro Durchlauf
'Ein zweiter Durchlauf erzeugt doppelte Einträge, vorher Teileliste löschen
'und neu einfügen
Dim oDrawDoc As DrawingDocument
Dim oRefedDoc As AssemblyDocument
Dim oDoc As Document
Dim oOcc As ComponentOccurrence
Dim oPartsList As PartsList
Dim oColumn As PartsListColumn
Dim sDocname As String
Dim sOccName As String
On Error Resume Next
Set oDrawDoc = ThisApplication.ActiveDocument
If Err <> 0 Then
MsgBox "Keine IDW aktiv."
Exit Sub
End If
Set oPartsList = oDrawDoc.ActiveSheet.PartsLists.Item(1)
If Err <> 0 Then
MsgBox "Keine Teileliste gefunden."
Exit Sub
End If
Set oColumn = oPartsList.PartsListColumns.Item("Baugruppe")
If Err <> 0 Then
Set oColumn = oPartsList.PartsListColumns.Add(PropertyTypeEnum.kCustomProperty, , "Baugruppe")
End If
On Error GoTo 0
oColumn.ValueHorizontalJustification = HorizontalTextAlignmentEnum.kAlignTextCenter
oColumn.Width = 3 'cm!!!
' -
Set oDoc = oPartsList.ReferencedDocumentDescriptor.ReferencedDocument
For Each oOcc In oPartsList.ReferencedDocumentDescriptor.ReferencedDocument.ComponentDefinition.Occurrences
If oOcc.SubOccurrences.Count > 0 Then
Set oDoc = oOcc.ReferencedDocumentDescriptor.ReferencedDocument
Call SubOcc(oOcc, oDoc)
Else
sOccName = CStr(StrReverse(Split(StrReverse(oOcc.ReferencedDocumentDescriptor _
.ReferencedDocument.FullFileName), "\")(0)))
sDocname = CStr(Split(StrReverse(CStr(Split(StrReverse(oDoc.FullFileName), "\")(0))), ".")(0))
Call writeDocname(sOccName, sDocname, oPartsList)
End If
Next
End Sub
Private Function writeDocname(sOccName As String, sDocname As String, oPartsList As PartsList)
Dim oRow As PartsListRow
Dim oCell As PartsListCell
For Each oRow In oPartsList.PartsListRows
If oRow.Item("DATEINAME").Value = sOccName Then
If oRow.Item(oRow.Count).Value <> "" Then
sDocname = oRow.Item(oRow.Count).Value & ", " & sDocname
End If
oRow.Item(oRow.Count).Value = sDocname
End If
Next
End Function
Private Sub SubOcc(oOcc As ComponentOccurrence, oDoc As Document)
Dim oSubOcc As ComponentOccurrence
Dim oDoc As String
Dim sOccName As String
For Each SubOcc In oOcc.SubOccurrences
On Error Resume Next
If SubOcc.SubOccurrences.Count > 0 Then
Call SubOcc(SubOcc, oDoc)
Else
sOccName = CStr(StrReverse(Split(StrReverse(oOcc.ReferencedDocumentDescriptor.ReferencedDocument.FullFileName) _
, "\")(0)))
Set oDoc = oOcc.ReferencedDocumentDescriptor.ReferencedDocument
sDocname = CStr(Split(StrReverse(CStr(Split(StrReverse(oDoc.FullFileName), "\")(0))), ".")(0))
Call writeDocname(sOccName, sDocname, oPartsList)
End If
On Error GoTo 0
Next
End Sub