Please help - Missing headers in mail merge

E

Earl B

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
 
P

Peter Jamieson

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
 
E

Earl B

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
 
E

Earl B

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

Peter Jamieson

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
 
E

Earl B

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
 

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