Re: Replacing merge fields in headers/footers

From: Charles Kenyon (msnewsgroup_at_remove.no.spam.addbalance.com)
Date: 02/17/04


Date: Tue, 17 Feb 2004 17:21:47 -0600

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.

-- 
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.
"AP" <adamp@indra.com> wrote in message
news:%23iNWsr%237DHA.2044@TK2MSFTNGP10.phx.gbl...
> 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());
>
> }
>
> }
>
>
>
>
> "Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL ADDRESS"
> <dkr@mOSTvALUABLEpROFESSIONALs.org> wrote in message
> news:Ol8m0a57DHA.488@TK2MSFTNGP12.phx.gbl...
> > 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.
> >
> > -- 
> > 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
> > "AP" <adamp@indra.com> wrote in message
> > news:OWfMFO37DHA.3008@TK2MSFTNGP09.phx.gbl...
> > >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
> > >
> > > "AP" <adamp@indra.com> wrote in message
> > > news:Osng%23u27DHA.3704@tk2msftngp13.phx.gbl...
> > >> 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
> > >>
> > >>
> > >
> > >
> >
> >
>
>


Relevant Pages

  • Re: Replacing merge fields in headers/footers
    ... Please post replies and questions to the newsgroup so that others can learn from my ignorance and your wisdom. ... The user selects> to retrieve what we call a template document from the server for a certain> "file number" in our database. ... > //this code does not work - not sure why the document fields are different> from the header/footer fields in the first place... ... > private void UpdateField(Word.Field theField, int theFileID, int theUserID,> int theOfficeID, int theLedgerEntryID) {> ...
    (microsoft.public.word.vba.general)
  • Re: Importing from text file.
    ... actually a printer file of a report from another system, and, much like ... > that we would like to import into our Access database. ... Open file and remove all header/footer, ...
    (microsoft.public.access.externaldata)
  • Re: Replacing merge fields in headers/footers
    ... a message that you post back to the newsgroup as a continuation of this ... Doug Robbins - Word MVP ... I iterate through the merge fields in the header/footer, ...
    (microsoft.public.word.vba.general)
  • Re: Replacing merge fields in headers/footers
    ... a message that you post back to the newsgroup as a continuation of this ... Doug Robbins - Word MVP ... I iterate through the merge fields in the header/footer, ...
    (microsoft.public.dotnet.framework.interop)
  • Re: headers and footers dont appear in merged document
    ... You may want to also post this in the Word Mailmerge newsgroup ... which contains the header/footer information and is also a Next Page type of ... MS Office System Products MVP ... MS Office Community discussion/newsgroups via Web Browser ...
    (microsoft.public.office.misc)

Loading