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 / February 2004

Tip: Looking for answers? Try searching our database.

Replacing merge fields in headers/footers

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AP - 10 Feb 2004 00:21 GMT
Hi,

I'm trying to programmatically update merge fields in headers/footers of a
word document. I iterate through the merge fields in the header/footer, and
set the result text to the text I want, then save the document, but when I
open it the merge field does not appear updated. If anyone can shed some
light on why this is not working I would greatly appreciate it.

Thanks,

Adam
AP - 10 Feb 2004 01:16 GMT
I just wanted to add that I use the same code to replace merge fields in the
regular body of the document and it works fine, just not in the
header/footer.

Adam

> Hi,
>
[quoted text clipped - 7 lines]
>
> Adam
Peter Hewett - 10 Feb 2004 04:58 GMT
Hi Adam

I'm not sure I understand what you're doing or why you're doing it! Normally
you use Merge fields in the Main Document of a mail merge. So why are you
trying to programatically update a Merge Field? Merge fields are where data
from the mail merge Data Source will be inserted in the output.

You can programatically control which records you want to merge, but not
which fields get updated by each merged records.

Can you explain what it is you are trying to achieve rather than how you are
going about it? I'm wondering whether you shouldn't be using a different type
of field.

Cheers - Peter

> I just wanted to add that I use the same code to replace merge fields in
> the regular body of the document and it works fine, just not in the
[quoted text clipped - 16 lines]
>>
>> Adam
Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL ADDRESS - 10 Feb 2004 05:28 GMT
Hi Adam,

It will be easier to replicate/solve your problem if you paste the code into
a message that you post back to the newsgroup as a continuation of this
thread.

Signature

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested.  Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP

>I just wanted to add that I use the same code to replace merge fields in
>the
[quoted text clipped - 17 lines]
>>
>> Adam
AP - 10 Feb 2004 15:31 GMT
What we are doing is using merge fields to indicate where we want to
programmatically update data in a document from a database. The user selects
to retrieve what we call a template document from the server for a certain
"file number" in our database. The server then updates our custom merge
fields with data from the database and sends the document down to the client
where it is opened in word. Merge fields seemed like the logical choice
because we can update the result and still maintain the underlying link to
the database through the merge field. Thus if data changes in the database,
the next time the document is pulled, the document will reflect any changes
that have been made. The following is an example of the code we're using:

public void PopulateDocument(some parameters) {
//Open up word with a new document from the template

wa = new Word.ApplicationClass();

wd = wa.Documents.Add(ref templateFile, ref missing, ref missing, ref
trueObject);

wd.Activate();

//Iterate through the fields in the word doc, pull the data from the db and

//update the fields in the word doc

//update the headers and footers

//this code does not work - not sure why the document fields are different
from the header/footer fields in the first place...

foreach (Field hff in
wd.Sections.First.Headers.Item(Word.WdHeaderFooterIndex.wdHeaderFooterPrimar
y).Range.Fields) {

UpdateField(hff,theFileID,theUserID,theOfficeID,theLedgerEntryID);

}

//this code works, but not for the header/footer fields

foreach (Field f in wd.Fields) {

UpdateField(f,theFileID,theUserID,theOfficeID,theLedgerEntryID);

}

}

private void UpdateField(Word.Field theField, int theFileID, int theUserID,
int theOfficeID, int theLedgerEntryID) {

string command = theField.Code.Text.Trim();

//if the merge field starts with the text of our custom command

int index = command.IndexOf(COMMAND_RESWARE);

if (index != -1) {

//populate the result from the database

theField.Result.Text = GetReplacementText(theFileID, theUserID, theOfficeID,

theLedgerEntryID, command.Substring(index + 1 +
COMMAND_RESWARE.Length).Trim());

}

}

> Hi Adam,
>
[quoted text clipped - 23 lines]
> >>
> >> Adam
AP - 17 Feb 2004 22:39 GMT
No solution to my problem then?

