none
Need help with VBA to save file with date/time as its name or save with only date as name RRS feed

  • Question

  • I am creating a Word "Form" to allow us to create a log of daily events but i am hung up on having it do what we need it to do.

    We need to start with a word Form Template every shift and as the day progresses we need to save this as a new named file so as not to destroy the template so i was going to add a "Save" Button in the form to save the file named with the DATE only to H drive directory "common".  Then at the end of day we need to Print. save as a PDF File and close the file with I thought a different "End of Day" button on the form, that would save as a PDF File Named with the DATE and TIME is was saved to a Different directory H drive directory "Everyone" then print the form for signing and Close the form.

    I have hunted online for ideas  but i am hung up and ill admit am in over my head,  We need to have the form at end of shift saved so it cannot be edited so i thought PDF would make that happen, We need the template to remain a template and we need to save the file during the day but at end of day save it as date time format.. Thoughts better ideas,, all welcome and thanks

    Sunday, April 14, 2019 2:35 PM

All replies

  • Your description suggests you're not using a true Word template at all; rather, you're using a document as a template. Otherwise you wouldn't have the issue of trying to ensure "to save this as a new named file so as not to destroy the template". A new Word document created from a true Word template (i.e. a dotx or dotm file) can only be saved via Save As.

    As for the broader issues, it's not clear whether you're saving the document immediately it's created or only at the end of the day. For the former, a Document_New macro in the template could be used to save the file with the desired date & time based name. For the latter, a MACROBUTTON field in the document body could be used to trigger a macro to save the file with the desired date & time based name - but even an ordinary save will do if you've already saved it when created via the Document_New macro.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, April 15, 2019 12:59 AM
  • Well lesson learned about true template that is a new one to me but helps a lot and simplifies things thank you

    Now i need to come up with a maco that works off a select button and auto saves the file as a PDF with the date and time as the file name into a different directory  h:common. everyone 

    Slowly getting there.. step at a time.. thanks again

    Monday, April 15, 2019 11:44 AM
  • To save your existing document as a Word template, see: https://support.office.com/en-us/article/Save-a-Word-document-as-a-template-CB17846D-ECEC-49D4-82EA-A6F5E3E8B9AE

    A macro to save a document in both .docx and .pdf formats might be:

    Sub SaveMe()
    Dim StrNm As String: StrNm = Format(Now(), "YYYYMMDD hhmm")
    Const DocPath As String = "Path to document folder"
    Const PDFPath As String = "Path to PDF folder"
    With ActiveDocument
      If Split(.Name, ".")(0) Like "20[0-9][0-9] [0-9][0-9][0-9][0-9]" Then StrNm Split(.Name, ".")(0)
      .SaveAs FileName:=DocPath & "/" & StrNm & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=True
      .SaveAs FileName:=PDFPath & "/" & StrNm & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
    End With
    End Sub

    Such a macro would be added to the template (which means you'll need to save the template in the .dotm format). As coded, if you re-save the document after it's first been saved with a name in the "YYYYMMDD hhmm", any further saves of the document via the macro will update both that document and the PDF, rather than creating new copies with a different date/time stamp. If you assign the macro to a keyboard shortcut, you can use that rather than needing a button in the document.

    For PC macro installation & usage instructions, see: http://www.gmayor.com/installing_macro.htm


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, April 15, 2019 10:30 PM