Results 1 to 7 of 7

Thread: [RESOLVED] Macro to add today's date when saving the file in a different format

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2016
    Posts
    23

    Resolved [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!

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2016
    Posts
    23

    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.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2016
    Posts
    23

    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

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2016
    Posts
    23

    Re: Macro to add today's date when saving the file in a different format

    Quote Originally Posted by westconn1 View Post
    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).

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2016
    Posts
    23

    Re: Macro to add today's date when saving the file in a different format

    Quote Originally Posted by VbtoMe View Post
    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
  •  



Click Here to Expand Forum to Full Width