Need to edit PDF macro

Hi all

I got a macro on the forum to save my Solidworks MEPs in PDF, my problem is that the macro changes the name of the PDF and I wouldn't like that.

I've already deleted the lines I don't need, but it's not good yet...

Could someone please modify the macro so that the file name remains like the MEP, I would like to keep the 2 save locations.

The macro:

' We define the necessary variables
Dim swApp As Object
Dim swModel As SldWorks.ModelDoc2
Dim swCustProp As CustomPropertyManager
Dim valOut1 As String
Dim valOut2 As String
Dim valOut3 As String
Dim resolvedValOut1 As String
Dim resolvedValOut2 As String
Dim resolvedValOut3 As String
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 boolstatus As Boolean
Dim lErrors As Long
Dim lWarnings As Long
Dim lgFile As Integer

Sub main()
' We cling to Solidworks
Set swApp = Application.SldWorks

' We retrieve the active document in Solidworks
Set swModel = swApp.ActiveDoc

' We check that a document is open in Solidworks
If swModel Is Nothing Then
    MsgBox "A document must be active in Solidworks.", vbCritical
    End
End If

' We verify that the document opened in Solidworks is a plan
If swModel.GetType <> swDocDRAWING Then
    MsgBox "The active document in Solidworks must be a plan.", vbCritical
    End
End If

' We check that the open plan in Solidworks is saved
If swModel.GetPathName = "" Then
    swModel.Save
End If

' We retrieve the path of the plan registration folder
Path = swModel.GetPathName
lgFile = InStrRev(Path, "\", -1, vbTextCompare) - 1
If lgFile > 0 Then
    Path = Left(Path, lgFile)
End If

' We recover the path of the Windows desktop
PathDesktop = About("USERPROFILE") & "\Desktop"

' We define the export parameters in PDF
Set swModelDocExt = swModel.Extension
Set swExportPDFData = swApp.GetExportFileData(1)
swExportPDFData.ViewPdfAfterSaving = False

' We define the path and the name of the first pdf file to be created
nFileName = Path & "\" & resolvedValOut1 & " - " & resolvedValOut2 & " - Ind." & resolvedValOut3 & " - " & dateNow & ".PDF"

' Save the first pdf file
boolstatus = swModelDocExt.SaveAs(nFileName, 0, 0, swExportPDFData, lErrors, lWarnings)

' We define the path and the name of the second pdf file to be created
nFileName2 = PathDesktop & "\" & resolvedValOut1 & " - " & resolvedValOut2 & " - Ind." & resolvedValOut3 & " - " & dateNow & ".PDF"

' We save the second pdf file
boolstatus = swModelDocExt.SaveAs(nFileName2, 0, 0, swExportPDFData, lErrors, lWarnings)

End Sub

Thank you in advance.

 

Hello, test this one.

Small precision, the PDF and DWG will save in the same place of the last recording. In plain English, before running the  Marco, save your plan  by doing file=> save as and then run the macro

May the force be with you


macro_pdf_-dwg.swp
3 Likes

You obviously don't miss much, just add a debug.print to see the values of your variables:

 

' On définit les variables nécessaires
Dim swApp As Object
Dim swModel As SldWorks.ModelDoc2
Dim swCustProp As CustomPropertyManager
Dim valOut1 As String
Dim valOut2 As String
Dim valOut3 As String
Dim resolvedValOut1 As String
Dim resolvedValOut2 As String
Dim resolvedValOut3 As String
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 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
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 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 = Path & "\" & resolvedValOut1 & " - " & resolvedValOut2 & " - Ind." & resolvedValOut3 & " - " & dateNow & ".PDF"
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 & "\" & resolvedValOut1 & " - " & resolvedValOut2 & " - Ind." & resolvedValOut3 & " - " & dateNow & ".PDF"
Debug.Print "nFileName2=" & nFileName2

' On sauvegarde le deuxième fichier pdf
boolstatus = swModelDocExt.SaveAs(nFileName2, 0, 0, swExportPDFData, lErrors, lWarnings)

End Sub

 

Then you edit your macro, you add the execution window, you run your macro and you look at the value of your property and you will see if there is an error in your path (for me the problem is in the concatenation of your variable Filename1 and 2)

If the value of your variable is not good or contains forbidden characters, it does not record.

In addition, in your concatenation, you use resolvedValOut1, 2 and 3 variables, declared as they should be in string, but which are not found in your macro, so empty!

 

 

2 Likes

Hello

From what I understand the need, I think the code below meets it.

' On définit les variables nécessaires
Dim swApp As Object
Dim swModel As SldWorks.ModelDoc2
Dim swCustProp As CustomPropertyManager
Dim valOut1 As String
Dim valOut2 As String
Dim valOut3 As String
Dim resolvedValOut1 As String
Dim resolvedValOut2 As String
Dim resolvedValOut3 As String
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 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
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 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

' 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 = Mid(Path, InStrRev(Path, "\") + 1) 'purge le chemin d'accès du fichier d'origine
nFileName2 = PathDesktop & left(nFileName2, len(nFileName2)-6) & "PDF"

' On sauvegarde le deuxième fichier pdf
boolstatus = swModelDocExt.SaveAs(nFileName2, 0, 0, swExportPDFData, lErrors, lWarnings)

End Sub

 

2 Likes

@ OBI WAN
Thanks for the macro, I'll keep it under my belt.

@Cyril.f

Macro not working :-(

@ sbadenis

"In addition, in your concatenation, you use variables resolvedValOut1, 2 and 3, declared as it should be in string, but which are not found in your macro, so empty!"

Yes its variables are not useful to me, I had removed the functions in the macro above, but left those, because I didn't know what to replace to save with the same name as my MAP.

But suddenly, your macro doesn't work either.

2 Likes

Hello

I have a functional macro to make the drawings in PDF

I'm sending it to you, it's currently running on solidworks 2016

I'll let you modify it if necessary

 


enregistrement_pdf.swp
1 Like

If you put debug.print and you look at the values you could have found the error of @Cyril.f by searching a little.

Here is the functional code with the debug.print, look at what it looks like in the execution window, then if you want you can comment them or not because it doesn't really weigh down a small macro like this one.

' 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

 

 

2 Likes

@sbadenis

The macro is perfect as well, thank you very much!

Hello, could you please send me this macro?

The code for the macro is available in the post that solved the topic.
Just create a new macro and paste the code into it.

If necessary, recreate a topic (with the related topic) rather than digging up an old topic.

3 Likes