Is it possible to determine from VBA when a template was last used?

Answered Is it possible to determine from VBA when a template was last used?

  • Sunday, July 29, 2012 9:48 AM
     
     

    Greetings,

    A newbie question if I may:

    I'd like to create a template (hopefully backward compatible with 2003) that is self contained and transportable i.e. does not use .ini files, or the registry.  This template will be used sporadically, but may also be left unused for months.

    So, I want to be able to check when it was last used and issue, for example a warning message that the template maybe out of date and needs to be checked against another reference document (that might or might not be immediately avaiable).  After the user has confirmed this once, I don't want them to have to respond to the same event for, say another 30 days, even if the template is used a hundred times.

    So, is there a document varialble, or some other property that I can access (with VBA?)?

    Kind Regards and thanks in advance for you time and help.

    Dave


    • Edited by BCOMDave Sunday, July 29, 2012 3:43 PM
    •  

All Replies

  • Monday, July 30, 2012 9:30 AM
    Moderator
     
      Has Code

    Hi Dave,

    Thanks for posting in the MSDN Forum.

    Based on your issue, I think the Application.RecentFiles property may be helpful. You can open the last used document by using the following macro

    Sub Test()
      If Application.RecentFiles.Count > 1 Then
      Application.RecentFiles(1).Open
      End If
    End Sub

    Also, the sample is provided for you.

    http://msdn.microsoft.com/en-us/library/ff195679.aspx

    Hope this can help you.

    Regards,


    Leo_Gao [MSFT]
    MSDN Community Support | Feedback to us


  • Monday, July 30, 2012 9:47 AM
     
     Answered

    You could use a custom document property in the template and use a Document_New macro to both check & update that property. This would work for any new documents based on the template. Those new documents, BTW would inheret the same property & date, so you could also check later on which date template they used was last checked/updated. The code for a custom document property named 'TmpltUpDt' would look like:

    Private Sub Document_New()
    With ThisDocument.CustomDocumentProperties("TmpltUpDt")
      If DateAdd("d", 30, .Value) < Now Then
        If MsgBox("This Template was last updated on " & .Value & vbCr & _
          "Reset Update check date to today?", vbYesNo) = vbYes Then
          .Value = Now
          ActiveDocument.CustomDocumentProperties("TmpltUpDt").Value = Now
        End If
      End If
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked As Answer by BCOMDave Wednesday, August 01, 2012 9:43 AM
    •  
  • Monday, July 30, 2012 8:47 PM
     
     

    Thanks for the suggestion.

    Recent files is about the application and I need to know about a particular template's activity.  Macropod is 99% there (I think) but I can't seem to get it to behave consistently at the moment.  I'll follow up with a post to his suggestion.

    Thanks again.

  • Monday, July 30, 2012 9:06 PM
     
     

    Maropod! You're a star!

    I'm almost there with your suggestion.  The code seems to work fine if I step through it in debug mode, but.

    I had a problem getting it to fire when the template was opened. - Found info about document event procedures.  I didn't realise they have to go in the special ThisDocument module.  I've moved the proc now and it fires on document creation when the template is double clicked, but I can't seem to get it to behave consistently.  The DocVar seems to be updated in the saved document, but not always changed in the template (unless I step through the code in the debug window after which, word asks me to save the template, but it doesn't seem to when I save the created doc).

    Am I doing something wrong? Have I got the proc in the right place?  Do I need to change ActiveDocument to ActiveTemplate or something similar?

    Also I want to call another proc to configure a couple of things and set the DocVar value a bit later, is it better to use a public variable for this value then set it in a 3rd proc later, or is it better to call other stuff and then return back here to finish off?

    Thanks so much for this info.  I think that when I get it robust I can adapt it for a few other tasks I have in mind, so you have helped millions!

    Dave

  • Tuesday, July 31, 2012 5:35 AM
     
     

    OK, it seems the custom document property in the template isn't carried over into the document. That necessitates a slightly different approach:

    Private Sub Document_New()
    With ThisDocument.CustomDocumentProperties("TmpltUpDt")
      If DateAdd("d", 30, .Value) < Now Then
        If MsgBox("This Template was last updated on " & .Value & vbCr & _
          "Reset Update check date to today?", vbYesNo) = vbYes Then
          .Value = Now
        End If
      End If
    End With
    ActiveDocument.CustomDocumentProperties.Add Name:="TmpltUpDt", LinkToContent:=False, _
      Type:=msoPropertyTypeDate, Value:=ThisDocument.CustomDocumentProperties("TmpltUpDt").Value
    End Sub

    if you're not interested in having the template revision date in the new document, delete the two lines beginning at'ActiveDocument'.


    Cheers
    Paul Edstein
    [MS MVP - Word]

  • Wednesday, August 01, 2012 9:44 AM
     
     Answered

    Sorry,

    This last suggestion throws an error:

    Run-time error ‘-2147467259 (80004005)’:

    Automation error

    Unspecified error

    Probably a kludge but, I found that I can manipulate the Doc Var independently in either the template or document with the following:

    ActiveDocument.AttachedTemplate.CustomDocumentProperties("TmpltUpDt").Value = Now

    ActiveDocument.CustomDocumentProperties("TmpltUpDt").Value = Now

    I have also forced a save of the template in the background with

    ActiveDocument.AttachedTemplate.Save AddToRecentFiles = False

    So it doesn't matter about any inheritance. I can just control things from the template, if it hasn'r been used for n days, then it throws a message and asks for some reconfiguration to update itself. - just what I wanted and I think this will work for my purposes.

    Thanks again Macropod for your time and help. The solution was 99% yours!

    Kind Regards,

    Dave     

    • Marked As Answer by BCOMDave Wednesday, August 01, 2012 9:45 AM
    •  
  • Wednesday, August 01, 2012 12:18 PM
     
     

    The error was probably because of (unwanted) line wrapping in the post. All of the following should be on one line:

    Type:=msoPropertyTypeDate, Value:=ThisDocument.CustomDocumentProperties("TmpltUpDt").Value

    with no spaces before the '('.


    Cheers
    Paul Edstein
    [MS MVP - Word]

  • Wednesday, August 01, 2012 12:59 PM
     
     

    Just for feedback.  No sorry that's not it.  I tried it on one line - same error.  The project window is happy and doesn't throw any thing up.  Only fails at runtime.  Don't worry though, you have helped me immensely.

    Thanks again.

    Dave

  • Wednesday, August 01, 2012 1:08 PM
     
     
    Curious - it worked fine in my testing.

    Cheers
    Paul Edstein
    [MS MVP - Word]

  • Wednesday, August 01, 2012 11:05 PM
     
     Answered

    Os / Version?  I have Vista home SP2 Build 6002 - 32 bit. MSO Office 2007 SP3 (12.0.6661.5000).  Both fully up to date with MS updates etc.  I ran the Office Daigs and they are clean.

    I treid twice.  Once in the template I was working with and then in a completely clean template.

    Aha! I just tried a few variations.  I added:

    ActiveDocument.CustomDocumentProperties.Add Name:="YourName", _
            LinkToContent:=False, Value:="Joe", Type:=msoPropertyTypeString

     Before the breakpoint of the error and stepped through it.  The first itteration was successful with the new .Add method, but a second ittereation failed with the same error as above.  If I delete the custom Doc Var then it will work again - So, (at least in my case) it seems that if the doc var exists (which I guess makes sense as the complier is happy, but runtime isn't) it throws the above error.

    As you say curious because in the case of the TmpltUpDt var in the code we have to have it in place or the early code won't work.

    Wouldn't it be a nice world if the runtime error gave us a clue 

    Anyhow,

    Thanks again.

    • Edited by BCOMDave Wednesday, August 01, 2012 11:09 PM
    • Marked As Answer by BCOMDave Wednesday, August 01, 2012 11:10 PM
    •