> What we are doing is using merge fields to indicate where we want to
> programmatically update data in a document from a database. The user selects
[quoted text clipped - 27 lines]
>
> foreach (Field hff in

wd.Sections.First.Headers.Item(Word.WdHeaderFooterIndex.wdHeaderFooterPrimar
> y).Range.Fields) {
>
[quoted text clipped - 65 lines]
> > >>
> > >> Adam
Charles Kenyon - 17 Feb 2004 23:21 GMT
I believe that in your code, wd ends up being the equivalent of
ActiveDocument. ActiveDocument.Fields.Update will _not_ update fields in the
headers/footers.

The headers/footers are not in the main story range. You could use StyleRef
fields in them to pick up info that is displayed in the main document.

How many sections does your merge document have? Probably at least as many
as you have records being pulled.

You can cycle through the sections and the story ranges to update merge
fields.

Signature

Charles Kenyon

See the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.

> What we are doing is using merge fields to indicate where we want to
> programmatically update data in a document from a database. The user selects
[quoted text clipped - 27 lines]
>
> foreach (Field hff in

wd.Sections.First.Headers.Item(Word.WdHeaderFooterIndex.wdHeaderFooterPrimar
> y).Range.Fields) {
>
[quoted text clipped - 65 lines]
> > >>
> > >> Adam
AP - 18 Feb 2004 06:27 GMT
Yeah the problem is even if I cycle through the header/footer merge fields
and set Result.text to what I want it to be, it still does not update those
fields, as it does in the ActiveDocument fields.

> I believe that in your code, wd ends up being the equivalent of
> ActiveDocument. ActiveDocument.Fields.Update will _not_ update fields in the
[quoted text clipped - 45 lines]
> >
> > foreach (Field hff in

wd.Sections.First.Headers.Item(Word.WdHeaderFooterIndex.wdHeaderFooterPrimar
> > y).Range.Fields) {
> >
[quoted text clipped - 73 lines]
> > > >>
> > > >> Adam
Charles Kenyon - 18 Feb 2004 18:06 GMT
If you cycle through the sections and through the story ranges in each
section (or at least the h/f ranges) and update the fields in those
sections, they will update. I've done this in Word 97, 2000, 2002, and 2003.

I'm not sure what you mean by "set Result.txt" to what you want.

The (pseudo)code you posted would only pick up fields in the first section
(if that).

Try stepping through your code.

If this doesn't work, I recommend posting again, only in the
word.vba.general newsgroup, including your actual code rather than
pseudocode.

Signature

Charles Kenyon

See the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.

> Yeah the problem is even if I cycle through the header/footer merge fields
> and set Result.text to what I want it to be, it still does not update those
[quoted text clipped - 55 lines]
> > >
> > > foreach (Field hff in

wd.Sections.First.Headers.Item(Word.WdHeaderFooterIndex.wdHeaderFooterPrimar
> > > y).Range.Fields) {
> > >
[quoted text clipped - 82 lines]
> > > > >>
> > > > >> Adam
Chip Orange - 10 Feb 2004 16:21 GMT
Here's some code that someone else was once kind enough to publish here on
the forums:

Public Sub UpdateFieldsInHeaders()
   Dim Story As Variant
   Dim rngNext As Word.Range

   ' Iterate through all story types
   For Each Story In ActiveDocument.StoryRanges

       ' Only Update fields in a header
       If Story.StoryType = wdPrimaryHeaderStory Or _
          Story.StoryType = wdFirstPageHeaderStory Or _
          Story.StoryType = wdEvenPagesHeaderStory Then

           ' Update fields in this header
           Story.Fields.Update

           ' There may be linked headers so Update them as well
           Set rngNext = Story.NextStoryRange
           Do Until rngNext Is Nothing

               ' Update fields in this header
               rngNext.Fields.Update

               ' Link to next story (if any)
               Set rngNext = rngNext.NextStoryRange
           Loop
       End If
   Next
End Sub

Public Sub updateFieldsInFooters()
   Dim Story As Variant
   Dim rngNext As Word.Range

   ' Iterate through all story types
   For Each Story In ActiveDocument.StoryRanges

       ' Only update fields in a footer
       If Story.StoryType = wdPrimaryFooterStory Or _
          Story.StoryType = wdFirstPageFooterStory Or _
          Story.StoryType = wdEvenPagesFooterStory Then

           ' Update fields in this footer
           Story.Fields.Update

           ' There may be linked footers so update them as well
           Set rngNext = Story.NextStoryRange
           Do Until rngNext Is Nothing

               ' Update fields in this footer
               rngNext.Fields.Update

               ' Link to next story (if any)
               Set rngNext = rngNext.NextStoryRange
           Loop
       End If
   Next
End Sub
AP - 10 Feb 2004 16:48 GMT
Thanks, this works for making sure we find the headers to update, but we are
not calling Update on the field, we are assigning the field.Result.Text
property directly, and for some reason this does not seem to work in the
header/footer.

Adam

> Here's some code that someone else was once kind enough to publish here on
> the forums:
[quoted text clipped - 56 lines]
>     Next
> End Sub
 
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.