I don't know anything about macros and I'd like to know if it's possible to make a DXF export macro that exports all the sheets of a drawing in separate files keeping the names of the sheets.
After searching on the forum I found this topic with the macro attached, which is what I'm looking for in principle, only it does PDFs and not DXFs:
Could a good soul of the forum modify this macro for me to replace the PDF with the DXF. Will the macro keep the SolidWorks "save as" options that I use when I do it manually, such as using a projection file?
The "pdf_page_par_page.swp" macro creates 1 file per drawing sheet and includes in the PDF file name the name of the drawing file + the name of the sheet
For example, if the drawing file is called "TOTO" and has 2 sheets, "PLAN" and "Rep A", the PDF macro does:
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim sPathname As String
Dim vSheetName As Variant
Dim nErrors As Long
Dim nWarnings As Long
Dim i As Long
Dim bRet As Boolean
Dim lParam As Long
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swDraw = swModel
lParam = swApp.GetUserPreferenceIntegerValue(swDxfMultiSheetOption)
'Changement paramétrage export dxf si différent de feuille active
If lParam <> 0 Then
bRet = swApp.SetUserPreferenceIntegerValue(swUserPreferenceIntegerValue_e.swDxfMultiSheetOption, swDxfMultisheet_e.swDxfActiveSheetOnly)
End If
sPathname = swModel.GetPathName
sPathname = Left(sPathname, Len(sPathname) - 7)
vSheetName = swDraw.GetSheetNames
For i = 0 To UBound(vSheetName)
bRet = swDraw.ActivateSheet(vSheetName(i))
bRet = swModel.SaveAs4(sPathname & "_" & vSheetName(i) & ".dxf", swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings)
Next i
' Retour à la Feuille 1
bRet = swDraw.ActivateSheet(vSheetName(0))
' Remise en place du paramétrage initial
bRet = swApp.SetUserPreferenceIntegerValue(swUserPreferenceIntegerValue_e.swDxfMultiSheetOption, lParam)
End Sub
Hello Sorry to dust off the subject, but I tried this macro on SW2025 and it doesn't work. This is exactly what I am looking to do as well, I do drawing of parts with multiple welded bodies. Thank you very much Manu
To be able to help you, we would need more information:
You say that the macro does not work under Sw2025, what and the error message? Is your version of Solidworks a Local or 3Dexperience version? Have you thought about loading Solidworks References into your VBA editor?
Note: The " SaveAs4 " function is considered deprecated in the Solidworks API Help. strangely it is replaced by " SaveAs3 " and their statements are slightly different: value = instance. SaveAs4(Name, Version, Options, Errors, Warnings) and value = instance. SaveAs3(Name, Version, Options, ExportData, AdvancedSaveAsOptions, Errors, Warnings)
That said, an " obsolete " function is not necessarily unusable...
But here we are attacking a subject that depends on your level in VBA programming. In your case I'm thinking more of the VBA References to add...
Hello Thank you for your feedback, when I went to check the references, the SW boxes were already all checked. I just added Microsoft Scripting Runtime, ran the macro again to try and it seems to work perfectly. I don't know if that was the problem, but in any case I'm satisfied. Thank you very much Manu
It's not strangely replaced by SaveAs3, it's just not on the same methods, SaveAs4 is part of the ModelDoc2 and SaveAs3 methods of ModelDocExtension. In response @Emmanuel_SERVEL , the Microsoft Scripting Runtime reference is normally not needed for the snippet of code I gave. The bug could come from the Left function which sometimes is not recognized, you have to use VBA as a replacement. Left.
Thank you @Cyril_f ... Enrichment of my personal culture: (and there's not even sarcasm)
ModelDoc2 : Represents the main document in SolidWorks (part, assembly, or drawing)
Allows access to basic operations on the document: open, save, rebuild, get document type, access configurations, manage views, etc So => ModelDoc2:p all the basic operations on the SolidWorks* document.
ModelDocExtension :
Is an extension of ModelDoc2, accessed through the property .Extension of a ModelDoc2* object.
Was created to add additional methods and properties when ModelDoc2 reached its capacity limit in terms of the number of methods.
Provides access to advanced or less common features, such as: custom property management, mass property calculations, bill of materials (BOM) table management, conversion operations, advanced record management (e.g., SaveAs2) So: ModelDocExtension :p advanced or specialized operations that are not available directly in ModelDoc2, accessible through the property .Extension of a ModelDoc2 object
In summary, ModelDocExtension complements ModelDoc2 and provides access to additional functionality needed for macros or advanced automations in SolidWorks