Reputation: 2308
I have a form in Word 2010 and am trying to enter two dates and get the difference in days. I tried creating a table with one row and three columns. Instead of dates I entered numbers in the first and second column and in the third I entered the formula =B1 - A1
. With numbers it works, but not with dates.
Is it possible to enter two dates and get the difference in days in Word 2010? I know I can easily do this in Excel, but since the form is extensive I would like to try to do it in Word.
Upvotes: 1
Views: 4440
Reputation:
There are at least 3 types of "inline forms" in Word 2010. All of them let you detect changes to fields in VBA.
Of those 3 options, only legacy forms are available on any version of Mac Word, and VBA is not avaiable on Mac Word 2008.
If you want to avoid VBA and use fields, there are two main considerations:
You can work around point (1) by converting the dates to Julian Day Numbers, and using { = } to subtract the resulting numbers. See below for some suggested code.
You can avoid the problem in point (2) as long as you use form fields to enter the dates. Then you can use the form field's bookmark name to get the text and the date field switch "\@" to get the day. month and year numbers.
So let's suppose your two date form fields are called DA and DB. Then you can insert the following field codes at the point in the document where you want the result - notice that all the { } pairs have to be the special field code braces that you can enter using ctrl-F9.
{ SET MA { DA \@M }
}{ SET CA { =INT((14-MA)/12) }
}{ SET XD { ={ DA \@YYYY }-CA }
}{ SET XC { =INT(XDA/100) }
}{ SET XB { =XD-XC*100}
}{ SET XA { =MA+12*CA-3 }
}{ SET JA { =INT(146097*XC/4) + INT(36525*XB/100) + INT((153*XA+2)/5) + { DA \@D } + 1721119 }
}{ SET MB { DB \@M }
}{ SET CB { =INT((14-MB)/12) }
}{ SET YD { ={ DB \@YYYY }-CB }
}{ SET YC { =INT(YD/100) }
}{ SET YB { =YD-YC*100 }
}{ SET YA { =MB+12*CB-3 }
}{ SET JB { =INT(146097*YC/4) + INT(36525*YB/100) + INT((153*YA+2)/5) + { DB \@D } + 1721119 }
}{ =JB-JA }
A few notes:
There is a much more complete paper by "macropod" on using fields for date calculations. It uses a slightly different (and probably shorter) version of the algorithm, but the paper also provides algorithms for going in the other direction, and so on. I think you have to log in to get a copy from here , but you may be able to find it elsewhere.
Upvotes: 1