+ Reply to Thread
Results 1 to 2 of 2

VBA to Export and Save as PDF

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-20-2014
    Location
    Ireland
    MS-Off Ver
    Excel 2013
    Posts
    108

    VBA to Export and Save as PDF

    Hi All,

    As the title suggests, I am looking to export and save an excel sheet as a PDF to a specific file location.

    I am currently using the code below, which works perfect, however I would like to know if there is a way to redirect the save to open a "save as" box should an error occur trying to save to the location listed

    Sub PDF()
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=Environ("USERPROFILE") & "\SharePoint\OFS Operations - Job Briefing Recor\\" & Cells(2, 1).Value & " - " & Cells(2, 2).Value & " - " & Cells(3, 2).Value & ".pdf", _
    OpenAfterPublish:=True
    End Sub
    The reason I ask this, is I am saving the PDF to a Synced SharePoint folder on the users computer, should this fail to sync or be removed by mistake, I dont want my code to return an error, and instead redirect to a manual save as box

    Thanks in advance to any replies

  2. #2
    Valued Forum Contributor
    Join Date
    08-22-2011
    Location
    Auckland
    MS-Off Ver
    Excel 2019
    Posts
    716

    Re: VBA to Export and Save as PDF

    It's 1:30am & here I am looking at macro code, I modified some code I already had & tested it as best I can. Hopefully it's what you're after

    Option Explicit
    
    Sub SaveAsMacro()
    
    Dim strSaveAsName As String
    
    On Error GoTo ErrorOut1
    
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=Environ("USERPROFILE") & "\SharePoint\OFS Operations - Job Briefing Recor\\" & Cells(2, 1).Value & " - " & Cells(2, 2).Value & " - " & Cells(3, 2).Value & ".pdf", _
    OpenAfterPublish:=True
    
    On Error GoTo 0
    
    GoTo ExitOut
    
    ErrorOut1:
    
    On Error GoTo ExitOut ' If an error occurs in the SaveAs routine, end the macro
    
    '   Promp user for save particulars
            strSaveAsName = Application.GetSaveAsFilename(FileFilter:="PDF Files (*.pdf), *.pdf", _
                        Title:="Select Folder And FileName To Save")
                        
        If strSaveAsName <> "False" Then ' Check user has entered save particulars
            
    '   Create the PDF & save it to SavedFileName variable destination
            With ActiveSheet
                .ExportAsFixedFormat Type:=xlTypePDF, Filename:=strSaveAsName, _
                    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, OpenAfterPublish:=False
            End With
                
        Else ' Cancel button was pressed
            GoTo ExitOut
        Exit Sub
            
        End If
    
        GoTo ExitOut
    
    ExitOut:
    On Error GoTo 0
    Exit Sub
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] PivotTable need to export or save to PDF
    By Epscan in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-29-2015, 02:52 PM
  2. [SOLVED] Cannot Save Export Specification
    By ffffloyd in forum Access Tables & Databases
    Replies: 1
    Last Post: 04-16-2012, 10:46 PM
  3. export a range and save as .jpg
    By skooter2k5 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 06-04-2010, 09:20 AM
  4. want to export/save column as....
    By Fredrik in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 11-11-2007, 12:27 PM
  5. [SOLVED] On export option where you want to save
    By Muaitai in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-16-2006, 11:35 PM
  6. Export with save as dialog
    By tommy_gtr in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-22-2005, 11:22 AM
  7. Export and save
    By ethereal.hell@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-17-2005, 09:25 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1