Results 1 to 8 of 8
  1. #1
    2 Star Lounger
    Join Date
    Mar 2003
    Posts
    174
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Date Comparison (Word 2000)

    I like to create put a macro in my word document that would do the following:

    1. On opening, compare the document creation date with current date;
    2. If the doc creation date is less than 100 days older than current date, it will open the document;
    3. If the doc creation date is 100 days older than current date, then it will executes a second macro (such as deleting part of the document)

  2. #2
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Hornsby Heights, New South Wales, Australia
    Posts
    3,822
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Date Comparison (Word 2000)

    You would need to get into VBE, press F1, enter "date" and check out the resulting links and samples. Then repeat the process for "Document Open".

    Good luck! <img src=/S/smile.gif border=0 alt=smile width=15 height=15>
    Cheers, Claude.

  3. #3
    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: Date Comparison (Word 2000)

    To find the creation date, you check the document properties:

    ActiveDocument.BuiltInDocumentProperties(wdPropert yTimeCreated)

    This actually gives you something like the following:

    3/18/2003 10:58:00 AM

    But that's okay, you still can get the difference between this date and today's date even with the time in there:

    DateDiff("d", ActiveDocument.BuiltInDocumentProperties(wdPropert yTimeCreated), Now)

    Which returns 5 for 5 days.

    Is this enough to get you started?

  4. #4
    2 Star Lounger
    Join Date
    Mar 2003
    Posts
    174
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Date Comparison (Word 2000)

    Sorry for my VBA ignorance. Could you give me the codes I need to insert into word file VBA module ? What I need is this:

    If file saving date is less than 5 days older than current date, then open file;
    If file saving date is more than 5 days older than current date, exit file.

    Many thanks in advance !

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

    Re: Date Comparison (Word 2000)

    What you want is problematic. You want a macro to be run automatically when the user opens a document. That in itself is possible, you can write a macro named AutoOpen in a standard module, or an Document_Open macro in the ThisDocument class module.

    But ... if the user has not set the macro security level to Low, Word will ask whether macro's should be enabled. If the user chooses to disable macros, the document will open, and your macro can't prevent that, precisely because macro's have been disabled. And even if the macro security level is low, it is possible to open a document with the Shift key held down; this circumvents automatic macros.

    If you still want to do this, here are the steps to take:
    1. <LI>With your document open, activate the Visual Basic Editor (Alt+F11)
      <LI>If necessary, activate the Project Explorer (Ctrl+R)
      <LI>Expand your document, until you see ThisDocument
      <LI>Double click this document to open the class module
      <LI>Enter the following code:

      Private Sub Document_Open()
      If DateDiff("d", Me.BuiltInDocumentProperties(wdPropertyTimeLastSav ed), Now()) > 5 Then
      Me.Close SaveChanges:=False
      End If
      End Sub

      <LI>Switch back to Word
      <LI>Save and close the document
    Now, test opening the document and the effect of enabling/disabling macros and of holding down the Shift key.

    Note that there is no code to open the document if the SaveDate is less than 5 days ago. By the time the macro runs, the document is already open (otherwise, the macro couldn't run), although it's not displayed yet.

  6. #6
    2 Star Lounger
    Join Date
    Mar 2003
    Posts
    174
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Date Comparison (Word 2000)

    I did exactly as told (copying the codes into a class module), and then changed the date on the computer, but the file opens regardless. Did I do anything wrong ?

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

    Re: Date Comparison (Word 2000)

    You should *not* copy the code into an arbitrary class module (one you create by Insert/Class Module). It *must* go into the module belonging to ThisDocument, for Document_Open is an event of the Document object.

  8. #8
    2 Star Lounger
    Join Date
    Mar 2003
    Posts
    174
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Date Comparison (Word 2000)

    Hans, my apology for the earlier post. Your codes worked ( I just changed me. to ActiveDocument). Many thanks.

Posting Permissions

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