Hide Table of Contents

Save File as PDF Example (VBA)

This example shows how to export the specified sheet in a drawing document to a PDF file.

' Preconditions:
' 1. Specified drawing document to open exists.
' 2. The folder (c:\test) to which to save
'    the PDF file exists. If it does not exist,
'    create it or change the path to one that
'    exists on your system.
' Postconditions:
' 1. Specified drawing document is opened.
' 2. All but the last drawing sheet are saved to an array.
' 3. The array of drawing sheets are saved to a PDF
'    file called foodprocessor.pdf.
' 4. foodprocessor.pdf opens.

Option Explicit

    Dim swApp               As SldWorks.SldWorks
    Dim swModel             As SldWorks.ModelDoc2
    Dim swModelDocExt       As SldWorks.ModelDocExtension
    Dim swExportPDFData     As SldWorks.ExportPdfData
    Dim boolstatus          As Boolean
    Dim filename            As String
    Dim lErrors             As Long
    Dim lWarnings           As Long
    Dim strSheetName(4)     As String
    Dim varSheetName        As Variant

Sub main()

    ' Path to which to save PDF file of drawing
    filename = "C:\foodprocessor.PDF"

    Set swApp = Application.SldWorks
    swApp.Visible = True

    ' Open specified drawing
    Set swModel = swApp.OpenDoc6("c:\Program Files\SolidWorks Corp\SolidWorks\samples\tutorial\advdrawings\foodprocessor.slddrw", swDocDRAWING, swOpenDocOptions_Silent, "", lErrors, lWarnings)
    Set swModelDocExt = swModel.Extension
    Set swExportPDFData = swApp.GetExportFileData(1)

    ' Names of the sheets
    strSheetName(0) = "Sheet1"
    ' strSheetName(1) = "Sheet2"
    ' strSheetName(2) = "Sheet3"
    ' strSheetName(3) = "Sheet4"

    varSheetName = strSheetName

    If swExportPDFData Is Nothing Then MsgBox "Nothing"
    boolstatus = swExportPDFData.SetSheets(swExportData_ExportSpecifiedSheets, varSheetName)
    swExportPDFData.ViewPdfAfterSaving = True

    boolstatus = swModelDocExt.SaveAs(filename, 0, 0, swExportPDFData, lErrors, lWarnings)

End Sub

Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

Subject:   Feedback on Help Topics
Page:   Save File as PDF Example (VBA)
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:


We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again

Web Help Content Version: API Help (English only) 2013 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.