|
-
Mar 11th, 2016, 02:25 AM
#1
Thread Starter
Junior Member
[RESOLVED] Macro to add today's date when saving the file in a different format
Hello!
I want to send specific worksheets in Excel to PDF and save in a specified folder. I have done this successfully using:
Code:
Sub SendPDF()
' SendToPDF Macro
Sheets(Array("COVER", "In DAILY Summary", "Out DAILY Summary", _
"Out DAILY by Time")).Select
Sheets("COVER").Activate
ChDir "C:\MyFileLocation"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:/MyOutputLocation\Stats.pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub
However, I'd like to take It one step further by adding today's date when it saves into the file name. So the above would become "Stats 11-03-2016.pdf".
I found this in an inferior forum for adding the date part. I've tried using part of the below in the above but doesn't work. Any ideas please?
Code:
Sub Save()
Dim dtDate As Date
dtDate = Date
Dim strFile As String
strFile = "X:\file" & Format(dtDate, "mm-dd-yyyy") & ".xlsm"
ActiveWorkbook.SaveAs Filename:=strFile, FileFormat _
:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
Thanks!
-
Mar 11th, 2016, 04:06 AM
#2
Re: Macro to add today's date when saving the file in a different format
try like
Code:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:/MyOutputLocation\Stats" & format(Date,"dd-mm-yyyy") & ".pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
though i am not sure you should have a / in your file path
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Mar 11th, 2016, 04:27 AM
#3
Thread Starter
Junior Member
Re: Macro to add today's date when saving the file in a different format
The / was my typo when I was changing the file path to something non-identifying I'll give this a go now.
Thanks.
-
Mar 11th, 2016, 04:45 AM
#4
Thread Starter
Junior Member
Re: Macro to add today's date when saving the file in a different format
I used this but error 1004, Select method of sheets class failed.
Code:
Sub SendPDF()
' Send To PDF with Date Macro
Sheets(Array("COVER", "In DAILY Summary", "Out DAILY Summary", _
"Out DAILY by Time")).Select
Sheets("COVER").Activate
ChDir "C:\MyFileLocation"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\MyOutputLocation\Stats" & Format(Date, "dd-mm-yyyy") & ".pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub
-
Mar 11th, 2016, 05:49 AM
#5
Re: Macro to add today's date when saving the file in a different format
the error would appear to be for the first line of code?
the first 3 lines are possibly irrelevant to the task, try
Code:
Sub SendPDF()
sheets("cover").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\MyOutputLocation\Stats" & Format(Date, "dd-mm-yyyy") & ".pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Mar 11th, 2016, 05:55 AM
#6
Thread Starter
Junior Member
Re: Macro to add today's date when saving the file in a different format
 Originally Posted by westconn1
the error would appear to be for the first line of code?
the first 3 lines are possibly irrelevant to the task, try
Code:
Sub SendPDF()
sheets("cover").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\MyOutputLocation\Stats" & Format(Date, "dd-mm-yyyy") & ".pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub
I need the first few lines because I have to nominate which worksheets I want printing or it prints all (there are 7 worksheets, I only want to export 4 to PDF).
-
Mar 11th, 2016, 07:02 AM
#7
Thread Starter
Junior Member
Re: Macro to add today's date when saving the file in a different format
 Originally Posted by VbtoMe
I need the first few lines because I have to nominate which worksheets I want printing or it prints all (there are 7 worksheets, I only want to export 4 to PDF).
Managed to work round my issue. I added
"worksheets("cover".visible = True" etc.
for the worksheets I wanted to print and used the same code as before and it works.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|