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 / Mailmerge and Fax / October 2004

Tip: Looking for answers? Try searching our database.

Problem with IF Conditions during mail merge

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Vimal - 18 Oct 2004 03:08 GMT
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
Peter Jamieson - 18 Oct 2004 11:57 GMT
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

Signature

Peter Jamieson

> Hi,
>
[quoted text clipped - 32 lines]
> Thanks in advance.
> Vimal
 
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.