by Derek Pinkerton
6. June 2006 11:07
Last week I had a customer that wanted to add a calculated field "Effective Date" to his Word documents that would automatically be updated with each revision of the document. This field would be calculated as the published date of the document plus 2 weeks. QualTrax itself will only enter the actual date of an event (such as published) so in order to get a date calculated based upon the published date I had to turn to Word. In researching this I found that there were two ways to do to this. The first method is pretty straight-forward; you simply write a vb macro to get the date published (inserted automatically by QualTrax as a Document Property) add two weeks and store this date into a new Document Property, which can then be accessed in fields in the same way as all other QualTrax variables.
Method 1 (Macro): With this method you would add a macro to each document that would calculate the "Effective Date" based upon the Published Date and store it into a new document variable called ##EFFECTIVE_DATE##. This macro would be setup to run automatically each time the document is opened so that you are always viewing up to date information. the problem with this is that if the person to open this document has their Word security setting set to anything higher than low, they will get a warning message asking them to enable/disable the macros. If they decide to disable the macros within the document, the information will not get updated and they may be looking at an old copy of the data. If you have the document converted to PDF using the PDF Module you will not have this problem.
The steps to implement method 1 are as follows:
1) Open the document in question and go do "File->Properties" and click on the "Custom" tab.
2) In the "Name" field enter "##EFFECTIVE_DATE##" without the quotes.
3) Select "Date" in the "Type" drop-down list.
4) Enter any date in the "Value" field. It doesn't matter what date you enter as this will automatically be overwritten when the macro runs.
5) Click the "Add" button then click "Ok"
6) Insert the field into the appropriate place in the document using the "Insert->Field" command like you normally do.
7) Go to "Tools->Macro->Macros" if you have already entered the "AutoOpen" macro select it and click Edit, if not type "AutoOpen" into the Macro Name field and click the Create button.
8) Overwrite the auto-generated code with the following:
Sub AutoOpen()
Set tmpProps = ActiveDocument.CustomDocumentProperties
dPublishedDate = tmpProps.Item("##DATE_PUBLISHED##").Value
dEffectiveDate = DateAdd("d", 14, dPublishedDate)
tmpProps.Item("##EFFECTIVE_DATE##").Value = dEffectiveDate
Dim aStory As Range
Dim aField As Field
For Each aStory In ActiveDocument.StoryRanges
For Each aField In aStory.Fields
aField.Update
Next aField
Next aStory
ActiveDocument.Saved = True
End Sub
Method 2 (Field Code): With this method you would add in a field using the "Insert->Field" command and put in some custom field codes that will calculate the effective date. This is much more difficult than using the macro because you cannot simply copy and paste the field code into the Word document. Most of it can be copied and pasted however each pair of curly braces {} would have to be entered by pressing ALT+F9 which turns out to be a bit of a pain especially with how long the field codes are.
{QUOTE
{SET Delay 14}
{SET a{=INT((14-{DOCPROPERTY ##DATE_PUBLISHED## \@ M})/12)}}
{SET b{={DOCPROPERTY ##DATE_PUBLISHED## \@ yyyy}+4800-a}}
{SET c{={DOCPROPERTY ##DATE_PUBLISHED## \@ M}+12*a-3}}
{SET d{DOCPROPERTY ##DATE_PUBLISHED## \@ d}}
{SET jd{=d+INT((153*c+2)/5)+365*b+INT(b/4)-INT(b/100)+INT(b/400)-32045+Delay}}
{SET e{=INT((4*(jd+32044)+3)/146097)}}
{SET f{=jd+32044-INT(146097*e/4)}}
{SET g{=INT((4*f+3)/1461)}}
{SET h{=f-INT(1461*g/4)}}
{SET i{=INT((5*h+2)/153)}}
{SET dd{=h-INT((153*i+2)/5)+1}}
{SET mm{=i+3-12*INT(i/10)}}
{SET yy{=100*e+g-4800+INT(i/10)}}
{=dd*10^6+mm*10^4+yy \# "00'-'00'-'0000"} \@ "dddd, d MMMM yyyy"}
I made each of the curly braces in the above code bold so you can tell them apart easier. Again each of these pairs of curly braces must be entered in Word by pressing ALT+F9 rather than typing them in.
You would also need to keep in mind that Word 2003 does not automatically update these fields so in order to force an update in this version of Word you would need to add in a macro that would do this.
Although you do have the security warning to worry about, method #1 would be my recommendation. It would be easier to implement and can easily be copied from one document to another.
*Credit for the Word field codes above goes to Woody's Lounge. See this forum post for more information.
0d85948b-4afb-4217-9844-b8bdedc8318e|0|.0
Tags: qualtrax, word