Macro Save to PDF In Excel

JERRY89
4StarLounger
Posts: 516
Joined: 21 Feb 2016, 02:52

Macro Save to PDF In Excel

Post by JERRY89 »

Dear All,

I do have a macro command that i record but the problem is i do want a dynamic file save location and i want the file name follow my excel file name because i usually will make a few copy of this excel file to be created as pdf file with different file name.Is there a solution for the below macro :

Sub PDF()
'
' PDF Macro
'

'
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"P:\JERRY\Payment advice-15th\Payment Advice_2016-17\2016.10\STM Tech.pdf"
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True
Application.WindowState = xlMinimized
End Sub

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Macro Save to PDF In Excel

Post by HansV »

Do you want to save the PDF file in the same folder as the Excel workbook?
Best wishes,
Hans

JERRY89
4StarLounger
Posts: 516
Joined: 21 Feb 2016, 02:52

Re: Macro Save to PDF In Excel

Post by JERRY89 »

Hi Hans,
Yup. It's better if can save in the same place with the excel file.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Macro Save to PDF In Excel

Post by Rudi »

Give this a try.
(Note: I could not test it as I don't have the PDF add-in installed on in Office).

Code: Select all

Sub SaveToPDF()
Dim wbA As Workbook
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
    
    On Error GoTo errHandler
    Set wbA = ActiveWorkbook

    'get active workbook folder, if saved
    strPath = wbA.Path
    If strPath = "" Then
        strPath = Application.DefaultFilePath
    End If
    strPath = strPath & "\"

    'create default name for savng file
    strFile = Split(ActiveWorkbook.Name, ".")(0) & ".pdf"
    strPathFile = strPath & strFile

    'export to PDF
    wbA.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=strPathFile, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=False
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
           & vbCrLf _
           & strPathFile, vbInformation

exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create PDF file", vbExclamation
    Resume exitHandler
End Sub
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

JERRY89
4StarLounger
Posts: 516
Joined: 21 Feb 2016, 02:52

Re: Macro Save to PDF In Excel

Post by JERRY89 »

Hi Rudi,

Thanks Ya..It work great..