Merging, variables and fields problem

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
 
D

Doug Robbins - Word MVP

I am not sure it will work in a mailmerge situation any way, I think that
you would need to be using mailmerge events as a minimum and I have not been
able to get them to work reliably though there is some research going into
that problem at the moment. In addition however, while I doubt that
Document_Close is the place for it, you are not saving the document after
changing the value of the variable. So, I would not expect it to work for
that reason if for no other.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top