Mailmerge Value in to a variable

G

Gaz uk

Hi

I'm having trouble getting the value of a mailmerge field in to a variable.
I think I may be approaching this the wrong way but as a test I put the
following code into a template and instead of the actual value for CCode I
get <<ClientCode>>

Private Sub Document_New()
For Each mmField In ActiveDocument.Fields
If mmField.Code = " MERGEFIELD ClientCode " Then
CCode = mmField.Result
x = MsgBox("Client Code is " & CCode, vbOKOnly)
End If
Next mmField
End Sub

Is it that the value isn't coming in because the data hasn't merged yet?

I'm sure there is probably a straight forward answer but have dug around and
found nothing that works.

Thanks in advance

Gaz
 
D

Doug Robbins - Word MVP

Probably better if you tell us what you want to do with the variable.

--
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
 
G

Gaz uk

The word template is opened by a third party app that somehow pushes through
the mailmerge fields to create the document. I want to take some of the
values and check them, which if incorrect will close the document and not
allow it to be created. In other words I'm forcing the user to fill out the
info in the 3rd party app otherwise it won't let them create the document.

Hope this explains ok

Gaz
 
D

Doug Robbins - Word MVP

You may (small letters) be able to do what you want with mailmerge events.
For an exampe of their use, take a look at the add-in that you can download
from

http://www.gmayor.com/individual_merge_letters.htm


You are probably better off to work directly with the data source however
before the merge, rather than try and do it via the mail merge main
document.

--
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
 
P

Peter Jamieson

The result depends on the state of

ActiveDocument.MailMerge.ViewMailMergeFieldCodes

You can also consider getting the current value of the mailmerge field using
e.g.

ActiveDocument.MailMerge.DataSource.DataFields("ClientCode")

Peter Jamieson
 
G

Gaz uk

Tried both of these and got an error for both, (Runtime error 5852.
Requested object is not available)

Any other thoughts appreciated

Thanks

Gaz
 
P

Peter Jamieson

That suggests that at the point you are running the code, the Word document
is not connected to the data source (and is not strictly speaking a mail
merge main document).

I suspect that to get any worthwhile results you will have to connect to the
datasource first. Because the merge is being done by a third party
application, it's difficult to know how you might achieve that. Have you
been able to work out how the app. is providing data to the Mailmerge
"application" ? Is it creating a temporary text file, or what?

Peter Jamieson
 

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