Search code examples
vbaapipdfsave-assolidworks

VBA (API) save SOLIDWORKS drawing as PDF in a set location


I am trying to figure out how to modify my PDF saving macro so that instead of saving the PDF in the same folder as drawing, it would save it to a set location that's written out in macro.

The code I am working with is:

Sub Save_PDF()

'Declare variables
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swExportPDFData As SldWorks.ExportPdfData
Dim strFilename As String
Dim status As Boolean
Dim errors As Long, warnings As Long

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

'Export to PDF if it is a drawing
If (swModel.GetType = swDocDRAWING) Then

strFilename = swModel.GetPathName
strFilename = Left(strFilename, Len(strFilename) - 6) & "pdf"

Set swExportPDFData = swApp.GetExportFileData(1)
swModel.Extension.SaveAs strFilename, 0, 0, swExportPDFData, 0, 0
End If

End Sub

Would someone, please, help me with this one?


Solution

  • You just need to get the name of the file then concatenate with the rest of the path, and remove the unnecessary lines, like this:

    Option Explicit
    Sub Save_PDF()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim strFilename As String
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    If swModel.GetType <> swDocDRAWING Then MsgBox ("Error: Not a drawing"): Exit Sub
    strFilename = swModel.GetPathName
    strFilename = "G:\45 Design\" & Mid(strFilename, InStrRev(strFilename, "\") + 1, InStrRev(strFilename, ".") - InStrRev(strFilename, "\")) & "pdf"
    MsgBox "Save path : " & strFilename
    swModel.Extension.SaveAs strFilename, 0, 0, Nothing, 0, 0
    End Sub