Problem with IF Conditions during mail merge

V

Vimal

Hi,

Am trying to generate documents through mail merge and are having
problems problems with the mail merge fields embedded in IF
conditions.

Because the number of documents which the user can generate varies(the
maximum is about 60 to 70) and thereby affect the processing time for
the values of the merge fields, we read the merge fields from the
selected documents and process only those merge fields. I haven't
encountered any problems with the normal merge fields except for the
field embedded in 'IF conditions'. The following is the code am using
for this purpose:

Set objWordDocApp = New Word.Application
Set objWordDoc = objWordDocApp.Documents.Open(strFileName)

For Each objMMField In objWordDOC.MailMerge.fields
objMMField.Select
strTemp = strTemp & "~" & objMMField.Code.Text
Next

In case of IF conditions, the following sample text is returned:
IF  MERGEFIELD StringLesseeDebtor  = "Lessee" "lease" "finance"

Is there some way how I can just get the merge field alone e.g.
MERGEFIELD StringLesseeDebtor?

Also, can you please give me an insight into how to read these fields
when the levels of nesting gets more complex.

Been banging my head on this for quite some time and would appreciate
any help in this regard.

Thanks in advance.
Vimal
 
P

Peter Jamieson

It isn't easy, partly because you have to work with the Selection because
you can't get the Range of a Field using e.g. oField.Range.

You might find the following article by Dave Rado at the Word MVPs site
useful (you should also note that dealing with fields in Headers/Footers may
be even harder):

http://word.mvps.org/faqs/macrosvba/NestedFieldsWithVBA.htm

I don't have any tested general-purpose code that deals with nested fields.
I threw the following together as a starting point - you would need to test
the approach. I expect a more experienced Word/VBA coder could do much
better!

Sub ListFieldCodesInMainDocument()
Dim bIsOutermostRange As Boolean
bIsOutermostRange = True
Call ListInnerFields(ActiveDocument.Range.Fields, bIsOutermostRange)
End Sub

Sub ListInnerFields(oOuterFields As Word.Fields, bIsOutermostRange As
Boolean)
Dim oInnerField As Word.Field
Dim oInnerFields As Word.Fields
Dim bFirstFieldInRange As Boolean
' The way I have done things, we need to eliminate the first field in the
collection
' unless we are dealing with the outermost collection
bFirstFieldInRange = True
For Each oInnerField In oOuterFields
If bFirstFieldInRange And Not bIsOutermostRange Then
bFirstFieldInRange = False
Else
oInnerField.Select
If Selection.Range.Fields.Count > 1 Then
' nested field
' do something with the outer field
Debug.Print oInnerField.Code
Set oInnerFields = Selection.Range.Fields
Call ListInnerFields(oInnerFields, False)
Set oInnerFields = Nothing
Else
' do something with the inner field
Debug.Print oInnerField.Code
End If
End If
Next

End Sub
 

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