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 / April 2006

Tip: Looking for answers? Try searching our database.

Please help - Missing headers in mail merge

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Earl B - 18 Apr 2006 00:22 GMT
I have a VB.Net tool that is performing a programmatic mail-merge, and one
of my users is putting the merge fields in the header for the document.

For some reason, this isn't working.  When I debug the merge process, there
are *no* merge fields listed in the document, but the finished document has
the fields listed.

Does this sound familiar to anybody?  Can someone give me a link to an
answer?  I'm under the gun on this one, and your help is GREATLY appreciated!

EB
Peter Jamieson - 18 Apr 2006 08:16 GMT
1. Can you spell out what you mean by

"When I debug the merge process, there
are *no* merge fields listed in the document, but the finished document has
the fields listed."

There's a starting document called the Mail Merge Main Document, and a
result document (when you merge to a new document, that is). Which contains
what, and how are you finding out?

2. The chances are the the .NET tool is not looking for fields in all the
StoryRanges in the document. Detecting /all/ fields in a document is
non-trivial (in fact, evidence suggests that it may not be possible) but the
following code should catch most of them:

Dim objStory As Range
Dim objField As Field

For Each objStory In ActiveDocument.StoryRanges
 For Each objField In objStory.Fields
   ' do what you want to the field here
 Next
' The header/footer ranges can be linked in a way
 ' that is not revealed by the outer For Each
 ' so we have to do the following
 While Not (objStory.NextStoryRange Is Nothing)
   Set objStory = objStory.NextStoryRange
   For Each objField In objStory.Fields
     ' do what you want to the field here
   Next
 Wend
Next objStory

Set objStory = Nothing
Set objField = Nothing

2. What kind of merge is the user performing? Fields in the header/footer
are likely to behave differently in Catalog/Directory merges than they do in
Letter merges.

Peter Jameieson

>I have a VB.Net tool that is performing a programmatic mail-merge, and one
> of my users is putting the merge fields in the header for the document.
[quoted text clipped - 10 lines]
>
> EB
Earl B - 18 Apr 2006 14:33 GMT
I am looking at the main document *before* beginning the merge:

Dim docSource As Word.DocumentClass = GetObject(SourceFile)

''' Do the mail merge
merge = docSource.MailMerge
With merge
 For Each field As Word.MailMergeField In .Fields
   Dim txt As String = field.Code.Text.Trim()
   If txt.StartsWith("MERGEFIELD") Then
''' Process the field
   End If
 Next
end with

I set a breakpoint on the "erge=..." line, then use watches to explore the
document tree.  In particular, docSource.merge.fields.count == 0 (which
proves true when I step through the code, it skips over the "for each...")

Thank you for the code snippett - I'll be exploring it today.

EB
Earl B - 18 Apr 2006 16:17 GMT
That CERTAINLY helped!  I still have some content that is in TextFrame
objects (I ~think~ it's in textframe objects), but I can mandate "don't use
those" easily enough.

Thanks!
Peter Jamieson - 18 Apr 2006 16:43 GMT
Good.

You may be able to find more fields by looking in the shapes in the
headers/footers and Text Frames, using e.g.

Dim iStories As Integer
Dim objSection As Section
Dim objHeaderFooter As HeaderFooter
Dim r As Range
Dim s As Shape
For Each objSection in ActiveDocument.Sections
 For Each objHeaderFooter in objSection. Headers
   For Each s in objHeaderFooter.Shapes
     If s.TextFrame.HasText Then
       ' use s.TextFrame.TextRange to find the fields
     End If
  Next s
 Next objHeaderFooter
 For Each objHeaderFooter in objSection.Footers
   For Each s in objHeaderFooter.Shapes
     If s.TextFrame.HasText Then
       ' use s.TextFrame.TextRange to find the fields
     End If
  Next s
 Next objHeaderFooter
Next objSection
End Sub
For Each r in ActiveDocument.StoryRanges
 If r.StoryType = wdTextFrameStory Then
   While Not (r.NextStoryRange Is Nothing)
      Set r = r.NextStoryRange
   Wend
 End If
Next r

Fields in text boxes can be a bit dodgy enyway, although sometimes they may
be the only way to do certain things. Fields in old-style Word frames are
generally a better idea if the user has to use out-of line texts.

Peter Jamieson

> That CERTAINLY helped!  I still have some content that is in TextFrame
> objects (I ~think~ it's in textframe objects), but I can mandate "don't
> use
> those" easily enough.
>
> Thanks!
Earl B - 18 Apr 2006 19:30 GMT
Thanks - I'll be looking into that the next time these issues come up; for
now it's an in-house tool and I've crossed the "good enough" threshold.

Thanks for the tips!

EB
 
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.