Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Date Variable & Calculation - Please Help. Thanks.

Status
Not open for further replies.

bzss7x

MIS
Mar 14, 2001
17
US
Need help, please! I am helping an OB/GYN with a word problem.

Requirement:
1. Open Word Doc and be greeted with Input Box for Due Date.
2. Have references to that entered date and calculations from it throughout document.

Example:
Input Box will assign due date (e.g. 8/23/06).
First Trimester Date (2/8/06) should be calculated (8/23/06 - 196 days), etc.

How is this done? I looked into Document Properties and Document Variables, but my macro skills aren't that great.

Any help will be greatly appreciated.
 
There are two ways (actually there are more...) you could go.

However, the first thing is to have an Inputbox to get the starting date (this will be the variable dteDueDate). If you have this in the Document_Open event then whenever the document is opened, the inputbox will ask for the due date. To get around having the Inputbox open after you have already set a due date, you can use a doc variable, and check for that. Here is some code.
Code:
Private Sub Document_Open()
Dim dteDueDate As Date
Dim dteFirstTrimester As Date
[COLOR=red]' declare other date variables[/color red]
Dim aVar As Variable
Dim num As Integer

For Each aVar In ActiveDocument.Variables
    If aVar.Name = "DueDateSet" Then num = aVar.Index
Next aVar
If num = 0 Then
    ActiveDocument.Variables.Add Name:="DueDateSet", Value:=1
Else
    Exit Sub
End If

dteDueDate = InputBox("Please enter due date in day/month/year format.")
dteFirstTrimester = dteDueDate – 196

[COLOR=red]' calculate other dates[/color red]

[COLOR=red]' use formfields procedure[/color red]
Call FillData_A(dteDueDate, "DueDate")
Call FillData_A(dteFirstTrimester, "FirstTrimester")

[COLOR=red]' use bookmarks procedure[/color red]
Call FillData_B(dteDueDate, "DueDate2")
Call FillData_B(dteFirstTrimester, "FirstTrimester2")
End Sub

Sub FillData_A(DataDate As Date, FormFieldName As String)
ActiveDocument.FormFields(FormFieldName).Result = DataDate
End Sub

Sub FillData_B(DataDate As Date, BK_Name As String)
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
    ActiveDocument.Unprotect
End If
On Error Resume Next
  With Selection
    .GoTo what:=wdGoToBookmark, Name:=BK_Name
    .Collapse Direction:=wdCollapseEnd
    ActiveDocument.Bookmarks(BK_Name).Range.Text = DataDate
       .MoveEnd unit:=wdCharacter, Count:=Len(DataDate)
    ActiveDocument.Bookmarks.Add Name:=BK_Name, _
        Range:=Selection.Range
    .Collapse Direction:=wdCollapseEnd
  End With
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True, Password:=""
End Sub
There are two procedures. One to put the data into Formfields, the other to put the data into bookmarks. In the code above I have used both to demonstrate both. You do not need both, one will do. There are advantages and disadvantages to either route.


Gerry
 
Shouldn't
Code:
 dteFirstTrimester = dteDueDate – 196
be
Code:
 dteFirstTrimester = dateadd (d, -196, dteDueDate)
?

Also, how do I display the results? Do I use {DueDate} or what? Thanks for all assistance.
 
Huh? Why do you say that?????

1. I have absolutely NO idea what "dateadd" is (or does) ...and since I don't...why would I use it?

2. I have absolutely NO idea what "d" is (or does)...and since I don't...why would I use it?

Especially as I have no need for them.

dteDueDate is...well a date. What do you call a date that is 196 days less than another date? Date - 196.

Hmmmm.

How do you display anything? First of all - what do you mean "display"? Do you mean have it as text in a document? Do you mean have it display in a message box?

If you want it in the document...put it there. In the case above, it IS there.

If you want to have it in a messagebox...put it in a messagebox. See Msgbox in VBA Help.

If you want to display the result of a formfield (Option A above), the result of a formfield is:

Code:
ActiveDocument.Formfields("[i]formfield_name[/i}")Result

If you want to get the text of a bookmark (Option B above), the text of a bookmark is:
Code:
ActiveDocument.Bookmarks("[i]bookmark_name[/i}").Range.Text

Why do you want to display something?

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top