Save worksheet as pdf with date as name

fattyhuman

New Member
Joined
Jan 26, 2025
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I am a complete novice at VBA's so apologies straight up. I thought I had this sorted but cannot get it to compile and work correctly.
I am trying to write a macro that will allow me to print a worksheet in an excel file as a pdf and then save it to a pre determined folder with the file name of the printed work sheet being "Daily Report yyyy-mm-dd"

I then need to send this sheet as an email, I have this part all but sorted, but just cannot get the saving right.

Any help would be greatly appreciated.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
The following will allow you to preselect the folder where you want the pdf to be saved. If you truly want the location "hard coded" let me know.

VBA Code:
Option Explicit

Sub PrintTableToPDF()
Dim strfile As String
Dim myfile As Variant
Dim strTable As String, r As Range
Application.ScreenUpdating = False


        'Prompt for save location
        strfile = "Daily Report " & _
        Format(Now(), "yyyy-mm-dd") & _
        ".pdf"
        strfile = ThisWorkbook.Path & "\" & strfile
        
        myfile = Application.GetSaveAsFilename _
        (InitialFileName:=strfile, _
        FileFilter:="PDF Files (*.pdf), *.pdf", _
        Title:="Select Folder and File Name to Save as PDF")
        
            If myfile <> "False" Then 'save as PDF
                ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=myfile, Quality:=xlQualityStandard, _
                IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
            Else
                MsgBox "No File Selected. PDF will not be saved", vbOKOnly, "No File Selected"
            End If

Application.DisplayAlerts = False

LetsContinue:
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With
Exit Sub
End Sub
 
Upvote 0
The following will allow you to preselect the folder where you want the pdf to be saved. If you truly want the location "hard coded" let me know.

VBA Code:
Option Explicit

Sub PrintTableToPDF()
Dim strfile As String
Dim myfile As Variant
Dim strTable As String, r As Range
Application.ScreenUpdating = False


        'Prompt for save location
        strfile = "Daily Report " & _
        Format(Now(), "yyyy-mm-dd") & _
        ".pdf"
        strfile = ThisWorkbook.Path & "\" & strfile
       
        myfile = Application.GetSaveAsFilename _
        (InitialFileName:=strfile, _
        FileFilter:="PDF Files (*.pdf), *.pdf", _
        Title:="Select Folder and File Name to Save as PDF")
       
            If myfile <> "False" Then 'save as PDF
                ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=myfile, Quality:=xlQualityStandard, _
                IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
            Else
                MsgBox "No File Selected. PDF will not be saved", vbOKOnly, "No File Selected"
            End If

Application.DisplayAlerts = False

LetsContinue:
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With
Exit Sub
End Sub
If it can be hard coded so I can eliminate people saving it wherever they want.
The code is awesome thanks.
 
Upvote 0
This code saves the pdf to the users desktop in a folder named "test" :

VBA Code:
Sub PrintTableToPDF()
Dim strfile As String
Dim strTable As String, r As Range
Application.ScreenUpdating = False

        'Fixed save location
        strfile = Environ("UserProfile") & "\OneDrive\Desktop\Test\Daily Report " & _
        Format(Now(), "yyyy-mm-dd") & _
        ".pdf"
        
        'Save as PDF
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strfile, Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
        
Application.DisplayAlerts = False

LetsContinue:
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With
Exit Sub
End Sub
 
Upvote 0

Forum statistics

Threads
1,226,529
Messages
6,191,587
Members
453,666
Latest member
madelineharris

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top