Export renamed PDFs to the corresponding folder

Hi all

I haven't found any similar posts in the forum so I'm taking the liberty of publishing it.

Having no skills in VBA I found a PDF export macro on the internet, and I want to modify it.

Currently, it automatically saves the MEP as a PDF in the folder where the file is located. Our MEP files are located in a library in a network and are called "902xxxx"

I would need 2 additional features:

  • Increment the new "room plan" PDF file at the beginning of the name.
    = "room plan 902xxxx"

  • Save the PDF file in a location other than the one in the MEP: the PDFs are stored in another directory and in subfolders named "902xx" but in the same network.

Example:

I have a "9021325" plan in U: Studies

I want to save it with the macro in PDF so that they end up in the U folder: Dosfab / 90213 by renaming themselves "room plan 9021325"

Below is the macro:

Dim swApp As Object

Dim Part As Object

Dim boolstatus As Boolean

Dim longstatus As Long, longwarnings As Long

Dim FeatureData As Object

Dim Feature As Object

Dim Component As Object

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

Path = Part.GetPathName

Part.SaveAs2 Left(Path, (Len(Path) - 6)) & " PDF ", 0, True, False

Set Part = Nothing

swApp.CloseDoc Path

End Sub

The goal is to reduce the time spent overwriting PDF files and finding them in the subfolders associated with their name.

I don't know if it's easily doable.

Hello @Tom_VITRE and welcome.

The search on this forum is not ideal... and yet by just writing " PDF " we come across some interesting results.
whose discussion:

or even better:

interesting for its didactic side.
@sbadenis offers a well-commented macro that you should be able to modify without too much difficulty: (Here is the copy of the code).

' On définit les variables nécessaires
Dim swApp As Object
Dim swModel As SldWorks.ModelDoc2
Dim Path As String
Dim PathDesktop As String
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swExportPDFData As SldWorks.ExportPdfData
Dim nFileName As String
Dim nFileName2 As String
Dim FileName As String
Dim boolstatus As Boolean
Dim lErrors As Long
Dim lWarnings As Long

'Dim lgFichier As Integer

Sub main()
' On se raccroche à Solidworks
Set swApp = Application.SldWorks

' On récupère le document actif dans Solidworks
Set swModel = swApp.ActiveDoc

' On vérifie qu'un document est bien ouvert dans Solidworks
If swModel Is Nothing Then
    MsgBox "Un document doit être actif dans Solidworks.", vbCritical
    End
End If

' On vérifie que le document ouvert dans Solidworks est un plan
If swModel.GetType <> swDocDRAWING Then
    MsgBox "Le document actif dans Solidworks doit être un plan.", vbCritical
    End
End If

' On vérifie que le plan ouvert dans Solidworks est bien enregistré
If swModel.GetPathName = "" Then
    swModel.Save
End If

' On récupère le chemin du dossier d'enregistrement du plan
Path = swModel.GetPathName

    
'on récupère le chemin complet sans le nom de fichier
FilePath = Left(Path, InStrRev(Path, "\"))
'Debug.Print "Filepath:" + Filepath
    
        
'on récupère le nom du fichier sans l'extension
FileName = Mid(Path, Len(FilePath) + 1, Len(Path) - (7 + Len(FilePath)))
' on affiche la variable dans la fenêtre Exécution
Debug.Print "FileName=" & FileName

' on affiche la variable dans la fenêtre Exécution
Debug.Print "Path=" & Path
lgFichier = InStrRev(Path, "\", -1, vbTextCompare) - 1
If lgFichier > 0 Then
    Path = Left(Path, lgFichier)
End If

' On récupère le chemin du bureau Windows
PathDesktop = Environ("USERPROFILE") & "\Desktop"
' on affiche la variable dans la fenêtre Exécution
Debug.Print "PathDesktop=" & PathDesktop

' On définit les paramètres d'export en PDF
Set swModelDocExt = swModel.Extension
Set swExportPDFData = swApp.GetExportFileData(1)
swExportPDFData.ViewPdfAfterSaving = False

' On définit le chemin et le nom du premier fichier pdf à créer
'nFileName = Left(Path, Len(Path) - 6) & "PDF" 'Retrait de l'extension SW en gardant le point et ajout PDF
nFileName = FilePath & FileName & ".pdf"

' on affiche la variable dans la fenêtre Exécution
Debug.Print "nFileName=" & nFileName
' On sauvegarde le premier fichier pdf
boolstatus = swModelDocExt.SaveAs(nFileName, 0, 0, swExportPDFData, lErrors, lWarnings)

' On définit le chemin et le nom du deuxième fichier pdf à créer
'nFileName2 = PathDesktop & Left(nFileName2, Len(nFileName2) - 6) & "PDF"
nFileName2 = PathDesktop & "\" & FileName & ".pdf"
' on affiche la variable dans la fenêtre Exécution
Debug.Print "nFileName2=" & nFileName2
' On sauvegarde le deuxième fichier pdf
boolstatus = swModelDocExt.SaveAs(nFileName2, 0, 0, swExportPDFData, lErrors, lWarnings)

End Sub

Kind regards.

4 Likes

Thank you for your answer, I'll take the time to look at it and adapt it with my settings

1 Like