Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2005

Tip: Looking for answers? Try searching our database.

Merging, variables and fields problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
James A. Hicks - 03 Nov 2005 20:33 GMT
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
Doug Robbins - Word MVP - 04 Nov 2005 06:39 GMT
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.

Signature

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

>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
[quoted text clipped - 43 lines]
>
> jim hicks
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.