Results 1 to 15 of 15
  1. #1
    Star Lounger
    Join Date
    Sep 2007
    Posts
    54
    Thanks
    7
    Thanked 1 Time in 1 Post

    Lock the Date Field (English/Word/2003)

    Hi.

    I work for a law firm and sometimes they don't want the automatic date on the letter or the memo to be updated. Because the memo and letter are templates and I need to have the automatic date so that it always opens with the current date, I have the macro locking the date as soon as the template is opened. Once the document is closed and then opened again, a message box asks the user if they would like to change the date to the current date. However, this message box pops up everytime and I would like to change this macro so that it only pops up when the date in the letter/memo is different than the current date.

    Is it possible to have the macro compare the date with the current date and then if they are different, ask the user if they want to change the date? If so, can you please give me some sample code so that I can fix this?

    Thanks so much for your help.

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Lock the Date Field (English/Word/2003)

    Hi Maverick,

    I wonder whether you'd be better off using one of:
    CREATEDATE
    SAVEDATE or
    PRINTDATE
    A CREATEDATE field, for example, will reflect the date on which the document is created (not the date its on which template was created). No macros required. If you need to update the date returned by a CREATEDATE field to the current date at any time, simply re-save the file using File|Save As.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    Star Lounger
    Join Date
    Sep 2007
    Posts
    54
    Thanks
    7
    Thanked 1 Time in 1 Post

    Re: Lock the Date Field (English/Word/2003)

    Hi Paul.

    This sounds like it would create a lot of duplication of documents as 9 times out of 10 we do want to update the date (lawyer's tend to make many revisions before the letter/memo gets finalized). That's why I would prefer to do it through the macro, which is working well except for it being triggered every time the document is open as opposed to every time the document is opened and the date is different.

    Thanks though.

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Lock the Date Field (English/Word/2003)

    Can you show us the code you are currently using? We can't suggest modifications until we see what you are using.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    New Lounger
    Join Date
    Feb 2008
    Location
    Beckenham, United Kingdom
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Lock the Date Field (English/Word/2003)

    Hi Maverick

    We do something similar although do not use fields (because of the issues you mentioned). The date is entered in the document as text and wrapped in bookmarks. The macro compares the date in the bookmark with the current date &, if the bookmark date is less than the current one, displays a message box etc. Does this code help?

    Sub Change_Date()
    Dim dtDoc As Date
    Dim rDate As Range

    Set rDate = ActiveDocument.Bookmarks("Date").Range
    dtDoc = rDate.Text

    If dtDoc < Date Then
    Message = MsgBox("Would you like to change the date?", vbYesNo, "Change date")
    If Message = vbYes Then
    rDate.Text = Format(Date, "Long Date")
    End If
    End If

    End Sub

  6. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Lock the Date Field (English/Word/2003)

    Hi Maverick,

    I think you misunderstood. You don't have to make a duplicate when using File|Save As - you can re-use the same name. And you'd only need to use that once - just before printing the final edited document. Alternatively, you could use a SAVEDATE field, making sure to save the document before printing after the final edits.

    Another way of approaching this would be to use a field coded along the lines of:
    <code>{QUOTE{IF{DATE @ YYYYMMDD}> {SAVEDATE @ YYYYMMDD} {ASK DocDate "Please Confirm the Document Date" d {DATE @ "dddd, d MMMM YYYY"}}}{REF DocDate}}</code>
    Provided the document is set to 'Update Fields' under Tools|Options|Print, the user will be prompted to update the date if the current date is later than the date last saved. Again, no vba required.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  7. #7
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Lock the Date Field (English/Word/2003)

    <P ID="edit" class=small>(Edited by wdwells on 19-May-08 08:26. Error trapping added to the code.)</P>Hi Maverick

    We've not seen anything to indicate whether any of the solutions satisfied your needs. Accordingly, here is another solution. This will require that you insert a bookmark "SOD" immediately in front of the date, and another "EOD" immediately after the date.
    <div style="width: 100%; background-color: #FFFFFF;"><code><font color=black><font color=blue>Sub</font color=blue> FinalizeDate()
    <font color=blue>Dim</font color=blue> myDate, ExistDate <font color=blue>As</font color=blue> <font color=blue>String</font color=blue>
    <font color=blue>Dim</font color=blue> Ctr <font color=blue>As</font color=blue> <font color=blue>Long</font color=blue>
    <font color=blue>Dim</font color=blue> Bmk1, Bmk2 <font color=blue>As</font color=blue> <font color=blue>Boolean</font color=blue>
    <font color=blue>Const</font color=blue> myt = "Document Date"

    <font color=blue>If</font color=blue> ActiveDocument.Bookmarks.Count < 2 <font color=blue>Then</font color=blue> <font color=blue>GoTo</font color=blue> ErrorHandler
    <font color=blue>With</font color=blue> ActiveDocument
    <font color=blue>For</font color=blue> Ctr = 1 <font color=blue>To</font color=blue> .Bookmarks.Count
    <font color=blue>If</font color=blue> .Bookmarks(Ctr).Name = "SOD" <font color=blue>Then</font color=blue> Bmk1 = <font color=blue>True</font color=blue>
    <font color=blue>If</font color=blue> .Bookmarks(Ctr).Name = "EOD" <font color=blue>Then</font color=blue> Bmk2 = <font color=blue>True</font color=blue>
    <font color=blue>If</font color=blue> Bmk1 And Bmk2 <font color=blue>Then</font color=blue> <font color=blue>Exit</font color=blue> <font color=blue>For</font color=blue>
    <font color=blue>Next</font color=blue> Ctr
    <font color=blue>End</font color=blue> <font color=blue>With</font color=blue>
    <font color=blue>If</font color=blue> <font color=blue>Not</font color=blue> (Bmk1 And Bmk2) <font color=blue>Then</font color=blue> <font color=blue>GoTo</font color=blue> ErrorHandler
    <font color=blue>If</font color=blue> ActiveDocument.Bookmarks("EOD").Start _
    - ActiveDocument.Bookmarks("SOD").Start < 1 _
    <font color=blue>Then</font color=blue> <font color=blue>GoTo</font color=blue> ErrorHandler

    ExistDate = ActiveDocument.Range(ActiveDocument.Bookmarks("SOD ").Start, _
    ActiveDocument.Bookmarks("EOD").Start)
    myDate = Format(Date, "d mmmm yyyy")
    <font color=blue>If</font color=blue> myDate = ExistDate <font color=blue>Then</font color=blue> <font color=blue>Exit</font color=blue> <font color=blue>Sub</font color=blue>
    <font color=blue>If</font color=blue> MsgBox("Do we update the date?", vbYesNo + vbQuestion, myt) _
    = vbNo <font color=blue>Then</font color=blue> <font color=blue>Exit</font color=blue> <font color=blue>Sub</font color=blue>
    ActiveDocument.Range(ActiveDocument.Bookmarks("SOD ").Start, _
    ActiveDocument.Bookmarks("EOD").Start - 1).Select
    Selection.TypeText myDate
    Selection.Delete
    <font color=blue>Exit</font color=blue> <font color=blue>Sub</font color=blue>

    ErrorHandler:
    MsgBox "Bookmark(s) missing or improperly positioned.", vbCritical, myt
    <font color=blue>Exit</font color=blue> <font color=blue>Sub</font color=blue>

    <font color=blue>End</font color=blue> <font color=blue>Sub</font color=blue>
    </font color=black></code></div hiblock>
    Regards
    Don

  8. #8
    Star Lounger
    Join Date
    Sep 2007
    Posts
    54
    Thanks
    7
    Thanked 1 Time in 1 Post

    Re: Lock the Date Field (English/Word/2003)

    I understand what you're saying, but we don't save our documents through Word, we use Worldox - a document management system. It doesn't matter what you name the document, this system assigns the document name as a number, which is never duplicated. Therefore, it would create a lot of duplication on our system specifically every time we do a Save As, not to mention confusion to the end user (some are pretty junior at the system and use it as a glorified typewriter). It is a good idea though, and I may use it for other templates on this system.

    Thanks

  9. #9
    Star Lounger
    Join Date
    Sep 2007
    Posts
    54
    Thanks
    7
    Thanked 1 Time in 1 Post

    Re: Lock the Date Field (English/Word/2003)

    Okay, I think I got it. I have the date field on the template bookmarked and then declare a variable to equal the date field and only run the rest of the macro when the date <> the variable. Here is the code I used.

    Sub AutoOpen()

    'Macro designed to lock the dates on letters and memos to stop accidental changing
    Dim dtDoc As Date

    'Start at the beginning of the document
    Selection.HomeKey Unit:=wdStory

    'Search for the ReportDate bookmark
    If ActiveDocument.Bookmarks.Exists("ReportDate") = True Then
    ActiveDocument.Bookmarks("ReportDate").Select
    'If date field is not locked, then lock it
    If Selection.Fields.Locked = False Then
    Selection.Fields.Locked = True
    End If
    dtDoc = Selection
    'If the current date is different than the document date, then ask to update it
    'If the current date is the same, then don't ask and end macro
    If Date <> dtDoc Then
    Response = MsgBox("Would you like to change the date to today's date?", vbYesNo + vbQuestion)
    If Response = vbYes Then
    Selection.Fields.Locked = False
    Selection.Fields.Update
    Selection.Fields.Locked = True
    End If
    End If
    End If
    End Sub

    Thanks for all your help everyone.

  10. #10
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Lock the Date Field (English/Word/2003)

    Hi Maverick,

    here's a simpler version that should work:<pre>Sub AutoOpen()
    'Search for the ReportDate bookmark
    With ActiveDocument
    If .Bookmarks.Exists("ReportDate") = False Then Exit Sub
    With .Bookmarks("ReportDate").Range
    'If the current date is different than the document date, then ask to update it
    If Date <> .Text Then
    Response = MsgBox("Would you like to change the date to today's date?", _
    vbYesNo + vbQuestion)
    If Response = vbYes Then
    With .Fields(1)
    .Locked = False
    .Update
    .Locked = True
    End With
    End If
    End If
    End With
    End With
    End Sub</pre>

    Cheers,

    Paul Edstein
    [MS MVP - Word]

  11. #11
    New Lounger
    Join Date
    Mar 2008
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Lock the Date Field (English/Word/2003)

    Maverick,

    I receive an error on line starting

    Response =

    is highlighed '"Compile Error Variable Not Defined

    I am using 2007 and placed as AutoOpen in my Normal.dotm. In 2007.
    <img src=/S/confused3.gif border=0 alt=confused3 width=45 height=45>

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

    Re: Lock the Date Field (English/Word/2003)

    You can add the line

    Dim Response As Integer

    immediately below the line Sub AutoOpen().

    Alternatively, you can avoid the error by changing the code to:
    <code>
    Sub AutoOpen()
    'Search for the ReportDate bookmark
    With ActiveDocument
    If .Bookmarks.Exists("ReportDate") = False Then Exit Sub
    With .Bookmarks("ReportDate").Range
    'If the current date is different than the document date, then ask to update it
    If Date <> .Text Then
    If MsgBox("Would you like to change the date to today's date?", _
    vbYesNo + vbQuestion) = vbYes Then
    With .Fields(1)
    .Locked = False
    .Update
    .Locked = True
    End With
    End If
    End If
    End With
    End With
    End Sub
    </code>
    BTW, it's actually very good that you got this error message. It means that you require all variables to be declared explicitly, and that is an excellent idea! <img src=/S/thumbup.gif border=0 alt=thumbup width=15 height=15>

  13. #13
    New Lounger
    Join Date
    Mar 2008
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Lock the Date Field (English/Word/2003)

    Hans thank you I try,

    Yes that works as far as asking me to update date, thank you.

    My only issue now is it does not in 2007 lock the field on my 1 page template after creating a New Document from the template, everything EXCEPT the DATE field (todays date) all CCs are marked no edit/no delete after XML datafill.
    I answered Yes and the field updated to today's date but if I place my cursor on the date field I can delete the date/code by using delete. The code as is does not lock DATE field is that normal?
    I've changed the bookmark TodaysDate for Date Field which is neither here nor there.

    Any ideas why?


    Any suggestions as to why my DATE won't lock DATE field with bookmark TODAYSDATE?
    Attached Files Attached Files

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

    Re: Lock the Date Field (English/Word/2003)

    The code doesn't run when the document is created, only when the document (after having been saved and closed) is reopened.

    The code locks the date field against being updated, not against being deleted. If you want to prevent the field from being deleted, you'll have to protect the document for forms and place the field in a protected section. (Perhaps Word 2007 has other ways of protecting part of a document, but I can't help you with that).

    BTW, I think the line

    If Date <> .Text Then

    should be

    If Date <> DateValue(.Text) Then

    to ensure that the code runs independently of the system date format.

  15. #15
    New Lounger
    Join Date
    Mar 2008
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Lock the Date Field (English/Word/2003)

    Updated not deleted (my mix up) yes the code does run after that's correct. But I was confusing locked with "and being deleted also".

    Thanks Hans

Posting Permissions

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