J
James A. Hicks
I have a software package that will merge data into MS Word documents to
prepare client letters and other documents. I am trying to do something
that is seemingly very easy but just will not work.
Assume my mailmerge master document contains three mergefields - mergefield
F1, mergefield F2 and mergefield F3 and a docvariable field called "sebas."
Mergefield F2 is a field that contains a date. I want to create and merge a
letter that is to be dated two weeks earlier than the date contained in
mergefield F2. The following is VBA code that I put in the master document:
Private Sub Document_Open()
With ActiveDocument
.Variables.Add Name:="sebas", Value:="ghjhjdj"
End With
MsgBox ActiveDocument.Variables("sebas").Value
End Sub
Private Sub Document_Close()
For Each mfield In ActiveDocument.MailMerge.DataSource.DataFields
MsgBox mfield.Name & " " & mfield.Value
If mfield.Name = "F2" Then
charlie = mfield.Value
End If
Next mfield
ActiveDocument.Variables("sebas").Value = DateAdd("ww", -2, charlie)
MsgBox ActiveDocument.Variables("sebas").Value & " " & charlie
ActiveDocument.Fields.Update
End Sub
The MsgBox in the open and close subs are simply there to allow me to verify
the value of "sebas" and "charlie".
This code will properly capture the date from F2 and assign it to "charlie".
It will then subtract two weeks from "charlie" and assign that value to
"sebas". However, it will not update the docvariable field "sebas" in the
newly created letter. That is what is driving me crazy. The filler value
"ghjhjdj" is set as the value of "sebas" when the variable is added to the
document and never changes, even after the Document_Close sub runs.
Am I missing something important here?
Do I want to place "sebas" in the document in another manner?
Is the document_close procedure too late to update the docvariable field?
Thanks in advance for your ideas.
jim hicks
prepare client letters and other documents. I am trying to do something
that is seemingly very easy but just will not work.
Assume my mailmerge master document contains three mergefields - mergefield
F1, mergefield F2 and mergefield F3 and a docvariable field called "sebas."
Mergefield F2 is a field that contains a date. I want to create and merge a
letter that is to be dated two weeks earlier than the date contained in
mergefield F2. The following is VBA code that I put in the master document:
Private Sub Document_Open()
With ActiveDocument
.Variables.Add Name:="sebas", Value:="ghjhjdj"
End With
MsgBox ActiveDocument.Variables("sebas").Value
End Sub
Private Sub Document_Close()
For Each mfield In ActiveDocument.MailMerge.DataSource.DataFields
MsgBox mfield.Name & " " & mfield.Value
If mfield.Name = "F2" Then
charlie = mfield.Value
End If
Next mfield
ActiveDocument.Variables("sebas").Value = DateAdd("ww", -2, charlie)
MsgBox ActiveDocument.Variables("sebas").Value & " " & charlie
ActiveDocument.Fields.Update
End Sub
The MsgBox in the open and close subs are simply there to allow me to verify
the value of "sebas" and "charlie".
This code will properly capture the date from F2 and assign it to "charlie".
It will then subtract two weeks from "charlie" and assign that value to
"sebas". However, it will not update the docvariable field "sebas" in the
newly created letter. That is what is driving me crazy. The filler value
"ghjhjdj" is set as the value of "sebas" when the variable is added to the
document and never changes, even after the Document_Close sub runs.
Am I missing something important here?
Do I want to place "sebas" in the document in another manner?
Is the document_close procedure too late to update the docvariable field?
Thanks in advance for your ideas.
jim hicks