Results 1 to 10 of 10
  1. #1
    2 Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    108
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Macro to put LastSavedDate in file name? (2002)

    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.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to put LastSavedDate in file name? (2002)

    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.

  3. #3
    2 Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    108
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macro to put LastSavedDate in file name? (2002

    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?

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to put LastSavedDate in file name? (2002

    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"

  5. #5
    2 Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    108
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macro to put LastSavedDate in file name? (2002

    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.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to put LastSavedDate in file name? (2002

    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"

  7. #7
    Silver Lounger
    Join Date
    Jan 2001
    Location
    West Long Branch, New Jersey, USA
    Posts
    1,921
    Thanks
    6
    Thanked 9 Times in 7 Posts

    Re: Macro to put LastSavedDate in file name? (2002

    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

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to put LastSavedDate in file name? (2002

    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 <img src=/S/grin.gif border=0 alt=grin width=15 height=15>). 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.

  9. #9
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macro to put LastSavedDate in file name? (2002

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

  10. #10
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Macro to put LastSavedDate in file name? (2002

    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. <img src=/S/smile.gif border=0 alt=smile width=15 height=15> See <post#=283508>post 283508</post#>. 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.)

Posting Permissions

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