Hello All,
I want to export part files from the solidworks assembly drawing files
Need to use BOM from assembly and need to export files as a step with files having a word pipe into files and file name format is drawing number + item number for the same
I am trying for the macro and getting error for the below code
The BOM PART numbers appears is filled in the configuration user specified name
Could you please help me into this?
Sub ExportPipeParts_Stable_NoTypeMismatch()
Dim swApp As SldWorks.SldWorks
Dim swDraw As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim swTableAnn As SldWorks.TableAnnotation
Dim swFeat As SldWorks.Feature
Dim swBomTable As SldWorks.BomTableAnnotation
Dim swComp As SldWorks.Component2
Dim swPart As SldWorks.ModelDoc2
Dim partNumberCell As String
Dim itemNumber As String
Dim filePath As String
Dim exportPath As String
Dim drawingPath As String
Dim drawingNameOnly As String
Dim rowCount As Integer
Dim colCount As Integer
Dim partColIndex As Integer
Dim i As Integer
Dim foundAny As Boolean
Dim comps As Variant
Dim exportFolder As String
Set swApp = Application.SldWorks
Set swDraw = swApp.ActiveDoc
If swDraw Is Nothing Or swDraw.GetType <> swDocDRAWING Then
MsgBox "Please open a drawing.", vbExclamation
Exit Sub
End If
drawingPath = swDraw.GetPathName
If drawingPath = "" Then
MsgBox "Please save the drawing first.", vbExclamation
Exit Sub
End If
drawingNameOnly = Mid(drawingPath, InStrRev(drawingPath, "\") + 1)
drawingNameOnly = Left(drawingNameOnly, InStrRev(drawingNameOnly, ".") - 1)
' === Folder Picker ===
Dim shellApp As Object
Set shellApp = CreateObject("Shell.Application")
Dim folder As Object
Set folder = shellApp.BrowseForFolder(0, "Select folder to save STEP files", 0, 0)
If Not folder Is Nothing Then
exportFolder = folder.Self.Path
Else
MsgBox "Folder selection cancelled.", vbExclamation
Exit Sub
End If
If Right(exportFolder, 1) <> "\" Then exportFolder = exportFolder & "\"
' ===============================
' Search drawing views for BOM table
Set swView = swDraw.GetFirstView
Do While Not swView Is Nothing
Set swTableAnn = swView.GetFirstTableAnnotation
Do While Not swTableAnn Is Nothing
If swTableAnn.Type = swTableAnnotation_BillOfMaterials Then
' Locate BOM table via feature loop
Set swFeat = swDraw.FirstFeature
Do While Not swFeat Is Nothing
If swFeat.GetTypeName2 = "BomFeat" Then
Set swBomTable = swFeat.GetSpecificFeature2
If Not swBomTable Is Nothing Then Exit Do
End If
Set swFeat = swFeat.GetNextFeature
Loop
If swBomTable Is Nothing Then
MsgBox "Unable to access BOM data from drawing.", vbCritical
Exit Sub
End If
rowCount = swTableAnn.RowCount
colCount = swTableAnn.ColumnCount
' Find "PART NUMBER" column
partColIndex = -1
For i = 0 To colCount - 1
If Trim(UCase(swTableAnn.Text(0, i))) = "PART NUMBER" Then
partColIndex = i
Exit For
End If
Next i
If partColIndex = -1 Then
MsgBox "'PART NUMBER' column not found in BOM.", vbCritical
Exit Sub
End If
' Loop BOM rows
For i = 1 To rowCount - 1
partNumberCell = Trim(swTableAnn.Text(i, partColIndex))
itemNumber = Trim(swTableAnn.Text(i, 0))
If InStr(UCase(partNumberCell), "PIPE") > 0 Then
comps = swBomTable.GetComponents(i)
If Not IsEmpty(comps) Then
Set swComp = comps(0)
If Not swComp Is Nothing Then
filePath = swComp.GetPathName
If filePath <> "" Then
Set swPart = swApp.OpenDoc6(filePath, swDocPART, swOpenDocOptions_Silent, "", 0, 0)
If Not swPart Is Nothing Then
Dim cleanItemNo As String
cleanItemNo = Replace(Replace(itemNumber, ",", ""), " ", "")
exportPath = exportFolder & drawingNameOnly & "_" & cleanItemNo & ".STEP"
swPart.Extension.SaveAs exportPath, swSaveAsSTEP, swSaveAsOptions_Silent, Nothing, 0, 0
swApp.CloseDoc swPart.GetTitle
foundAny = True
End If
End If
End If
End If
End If
Next i
End If
Set swTableAnn = swTableAnn.GetNext
Loop
Set swView = swView.GetNextView
Loop
If foundAny Then
MsgBox "PIPE parts exported successfully.", vbInformation
Else
MsgBox "No PIPE parts found in BOM.", vbInformation
End If
End Sub