Consulting

Results 1 to 14 of 14

Thread: VBA how to use date to write file name in PDF

  1. #1
    VBAX Regular
    Joined
    Oct 2018
    Location
    Antwerp
    Posts
    41
    Location

    VBA how to use date to write file name in PDF

    How do I use "date document" for a PDF file name?
    The declaration of "document date" causes an error:

    Dim datedoc As Date
    Then I use datedoc to my PDF-file name which does not work.

    Sub InvoiceToPdf()
    Dim volgnr As Long
    Dim slo As String
    Dim Toganr As Long
    Dim bedrag As Currency
    Dim fname As String
    Dim datedoc As Date
    volgnr = Range("B1")
    slo = Range("B2")
    Toganr = Range("B3")
    bedrag = Range("B4")
    Path = "C:\Users\gebruiker\factuur"
    ' this works not, the datedoc causes error
    fname = Toganr & " - " & slo & " - " & volgnr & " - " & datedoc
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, ignoreprintareas:=False, Filename:=Path & fname
    End Sub
    ___________
    Thanks, Ward
    Last edited by Aussiebear; 06-26-2022 at 02:49 AM. Reason: Added code tags to supplied code

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    I don't see where you're putting a value into 'datedoc'
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    As Paul alluded to, put the following line after the "bedrag = Range("B4")" line
    datedoc = Format(Date, "yyyy-mm-dd")

  4. #4
    Format(Now(), "yyyy-mmmm-dd")
    'or maybe
    Format(Now(), "mmmm-dd-yyyy")

  5. #5
    VBAX Regular
    Joined
    Oct 2018
    Location
    Antwerp
    Posts
    41
    Location
    Dim volgnr As Long
    Dim slo As String
    Dim Toganr As Long
    Dim bedrag As Currency
    Dim fname As String
    Dim datedoc As Date
    volgnr = Range("B1")
    slo = Range("B2")
    Toganr = Range("B3")
    bedrag = Range("B4")
    datedoc = Range("B5")
    Path = "C:\Users\gebruiker\factuur"
    ' fname = Toganr & slo & volgnr & datedoc
    fname = Toganr & " - " & slo & " - " & volgnr & " - " & datedoc
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, ignoreprintareas:=False, Filename:=Path & fname
    ______
    Here I did put the value of cell B5 into "datedoc".
    The macro does not work.
    Last edited by Aussiebear; 06-26-2022 at 02:50 AM. Reason: Added code tags to supplied code

  6. #6
    VBAX Regular
    Joined
    Oct 2018
    Location
    Antwerp
    Posts
    41
    Location
    Thanks, but adding this line causes an error.

    Quote Originally Posted by jolivanes View Post
    Format(Now(), "yyyy-mmmm-dd")
    'or maybe
    Format(Now(), "mmmm-dd-yyyy")

  7. #7
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    Quote Originally Posted by wdg1 View Post
    Thanks, but adding this line causes an error.
    Ward, please tell us the type of error? Oh by the way please enclose your code by using the # tag
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Quote Originally Posted by wdg1 View Post
    Dim volgnr As Long
    Dim slo As String
    Dim Toganr As Long
    Dim bedrag As Currency
    Dim fname As String
    Dim datedoc As Date
    volgnr = Range("B1")
    slo = Range("B2")
    Toganr = Range("B3")
    bedrag = Range("B4")
    datedoc = Range("B5")
    Path = "C:\Users\gebruiker\factuur"
    ' fname = Toganr & slo & volgnr & datedoc
    fname = Toganr & " - " & slo & " - " & volgnr & " - " & datedoc
    
    
    MsgBox Path & fname  '  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    
    
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, ignoreprintareas:=False, Filename:=Path & fname
    ______
    Here I did put the value of cell B5 into "datedoc".
    The macro does not work.

    Not being a mind reader or having a workbook to test, I'm guessing that if you added the marked line, there's no backslash before fname

    Otherwise it depends on the contents of the ranges that are used to construct the fname
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    VBAX Regular
    Joined
    Oct 2018
    Location
    Antwerp
    Posts
    41
    Location
    Quote Originally Posted by Paul_Hossler View Post
    Not being a mind reader or having a workbook to test, I'm guessing that if you added the marked line, there's no backslash before fname

    Otherwise it depnds on the contents of the ranges that are used to construct the fname
    The error is 1004:
    The document has not been saved. The document may still be open or an error may have occurred while saving the document.

    this is a strange error because, if I delete the "date", the macro works fine:
    (A)
    fname = Toganr & " - " & slo & " - " & volgnr & " - " & datedoc
    ERROR

    (B)
    fname = Toganr & " - " & slo & " - " & volgnr & "
    NO ERRORtest25062022.xlsm

    Sub InvoiceToPdf()
    Dim volgnr As Long
    Dim slo As String
    Dim Toganr As Long
    Dim bedrag As Currency
    Dim fname As String
    Dim datedoc As Date
    datedoc = Format(Date, "mmmm-dd-yyyy")
    
    
    volgnr = Range("B1")
    slo = Range("B2")
    Toganr = Range("B3")
    bedrag = Range("B4")
    datedoc = Range("B5")
    
    
    Path = "C:\Users\gebruiker\factuur\"
    
    
    fname = Toganr & " - " & slo & " - " & volgnr & " - " & datedoc
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, ignoreprintareas:=False, Filename:=Path & fname
    
    
    
    
    End Sub
    ..



  10. #10
    What is in B5?

  11. #11
    VBAX Regular
    Joined
    Oct 2018
    Location
    Antwerp
    Posts
    41
    Location
    Quote Originally Posted by jolivanes View Post
    What is in B5?
    In B5 is today date.
    In Belgium =vandaag()
    In english =today()

    If I put the date of today, dd/mm/yy (26th June 2022), which in the USA is 06/26/2022, the macro does not work.

  12. #12
    VBAX Regular
    Joined
    Oct 2018
    Location
    Antwerp
    Posts
    41
    Location
    Quote Originally Posted by jolivanes View Post
    Format(Now(), "yyyy-mmmm-dd")
    'or maybe
    Format(Now(), "mmmm-dd-yyyy")
    ok, finally, I discovered where to use this suggestion!
    The line has to be:

    fname = Toganr & " - " & slo & " - " & volgnr & " - " & Format(Now(), "dd-mm-yyyy") & ".pdf"
    Now today's date is added to the fine name.

    Thank you again for your help, which is much appreciated.
    Ward, belgium

  13. #13
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    What happens if you wish to reprint or resave the pdf at a later date?
    Last edited by Aussiebear; 06-26-2022 at 01:54 PM. Reason: Missed part of the question
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  14. #14
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    If I put the date of today, dd/mm/yy (26th June 2022), which in the USA is 06/26/2022, the macro does not work.

    You cannot use slashes in a filename


    https://docs.microsoft.com/en-us/win.../naming-a-file

    Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:

    • The following reserved characters:
      • < (less than)
      • > (greater than)
      • : (colon)
      • " (double quote)
      • / (forward slash)
      • \ (backslash)
      • | (vertical bar or pipe)
      • ? (question mark)
      • * (asterisk)
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •