Add date to pdf output

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,734
Office Version
  1. 2007
Platform
  1. Windows
Hi,
Currently when i save my excel sheet as pdf i use this line of code.
VBA Code:
strFileName = "C:\Users\Ian\Desktop\GRASS CUTTING\CURRENT GRASS SHEETS\PAUL GODDARD\" & "INVOICE " & Range("L4").Value & ".pdf"

So the above is saved like so INVOICE 1.pdf

Now i wish to include the date so the above becomes INVOICE 1 27/03/2024.pdf
So i added this extra piece of code to the previous mentioned above so now like so.
Rich (BB code):
strFileName = "C:\Users\Ian\Desktop\GRASS CUTTING\CURRENT GRASS SHEETS\PAUL GODDARD\" & "INVOICE " & Range("L4").Value & Range("L13").Value & ".pdf"

But when i now go to save i get a RTE 1004 & the line below is shown in yellow


FULL CODE.
Rich (BB code):
Private Sub Generate_Pdf_Click()
    Dim strFileName As String
  
    strFileName = "C:\Users\Ian\Desktop\GRASS CUTTING\CURRENT GRASS SHEETS\PAUL GODDARD\" & "INVOICE " & Range("L4").Value & Range("L13").Value & ".pdf"
  
    If Dir(strFileName) <> vbNullString Then
        MsgBox "GENERATED PDF INVOICE" & vbNewLine & vbNewLine & "INVOICE " & Range("L4").Value & vbNewLine & vbNewLine & "WAS NOT SAVED AS IT ALLREADY EXISTS", vbCritical + vbOKOnly, "GENERATE PDF FILE MESSAGE"
        Exit Sub
    End If
  
    With ActiveSheet
        .PageSetup.PrintArea = "$F$2:$N$61"
        .ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFileName, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False
        pdf = MsgBox("GENERATED PDF INVOICE" & vbNewLine & vbNewLine & "INVOICE " & Range("L4").Value & vbNewLine & vbNewLine & "WAS SAVED SUCCESSFULLY", vbInformation + vbOKOnly, "GENERATE PDF FILE MESSAGE")
                              
        Range("L4").Value = Range("L4").Value + 1 'INVOICE IS INCREMATED BY 1
        ActiveWorkbook.Save
      
    End With
      
End Sub
 
Last edited by a moderator:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Now i wish to include the date so the above becomes INVOICE 1 27/03/2024.pdf
You cannot use "/" in file names - those are illegal characters for file names. You could use dashes instead, i.e.
INVOICE 27-03-2024.pdf

If cell L13 has a valid date in it, to get it to format like that for your file name, you would refer to it like this in your VBA code:
VBA Code:
... & FORMAT(Range("L13"),"dd-mm-yyyy") & ...

Also note: When posting VBA code, if you want to add formatting (like text color) to your VBA code, you need to use the RICH code tags, and not the VBA code tags.
You cannot use formatting within VBA code tags.
I fixed your last two code tag blocks in your previous post.
 
Upvote 0
Solution
Hi,
The line of code now in use is shown below.
Rich (BB code):
strFileName = "C:\Users\Ian\Desktop\GRASS CUTTING\CURRENT GRASS SHEETS\PAUL GODDARD\" & "INVOICE " & Range("L4").Value & Format(Range("L13"), "dd-mm-yyyy") & ".pdf"

It saves now as supplied screenshot.

Can you advise what i need to do so the output is saved like so,
INVOICE 2
27-03-2024.PDF

Where its on two lines.

With reference RICH / VBA i always used RICH but recently was told that i hadf to use VBA ???
I will just go back to using RICH
Thanks
 

Attachments

  • EaseUS_2024_03_27_11_41_12.jpg
    EaseUS_2024_03_27_11_41_12.jpg
    4.2 KB · Views: 22
Upvote 0
Can you advise what i need to do so the output is saved like so,
INVOICE 2
27-03-2024.PDF

Where its on two lines.
You cannot put formatting characters, like soft carriage returns, in the file names.
If you show it splitting over two lines, like on a Desktop shortcut, that is a function of the horizontal space the shortcut allows.
I don't think there is really a soft carriage return in there, I think it is more of an auto-wrap situation.

With reference RICH / VBA i always used RICH but recently was told that i hadf to use VBA ???
I will just go back to using RICH
You can use either code tag.
Just realize that you CANNOT use formatting features, like text color, bolding, underline, italics, etc within VBA code tags. It won't render properly.
So if you want to use those formatting features, use the RICH code tags in those situations.
 
Upvote 0
This worked for what i need Thanks.

Rich (BB code):
strFileName = "C:\Users\Ian\Desktop\GRASS CUTTING\CURRENT GRASS SHEETS\PAUL GODDARD\" & "INVOICE " & Range("L4").Value & " " & Format(Range"L13"), "dd-mm-yyyy") & ".pdf"
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,205
Members
452,618
Latest member
Tam84

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