Can anyone tell me why my file isn´t save with this code?

KhallP

Board Regular
Joined
Mar 30, 2021
Messages
157
Office Version
  1. 2016
Platform
  1. Windows
Since I added the time format the code stopped working, is there any way to include the time in the file name?



Code:

Sub SaveAsPDF()

Dim FilePath As String

Select Case MachineName

Case "A4", "A8.1", "A8.2", "A8.3", "A12.1", "A12.2", "A12.3", "A20.1", "A20.2"
FilePath = Environ("userprofile") & "\Documents\Projeto\" & MachineName & "\"

Sheets("Final").Range("A1:AO69").ExportAsFixedFormat Type:=xlTypePDF, filename:=FilePath & MachineName & " _ " & Format(Now(), "dd.mm.yyyy") & " _ " & Format(Now(), "hh:mm") & " _ " & ".pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
End Select
End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
A few things:
1. I do not believe the ":" is a valid character you can use in a file name. I think that is the issue.
2. There is no need to use two FORMAT functions, one for date and one for time. You can do both in a single function.
3. There is no need to split up "_" and ".pdf". They are both strings, so can be combined to one.

So, try this:
VBA Code:
Sheets("Final").Range("A1:AO69").ExportAsFixedFormat Type:=xlTypePDF, filename:=FilePath & MachineName & Format(Now(), "_dd.mm.yyyy_hh.mm") & "_.pdf"
Note that I noticed you had some extra spaces around the "_" which I removed. If you really want them, you can easily add them back in, i.e.
VBA Code:
Sheets("Final").Range("A1:AO69").ExportAsFixedFormat Type:=xlTypePDF, filename:=FilePath & MachineName & Format(Now(), " _ dd.mm.yyyy _ hh.mm") & " _ .pdf"
 
Upvote 0
I might also suggest you make the filename a separate string variable before the save statement. That way you can use Debug.Print or MSGBOX to check the name for correctness before the save.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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