Macro Save to PDF with date stamp

ReaSCH

New Member
Joined
Jun 25, 2013
Messages
5
Dear all,

I unfortunately have no clue how to build macros. I need a Save to PDF macro that saves my dashboard following the document path, takes the worksheet name and puts the date next to the worksheet name when saving the PDF.
I have been looking through multiple blogs and forums, but I seem not able to put the right bits and pieces together.

My brother built the following macro for me, which already does 80 % of what I would need.

Sub Save_as_pdf()

Dim FSO As Object
Dim s(1) As String
Dim sNewFilePath As String
Dim cut_date As String
Dim add_date As String
Set FSO = CreateObject("Scripting.FileSystemObject")
s(0) = ThisWorkbook.FullName
cut_date = Left(s(0), Len(s(s0)) - 5)
add_date = cut_date & "_" & Format(Date$, "dd.mm.yy")

If FSO.FileExists(s(0)) Then
'//Change Excel Extension to PDF extension in FilePath
s(1) = FSO.GetExtensionName(add_date)
If s(1) <> "" Then
s(1) = "." & s(1)
sNewFilePath = Replace(add_date, s(1), ".pdf")
'//Export to PDF with new File Path
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
filename:=sNewFilePath, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End If
Else
'//Error: file path not found
MsgBox "Error: this workbook may be unsaved. Please save and try again. A team of highly traind monkeys is working on a solution for this problem!"
End If

Set FSO = Nothing

End Sub

However, in the saved document name, the year stamp (.yy) is not visible. Other than that I would need to save the file with the worksheet name and not the workbook name, since I have more than one dashboard in the same file. Would it also be possible to add a time stamp next to the date stamp in the saved document name?

If this helps I could also include the document name and date/timestamp from cell values. (Cell: B2 and the date/timestamp Cell; BI2) as I have seen many solutions trying it that way.

I really appreciate your help!
Cheers,
Rea
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi,

Try this instead:
Code:
Sub Save_ActSht_as_Pdf()
    ' Saves active sheet as PDF file.

    Dim Name As String
    
    Name = ThisWorkbook.Path & "\" & ActiveSheet.Name & " " & _
        Format(Now(), "dd.mm.yy hh.mm") & ".pdf"
    
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Name, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub

Succes,

Paul
 
Upvote 0
Dear Paul!

Thank you so much. You made my day... :)
The macro does exactly what I wanted and even looks a lot better.
Thanks!
Rea
 
Upvote 0
I know this is an old post but, is it possible to set it to all sheets in the file instead of the active sheet?
 
Upvote 0
Hi,

Try this instead:
Rich (BB code):
Sub Save_ActSht_as_Pdf()
    ' Saves active sheet as PDF file.

    Dim Name As String
    
    Name = ThisWorkbook.Path & "\" & ActiveSheet.Name & " " & _
        Format(Now(), "dd.mm.yy hh.mm") & ".pdf"
    
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Name, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub

Succes,

Paul

Cheers DeBeuz

I changed the highlighted text to suit my requirements, works a treat!

ThisWorkbook... to required directory path
Now() changed to Range()
 
Upvote 0

Forum statistics

Threads
1,216,007
Messages
6,128,244
Members
449,435
Latest member
Jahmia0616

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