Hallo, ich benutze VBA in Excel, um eine Materialliste zu erstellen, ohne es manuell in SolidWorks zu machen. Ich habe bereits ein Makro, das zum Erstellen von Montageplänen funktioniert, und möchte daher gerne eine Stückliste erstellen, aber ich drehe mich im Kreis (Fehlerzeile: Wenn nicht swView.GetModel dann nichts ist):
Option Explicit
Sub InsererBOMAssemblage()
Dim swApp As Object
Dim swModel As Object
Dim swDraw As Object
Dim swSheet As Object
Dim swView As Object
Dim swBOM As Object
Dim cheminPlan As String
Dim cheminBOM As String
Dim erreur As Long
Dim avertissement As Long
cheminPlan = "chemin du plan d'assemblage" ' <-- modifier selon ton fichier"
cheminBOM = "chemin de la nomenclature" ' <-- modèle de nomenclature
On Error Resume Next
Set swApp = GetObject(, "SldWorks.Application")
If swApp Is Nothing Then
Set swApp = CreateObject("SldWorks.Application")
If swApp Is Nothing Then
MsgBox "Impossible de lancer SolidWorks.", vbCritical
Exit Sub
End If
swApp.Visible = True
End If
On Error GoTo 0
Set swModel = swApp.OpenDoc6(cheminPlan, 3, 0, "", erreur, avertissement)
If swModel Is Nothing Then
MsgBox "Impossible d'ouvrir le plan : " & cheminPlan & vbCrLf & _
"Erreur = " & erreur & ", Avertissement = " & avertissement, vbCritical
Exit Sub
End If
Set swDraw = swModel
Set swSheet = swDraw.GetCurrentSheet
If swSheet Is Nothing Then
MsgBox "Impossible de récupérer la feuille du plan.", vbCritical
Exit Sub
End If
Set swView = swDraw.GetFirstView
Do While Not swView Is Nothing
If Not swView.GetModel Is Nothing Then
Exit Do
End If
Set swView = swView.GetNextView
Loop
If swView Is Nothing Then
MsgBox "Aucune vue valide (pièce ou assemblage) trouvée. Ajoute une vue dans le plan.", vbCritical
Exit Sub
End If
On Error Resume Next
Set swBOM = swDraw.InsertBomTable(swView, 0.1, 0.1, 0, cheminBOM)
On Error GoTo 0
If swBOM Is Nothing Then
MsgBox "Erreur lors de l'insertion de la nomenclature. Vérifie la vue et le chemin du modèle BOM.", vbCritical
Else
swDraw.EditRebuild3
MsgBox "Nomenclature insérée avec succès !", vbInformation
End If
End Sub