• Macro to put LastSavedDate in file name? (2002)

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Macro to put LastSavedDate in file name? (2002)

    Author
    Topic
    #402185

    I’ve been trying, unsuccessfully, to write a macro that automatically saves a document with a name that includes the date and time it was last saved, so I can keep track of multiple versions easily. E,g.: Memo re Macros 03-12-04 1301.doc. (The 1301 is 1:01 p.m. — since Windows doesn’t accept colons in filenames, that’s the best format I can think of.) I know about Word’s Document Property that returns the LastSavedDate, But that’s not enough for me to do this. I have two problems:

    First is getting the date into the file name reliably, in the right place without scewing up the rest of the name. Right now, I’m envisioning just saving the file for the very first time, when I create it, with just the descriptive name. (In the example above, the descriptive name is Memo re Macros.) After that, every time I save it, I want Word to insert the right date and time after the descriptive name, adding the date/time after the descriptive name if there’s no date/time there yet, or replacing the date/ time that’s already there with the updated LastSavedTime.

    Second, for some reason I’m having a problem getting Word to reliably get the last saved date. I know I have to have the macro save the document twice: the first time to set the last saved date variable properly, and the second to then use that updated date to put in the filename, but whatever I’m doing isn’t working consistently.

    I can’t believe that there aren’t already a whole lot of macros floating around that do this, or that there isn’t a MUCH easier way of doing it than I’ve been envisioning.

    Viewing 1 reply thread
    Author
    Replies
    • #798039

      Why don’t you just use the function Now to get the current date/time? If you’re saving the document now, the SavedDate will be just that, and you’d avoid having to save the document twice.

      • #798083

        Thanks! Didn’t know the function, but I’ll give it a try, Now if someone can just help figure out the formatting problem re getting it into the filename….. You’d really think that Word would have a built-in function for that, wouldn’t you?

        • #798119

          You could put the file name without the date in a document property or document variable, say

          ActiveDocument.Variables(“DocName”) = “Memo Re Macros”

          and then retrieve this and combine it with the date to construct the file name:

          ActiveDocument.SaveAs FileName:=ActiveDocument.Variables(“DocName”) & ” ” & _
          Format(Now, “mm-dd-yy hhmm”) & “.doc”

          • #798357

            Thanks, that gets me part of the way there, but not all the way. Let’s say the file already is saved as “Memo Re Macros 03-12-04 1301.doc” But now it’s tomorrow at 9:00 a.m. so when I save it, I need the macro to change the name to “Memo re Macros 03-13-04 0900.doc.” I need to have a way for the macro to find what you call thestring that goes into “DocName” in your snippet below, before I do the concatenation. There must be somevariable that gives me the full document name, then a string function that lets me count 17 characters back from the “c” in the .doc extension, and then something else that lets me assign everthing to the left of that, no matter how long, to the variable you’re calling DocName. Maybe someone can just recommend a good book. Word 2002’s VBA help material is much more confusing to me than Word 2000’s was, which is why I’m having trouble, I think.

            • #798369

              If you use the document variable as I suggested, you wouldn’t have to extract the “bare” name. But you can easily retrieve it too:

              Dim strFileName As String
              ‘ Get complete file name
              strFileName = ActiveDocument.FullName
              ‘ Get everything except last 17 characters
              strFileName = Left(strFileName, Len(strFileName) – 17)
              ‘ Append date/time
              strFileName = strFileName & Format(Now, “mm-dd-yy hhmm”) & “.doc”

            • #798464

              Hans,

              is there a reason for the document variable? couldn’t your code work just by looking at the current fullname and changing it for the current date-time as you’ve done?

              as far as the actual saving goes, wouldn’t there also be some issue with intercepting a “save” bcs the process would just use the existing name (with the last date-time). Are you assuming that’s taken care of and that some existing (part of a user-written) macro is used to do the saving?

              I also wonder if Word (or a user written macro) distinguishes between a Save on a document that’s never been saved before (hence treating it as a Save As). So does your snippet also assume the initial Save As has been done so the “Memo re Macros” has already been created?

              Fred

            • #798470

              Fred,

              >> is there a reason for the document variable?

              It was just one way to do it; my previous reply showed how you could modify the current fullname.

              >> would just use the existing name

              That would only occur if you save twice within the same minute (or in the autumn, when the clock is put back an hour grin). If that is a problem, you would have to build in an extra check.

              >> So does your snippet also assume the initial Save As has been done so the “Memo re Macros” has already been created?

              I assumed that Michael (the original poster) would take care of that in some way or other.

            • #798471

              Fred,

              >> is there a reason for the document variable?

              It was just one way to do it; my previous reply showed how you could modify the current fullname.

              >> would just use the existing name

              That would only occur if you save twice within the same minute (or in the autumn, when the clock is put back an hour grin). If that is a problem, you would have to build in an extra check.

              >> So does your snippet also assume the initial Save As has been done so the “Memo re Macros” has already been created?

              I assumed that Michael (the original poster) would take care of that in some way or other.

            • #798465

              Hans,

              is there a reason for the document variable? couldn’t your code work just by looking at the current fullname and changing it for the current date-time as you’ve done?

              as far as the actual saving goes, wouldn’t there also be some issue with intercepting a “save” bcs the process would just use the existing name (with the last date-time). Are you assuming that’s taken care of and that some existing (part of a user-written) macro is used to do the saving?

              I also wonder if Word (or a user written macro) distinguishes between a Save on a document that’s never been saved before (hence treating it as a Save As). So does your snippet also assume the initial Save As has been done so the “Memo re Macros” has already been created?

              Fred

            • #798370

              If you use the document variable as I suggested, you wouldn’t have to extract the “bare” name. But you can easily retrieve it too:

              Dim strFileName As String
              ‘ Get complete file name
              strFileName = ActiveDocument.FullName
              ‘ Get everything except last 17 characters
              strFileName = Left(strFileName, Len(strFileName) – 17)
              ‘ Append date/time
              strFileName = strFileName & Format(Now, “mm-dd-yy hhmm”) & “.doc”

            • #798497

              See the list of Word VBA books at my URL.
              Steve Roman’e Writing Word Macros is the best place to start.

            • #798498

              See the list of Word VBA books at my URL.
              Steve Roman’e Writing Word Macros is the best place to start.

            • #798664

              I posted a macro last year (I think the word “backup” appeared in the subject) which hijacked the Save command and replaced it with a SaveAs that incremented a version number in the file name. You could use the same code and replace the version number part with the date. I’ll see if I can find it.

              [Searching…]

              Sorry, that took longer than I expected. The post with the code didn’t explain the context. smile See post 283508. Hope this helps.

              (Just to explain part of the code, the InStrRev searches from the end of the existing filename back toward the beginning for a period, and then tacks on everything from that point forward to the end of the file name. Normally, this would be .doc, but the code is kept general in case it’s something else. This method is not available in Word 97.)

            • #798665

              I posted a macro last year (I think the word “backup” appeared in the subject) which hijacked the Save command and replaced it with a SaveAs that incremented a version number in the file name. You could use the same code and replace the version number part with the date. I’ll see if I can find it.

              [Searching…]

              Sorry, that took longer than I expected. The post with the code didn’t explain the context. smile See post 283508. Hope this helps.

              (Just to explain part of the code, the InStrRev searches from the end of the existing filename back toward the beginning for a period, and then tacks on everything from that point forward to the end of the file name. Normally, this would be .doc, but the code is kept general in case it’s something else. This method is not available in Word 97.)

          • #798358

            Thanks, that gets me part of the way there, but not all the way. Let’s say the file already is saved as “Memo Re Macros 03-12-04 1301.doc” But now it’s tomorrow at 9:00 a.m. so when I save it, I need the macro to change the name to “Memo re Macros 03-13-04 0900.doc.” I need to have a way for the macro to find what you call thestring that goes into “DocName” in your snippet below, before I do the concatenation. There must be somevariable that gives me the full document name, then a string function that lets me count 17 characters back from the “c” in the .doc extension, and then something else that lets me assign everthing to the left of that, no matter how long, to the variable you’re calling DocName. Maybe someone can just recommend a good book. Word 2002’s VBA help material is much more confusing to me than Word 2000’s was, which is why I’m having trouble, I think.

        • #798120

          You could put the file name without the date in a document property or document variable, say

          ActiveDocument.Variables(“DocName”) = “Memo Re Macros”

          and then retrieve this and combine it with the date to construct the file name:

          ActiveDocument.SaveAs FileName:=ActiveDocument.Variables(“DocName”) & ” ” & _
          Format(Now, “mm-dd-yy hhmm”) & “.doc”

      • #798084

        Thanks! Didn’t know the function, but I’ll give it a try, Now if someone can just help figure out the formatting problem re getting it into the filename….. You’d really think that Word would have a built-in function for that, wouldn’t you?

    • #798040

      Why don’t you just use the function Now to get the current date/time? If you’re saving the document now, the SavedDate will be just that, and you’d avoid having to save the document twice.

    Viewing 1 reply thread
    Reply To: Macro to put LastSavedDate in file name? (2002)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: