Hello
I want to make a macro for Solidworks that transforms drawing sheets into PDFs or DXFs.
PDF files will be named "filename+sheetname"
DXF files will be named in the same way.
The macro must differentiate between the sheets to be saved as PDF or DXF following the presence or absence of the word DXF in the name of the sheet.
Below is the macro created. Unfortunately I get the error "compilation error: User-defined type not defined".
With "swExportDXFData As SldWorks.ExportDxfData" highlighted in blue.
I think I understand that there is a piece of data missing in the references but I am lost.
I'm on SolidWorks 2022.
Could anyone help me?
Option Explicit
Sub ExportPDFandDXF()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swSheet As SldWorks.Sheet
Dim swExportData As SldWorks.ExportPdfData
Dim swExportDXFData As SldWorks.ExportDxfData
Dim sheetName As String
Dim fileName As String
Dim filePath As String
Dim numSheets As Integer
Dim i As Integer
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then
MsgBox "Aucun document ouvert dans SolidWorks."
Exit Sub
End If
If Not swModel.GetType = swDocumentTypes_e.swDocDRAWING Then
MsgBox "Ce n'est pas un document de mise en plan."
Exit Sub
End If
Set swDraw = swModel
numSheets = swDraw.GetSheetCount
fileName = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, ".") - 1)
filePath = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, "\"))
For i = 1 To numSheets
Set swSheet = swDraw.Sheet(i)
sheetName = swSheet.GetName
If InStr(sheetName, "DXF") > 0 Then
Set swExportDXFData = swApp.GetExportFileData(1)
swExportDXFData.fileName = fileName & sheetName & ".dxf"
swExportDXFData.SetSheets swSheet.GetName
swModel.Extension.ExportToDWGDXF swExportDXFData
Else
Set swExportData = swApp.GetExportFileData(swExportPdfData)
swExportData.fileName = fileName & sheetName & ".pdf"
swExportData.SetSheets swSheet.GetName
swModel.Extension.SaveAs swExportData
End If
Next i
MsgBox "Exportation terminée."
End Sub