Re: Replacing merge fields in headers/footers
From: AP (adamp_at_indra.com)
Date: 02/19/04
- Next message: Charles Kenyon: "Re: Replacing merge fields in headers/footers"
- Previous message: Charles Kenyon: "Re: Count instance of a specific word"
- In reply to: Charles Kenyon: "Re: Replacing merge fields in headers/footers"
- Next in thread: Charles Kenyon: "Re: Replacing merge fields in headers/footers"
- Reply: Charles Kenyon: "Re: Replacing merge fields in headers/footers"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 19 Feb 2004 16:04:37 -0700
Because we're not doing a mail merge. We have ~500 unique template documents
that need to be populated from data in a database with 100 tables. We have
written stored procedures that correspond to items of data that we wish to
place in our documents. The documents are created with the mergefields named
with our command identifier + the name of the stored procedure we wish to
execute. This needs to happen on the fly to reflect any changes in the
database, and a newly populated document can be requested at any time.
Adam
"Charles Kenyon" <msnewsgroup@remove.no.spam.addbalance.com> wrote in
message news:OCuiyrw9DHA.1816@TK2MSFTNGP12.phx.gbl...
> If you are using merge fields why are you changing their results rather
than
> getting it from your data file?
>
> The ideal mail merge, to my way of thinking is to have a merge template
and
> a data source. You can use a query or other method to pare your data
source
> to the records you want. You create a new document based on the template
and
> merge it with your data source. That merge can be to the printer or to a
new
> document containing the complete merge. I am assuming that this is what we
> are talking about altering with your code. I'm not sure why you are
altering
> this rather than redoing the merge to reflect the updated data.
> --
>
> 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:uVdIc4k9DHA.1636@TK2MSFTNGP12.phx.gbl...
> > I step through my code. It finds all the fields in the headers/footers
> > section. I'm using exactly the same code to populate the fields in the
> main
> > section as I am to populate the header/footer fields, but for the
> > header/footer fields it does not work. What I meant by set result.text
was
> > this line in the UpdateFields method:
> >
> > theField.Result.Text = GetReplacementText(theFileID, theUserID,
> theOfficeID,
> > theLedgerEntryID, command.Substring(index + 1 +
> > COMMAND_RESWARE.Length).Trim());
> >
> > As I have said, this works for fields in the document, but not the
> > header/footer fields.
> >
> > Below is all of my code.
> >
> > Thankyou,
> >
> > Adam
> >
> > /// <summary>
> >
> > /// Populates a template document.
> >
> > /// </summary>
> >
> > /// <param name="theFileID">FileID of the file that we wish to pull info
> > from.</param>
> >
> > /// <param name="theUserID">UserID of the person creating the
> > document</param>
> >
> > /// <param name="theOfficeID">Current OfficeID of the user</param>
> >
> > /// <param name="theTemplateFullFilepath">Path to the template
> file.</param>
> >
> > /// <returns>A FileStream to the populated document.</returns>
> >
> > private FileStream PopulateTemplateDocument(int theFileID, int
theUserID,
> > int theOfficeID,
> >
> > int theLedgerEntryID, string theTemplateFullFilepath)
> >
> > {
> >
> > Word.ApplicationClass wa = null;
> >
> > Word.Document wd = null;
> >
> > object readOnly = false;
> >
> > object trueObject = true;
> >
> > object missing = System.Reflection.Missing.Value;
> >
> > object saveChangesYes = Word.WdSaveOptions.wdSaveChanges;
> >
> > object saveChangesNo = Word.WdSaveOptions.wdDoNotSaveChanges;
> >
> > try {
> >
> > if (!Directory.Exists(TEMP_DIR)) {
> >
> > Directory.CreateDirectory(TEMP_DIR);
> >
> > }
> >
> > object templateFile = Path.GetTempFileName();
> >
> > //copy the file from its original location to a temporary location
> >
> > File.Copy(theTemplateFullFilepath,(string) templateFile,true);
> >
> >
> > //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();
> >
> > Range nextRange = null;
> >
> > //update headers and footers
> >
> > foreach (Range storyRange in wd.StoryRanges) {
> >
> > if (storyRange.StoryType == WdStoryType.wdPrimaryHeaderStory ||
> >
> > storyRange.StoryType == WdStoryType.wdFirstPageHeaderStory ||
> >
> > storyRange.StoryType == WdStoryType.wdEvenPagesHeaderStory ||
> >
> > storyRange.StoryType == WdStoryType.wdPrimaryFooterStory ||
> >
> > storyRange.StoryType == WdStoryType.wdFirstPageFooterStory ||
> >
> > storyRange.StoryType == WdStoryType.wdEvenPagesFooterStory ) {
> >
> > foreach (Field hff in storyRange.Fields) {
> >
> > UpdateField(hff,theFileID,theUserID,theOfficeID,theLedgerEntryID);
> >
> > }
> >
> > nextRange = storyRange.NextStoryRange;
> >
> > while (nextRange != null) {
> >
> > foreach (Field hff in nextRange.Fields) {
> >
> > UpdateField(hff,theFileID,theUserID,theOfficeID,theLedgerEntryID);
> >
> > }
> >
> > nextRange = nextRange.NextStoryRange;
> >
> > }
> >
> > }
> >
> > }
> >
> >
> >
> > //update regular document fields
> >
> > foreach (Field f in wd.Fields) {
> >
> > UpdateField(f,theFileID,theUserID,theOfficeID,theLedgerEntryID);
> >
> > }
> >
> > //create another temp file
> >
> > object newTemplate = Path.GetTempFileName();
> >
> > if (File.Exists((string) newTemplate)) {
> >
> > File.Delete((string) newTemplate);
> >
> > }
> >
> > //Save another temp file and close word
> >
> > wd.SaveAs(ref newTemplate,ref missing,ref missing,ref missing,ref
> > missing,ref missing,ref missing,
> >
> > ref missing,ref missing,ref missing,ref missing);
> >
> > wd.Close(ref saveChangesYes, ref missing, ref missing);
> >
> > wa.Quit(ref saveChangesYes, ref missing, ref missing);
> >
> > wd = null;
> >
> > wa = null;
> >
> > //delete the temporary file
> >
> > File.Delete((string) templateFile);
> >
> > //Return a new filestream to this doc
> >
> >
> > return new FileStream((string)
newTemplate,FileMode.Open,FileAccess.Read);
> >
> > }
> >
> > catch {
> >
> > throw;
> >
> > }
> >
> > finally {
> >
> > if (wd != null) {
> >
> > wd.Close(ref saveChangesNo, ref missing, ref missing);
> >
> > wd = null;
> >
> > }
> >
> > if (wa != null) {
> >
> > wa.Quit(ref saveChangesNo, ref missing, ref missing);
> >
> > wa = null;
> >
> > }
> >
> > }
> >
> > }
> >
> > private void UpdateField(Word.Field theField, int theFileID, int
> theUserID,
> > int theOfficeID, int theLedgerEntryID) {
> >
> > string command = theField.Code.Text.Trim();
> >
> > int index = command.IndexOf(COMMAND_RESWARE);
> >
> > if (index != -1) {
> >
> > theField.Result.Text = GetReplacementText(theFileID, theUserID,
> theOfficeID,
> >
> > theLedgerEntryID, command.Substring(index + 1 +
> > COMMAND_RESWARE.Length).Trim());
> >
> > }
> >
> > }
> >
> >
> >
> >
> >
> > "Charles Kenyon" <msnewsgroup@remove.no.spam.addbalance.com> wrote in
> > message news:uRWyunk9DHA.888@tk2msftngp13.phx.gbl...
> > > 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.
> > >
> > > --
> > >
> > > 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:ORJkMhe9DHA.3292@TK2MSFTNGP11.phx.gbl...
> > > > 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.
> > > >
> > > >
> > > > "Charles Kenyon" <msnewsgroup@remove.no.spam.addbalance.com> wrote
in
> > > > message news:uh$oSza9DHA.2856@TK2MSFTNGP10.phx.gbl...
> > > > > 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
> > > > > > > >>
> > > > > > > >>
> > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>
- Next message: Charles Kenyon: "Re: Replacing merge fields in headers/footers"
- Previous message: Charles Kenyon: "Re: Count instance of a specific word"
- In reply to: Charles Kenyon: "Re: Replacing merge fields in headers/footers"
- Next in thread: Charles Kenyon: "Re: Replacing merge fields in headers/footers"
- Reply: Charles Kenyon: "Re: Replacing merge fields in headers/footers"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|