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

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
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,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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