Assign a defined material to a part from an XLSX file

Assign a defined material to a part from an XLSX file

max.baumann07
Enthusiast Enthusiast
183 Views
2 Replies
Message 1 of 3

Assign a defined material to a part from an XLSX file

max.baumann07
Enthusiast
Enthusiast

I have a linked XLSX file containing parameters for a part. The part could also be made from a different material. I am using a custom material library called "Materials_SAP," and I want to populate the material column in the XLSX with a dropdown list of materials from this library.

The idea is that the user can select the corresponding material from the library in the dropdown list, and once the material is selected, it will be assigned to the part.

Is there a way to achieve this, where the material selected in the XLSX file is automatically assigned to the part afterward?


0 Likes
184 Views
2 Replies
Replies (2)
Message 2 of 3

marcin_otręba
Advisor
Advisor

if you have name of material then yes without problem :

ThisApplication.ActiveDocument.ActiveMaterial =  ThisApplication.ActiveDocument.MaterialAssets("name from xls")

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 3 of 3

max.baumann07
Enthusiast
Enthusiast

Thanks.
I created now a solution.:

Try
' Excel-Anwendung starten und Datei öffnen
excelApp = CreateObject("Excel.Application")
excelWorkbook = excelApp.Workbooks.Open("C:\XXXXXXX.xlsx")
excelSheet = excelWorkbook.Sheets(1)

' Lesen des Materialnamens aus der entsprechenden Zelle (Zeile 8, Spalte 2)
materialName = excelSheet.Cells(4, 2).Value.ToString()

' Excel-Anwendung schließen
excelWorkbook.Close(False)
excelApp.Quit()
Catch ex As Exception
MessageBox.Show("Fehler beim Zugriff auf die Excel-Datei: " & ex.Message)
End Try

' Zugriff auf das aktive Dokument
Dim doc As Document = ThisApplication.ActiveDocument

If doc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
Dim compDef As PartComponentDefinition = doc.ComponentDefinition

' Zugriff auf die Materialien des aktiven Dokuments
Dim material As Material = Nothing

' Durchsuchen der Materialien im aktuellen Dokument
For Each mat As material In doc.Materials
If mat.Name.Trim().ToLower() = materialName.Trim().ToLower() Then
material = mat
Exit For
End If
Next

' Material zuweisen, wenn gefunden
If material IsNot Nothing Then
' Zuweisung des Materials an die PartComponentDefinition
compDef.Material = material
MessageBox.Show("Material '" & materialName & "' wurde dem Modell zugewiesen.")
Else
MessageBox.Show("Material '" & materialName & "' im aktuellen Dokument nicht gefunden.")
End If
Else
MessageBox.Show("Das Dokument ist kein Part-Dokument.")
End If

0 Likes