Macro export PDF & DXF active page

Hello,
Not having a lot of knowledge in macro and VBA code, I am looking to export the current page in PDF and DXF with the following file name:
File name Setting plan_Indice page_Nom révision_Numéro of the day's page_Date.

After multiple searches and attempts to write a macro, I arrived at a result that does not satisfy me, I cannot retrieve all the desired data: Number and name of the page.
Can anyone help me?

Attached is my code:

Option Explicit
Dim swApp               As SldWorks.SldWorks
Dim swModel             As SldWorks.ModelDoc2
Dim swDrawModel         As SldWorks.ModelDoc2
Dim swDraw              As SldWorks.DrawingDoc
Dim swCustProp          As CustomPropertyManager
Dim swView              As SldWorks.View
Dim swExportPDFData     As SldWorks.ExportPdfData
Dim sFileName           As String
Dim sPathname           As String
Dim Revision            As String
Dim resolvedRevision    As String
Dim sSheetName          As String
Dim sSheetNumber        As String
Dim dateNow             As String
Dim nErrors             As Long
Dim nWarnings           As Long

Sub main()
    Set swApp = Application.SldWorks
    Set swDrawModel = swApp.ActiveDoc
    Set swDraw = swDrawModel
        
        ' Vérifier si une mise en plan est ouverte
        If swDrawModel Is Nothing Then
                MsgBox "Il n'y a pas de document de mise en plan ouvert."
                Exit Sub
        End If

        If swDrawModel.GetType <> swDocDRAWING Then
                MsgBox "Ouvrez d'abord une mise en plan, puis réessayez "
                Exit Sub
        End If

        If swDrawModel.GetPathName = "" Then
                MsgBox "Enregistrez d'abord le dessin, puis réessayez !"
                Exit Sub
        End If

    Set swView = swDraw.GetFirstView

    Set swView = swView.GetNextView

        ' Déterminer s'il y a une vue existante
        If swView Is Nothing Then
                MsgBox "Insérez d'abord une vue, puis réessayez !"
                Exit Sub
        End If

        ' On récupère le nom du fichier de la mise en plan
    sPathname = Replace(swDraw.GetPathName, ".SLDDRW", "")     ' Récupère le nom du fichier et enlève l'extension .SLDDRW
      
        ' On récupère les valeurs qui nous intéresse dans les propriétés personnalisées du plan
    Set swCustProp = swDraw.Extension.CustomPropertyManager("")
    swCustProp.Get2 "Révision", Revision, resolvedRevision      ' Récupère l'indice de Révision du fichier Mise en Plan
    
        ' On récupère la date du jour et on la met dans un format pouvant se mettre dans le nom d'un fichier
    dateNow = Replace(Date, "/", ".")
    
        ' On récupère les données de la feuille active
    'sSheetName = swDraw.ActivateSheet.GetSheetNames    ' Récupère le nom de la feuille active
    'sSheetNumber = swDraw.GetCurrentSheet              ' Récupère le numéro de la feuille active
     
        'Obtenir et définir le nom du fichier
    sFileName = sPathname & " - " & resolvedRevision & " - " & dateNow      'Code fonctionnel mais sans le numéro et le nom de la page
    'sFileName = sPathname & " - " & resolvedRevision & " - " & sSheetNumber & " - " & sSheetName  & " - " & dateNow        'Code non-fonctionnel voulu

    Set swExportPDFData = swApp.GetExportFileData(1)

    swExportPDFData.SetSheets swExportData_ExportCurrentSheet, ""

    swExportPDFData.ViewPdfAfterSaving = False

    swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swDxfMultiSheetOption, swDxfActiveSheetOnly

        'Enregistrer au format DXF

    swDraw.Extension.SaveAs sFileName & ".DXF", 0, 0, Nothing, nErrors, nWarnings

        'Enregistrer au format PDF

    swDraw.Extension.SaveAs sFileName & ".PDF", 0, 0, swExportPDFData, nErrors, nWarnings

    End Sub

Thank you very much for your feedback.
Manu

A screenshot of a page name and its number?
N° included in the name of the sheet or in the order in which they appear?

1 Like

Hello Manu,

Here is what is used for this kind of treatment.
2026-02-20_132442
… There you go, there you go, there you go...
@+
AR.

To get the name of a sheet:

vSheetName = swDraw.GetSheetNames
'On boucle sur les feuilles
For i = 0 To UBound(vSheetName)
        sheetName = vSheetName(i)
        'Debug.Print "Nom de feuille:" & sheetName

Next i

To change the name of the sheet:

            swDraw.GetCurrentSheet.SetName "nom de la feuille"

For the N° either the i (increment), or you get the N° in the name of the sheet (if necessary)

1 Like

:smile:I thought this conversation was telling me something, it's the Macro DXF export suite sheet by sheet - #23 by Cyril_f right?
The previous proposals were not suitable?
Apart from the Leaf Name, which is a new request, have you tried to modify your macro?
That said, @sbadenis's proposals are quite relevant... :grin:

1 Like