Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Questions on calculated date fields

    By searching various sites I have found a lot of the info on calculated date fields in MS Word including the complex (but slick) field code approaches and the VBA macro approach. I have had partial success with both but don't understand why what I have is not working.

    I have a Word document I use as a template. It is not a Word Template per se, just a .docx.

    I elected to use the CreateDate fieldcode in one cell and an autonew macro to populate the cell beneath it in the table. The goal is to set the create date as the initial date for a services quote and the cell beneath is the quote expiration date, typically one month later.

    The CreateDate field code { CreateDate \@ "MMMM dd, YYYY" } works fine but the autonew macro does not appear to be working. I defined the target cell as a bookmark called ExpireDate and put in the macro as follows

    Sub AutoNew()
    Selection.GoTo What:=wdGoToBookmark, Name:="ExpireDate"
    Selection.InsertBefore Format((CreateDate + 30), "MMMM dd, yyyy")
    End Sub

    My expectation is that when I save the document as a new name, the CreateDate field would update to the current date in the desired format and the autonew macro would update the value at the bookmark.

    To test this I tried creating a minimalist macro:

    Sub InsertEndDate()
    '
    ' InsertEndDate Macro
    '
    '
    Selection.InsertBefore Format((CreateDate + 30), "MMMM dd, yyyy")
    End Sub

    I deleted the value in the cell, leaving the cursorin the field, then manually ran the macro InsertEndDate, nothing happened. The nbookmark still seems ot exist, not sure what I am doing wrong.

    Any thoughts would be welcome. I am a total novice at VBA, typically able to decipher the logic but know nothing about syntax.

    Thanks for any comments.

  2. #2
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Madison, Wisconsin, Wisconsin, USA
    Posts
    1,708
    Thanks
    57
    Thanked 65 Times in 63 Posts
    First, I would advise actually using a template rather than a document.
    Next, read and use the fields in the tutorial pinned at the beginning of this forum.
    Charles Kyle Kenyon
    Madison, Wisconsin

  3. #3
    New Lounger
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Mr. Kenyon,

    Thanks for both suggestions. Before posting I had in fact found the pinned thread and downloaded the zip file with the info on calculated dates, the information was most insightful. I must have overlooked something in the document or failed to comprehend some of the info as I was not entirely successful in adapting the info to my use. This led me to post my question seeking further enlightenment.


    With respect to setting up the doc as a template, I agree that would probably be the wise thing. Historically, i have not used the form sufficiently to justify spending the time to learn all the tricks of templates. I am a rare user of Word and have not worked with templates or form construction for years.

    Recently, my use of this form has increased and it seemed reasonable to try to automate some of the minor annoyances that previously were acceptable. Hence my search for info that brought me here.

    I will move on and continue my search and see what I can figure out.


    Thanks again for taking the time to read and respond to my post.

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,534
    Thanks
    3
    Thanked 147 Times in 140 Posts
    Your AutoNew macro won't automatically trigger when you do a save as. It will if you create a new document by 'opening' a template.

    As Charles advised, you should save your document as a template. Besides allowing the AutoNew to run automatically, it also enables the resulting documents to be saved in docx format since they don't need to carry the VBA code which just resides in the template. If you keep creating copies of the docm file then you will have multiple copies of the code which makes it very hard to update.

    The code you should be using to insert the date goes along the lines of the following
    Code:
    Sub InsertEndDate()
      Dim aDte As Date
      aDte = ActiveDocument.BuiltInDocumentProperties(wdPropertyTimeCreated)
      ActiveDocument.Bookmarks("ExpireDate").Range.InsertBefore Format((aDte + 30), "MMMM dd, yyyy")
    End Sub
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    New Lounger
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Aha, I get it. I will work on setting it up as a template. I understand your point about the repeating code. The form is inherited from my manager but I thinkn I can build my own template for my own use.

    Thanks for your enlightening comments.

Posting Permissions

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