none
Word Automation - Creating a document that contains multiple pages based on the same template

    Question

  • Hi All

     

    I am pretty new to Word Automation and my current objective is as follows. Appreciate any kind of reference or how-to's

     

     

    The problem as such is that I want to programatticaly create a word document that uses a template. The template has pre-defined placeholders(bookmarks) into which data will be placed during runtime.The details of a single record should be represented by the template, therefore effectively a record on one page using the same template.

     

    I have done something like this.

     

    1. Create an instance of word application and a word document instance using a template.

    2. Locate the bookmarks that is in the template - using the start and end location of the book marks that  I select and insert data at rutime.

    3. Create a page break

    4. Copy the contents and paste it to the new page.The contents copied do not have the bookmarks. So I have to repeat steps 2,3 and 4.

     

    Definetly Step 4 does not seem the right way. There must be a way I should be able to say, "use the template and create a new page". I guess this is more like MailMerge only that I don't send or print letters. I want to finally save the document.

     

    Could anyone provide a more standard way of approaching and addressing this? Have I taken the right apporach of creating the tempate with bookmarks and accessing them programmatically?

     

    Language: C#

     

    Thank you

     

    Tuesday, January 29, 2008 4:46 AM

Answers

  • Thank you for the additional details :-)

     

    In this case, I almost certainly would save the entire page as a Building Block entry, within the template. That way, it will travel with the template and always be available. Work in the Word 2007 application. Select the entire page, then click the "Insert" tab. In the group "Text" click the arrow of the "Quick Parts" button. Select the entry "Save selection to Quick Parts Gallery". In this dialog box, besides the Name, pay special attention to the "Save in" box (select your template) and "Options" fields. The other fields aren't as important, but you might want to choose "AutoText" rather than "Quick Parts" as the Gallery. And it would probably be helpful to enter a description.

     

    For the sake of completeness, here are a couple of alternate methods. But there would have to be special circumstances for me to choose any of them over using a Building Block:

    • Create a copy of the template file. Distribute it with your solution. Use the InsertFile method to bring it into the document you're building for each new page.
    • Don't use bookmarks. Use some kind of "placeholder" text to mark the beginning and end points of each "field". These would then remain in the document until you've built all the pages (so that they're still there when you copy). You'd use the Find property to locate them and, at the end, Find with Replace to remove them.
    •  

     

    Wednesday, January 30, 2008 10:18 AM

All replies

  • Technically, this forum supports the VSTO technology, not automation. You'll find a list of venues for automation questions (newsgroups) in the "Please Read First" message at the top of the forum. Please always mention the version of the Office application when asking for assistance! Usually, the application specialists (in your case, Word developers) can give the best advice on how to work with an application's object model.

     

    The first thought that comes to my mind when reading your problem description is: have you considered mail merge? If you have data that can basically be passed as "plain text" into the Word bookmarks - no font formatting or similar required - then mail merge would be the most efficient and simplest approach.

     

    If mail merge for some reason is not appropriate for your task, then what I'd probably do is to select that single page (content of your template) and save it in the template as an AutoText entry (or, in the case of 2007, a Building Block).

     

    After you've processed the first data item (first page), insert the AutoText after this range. It will contain the bookmarks. As long as your code has removed the original bookmarks, this should work with no problems.

     

    If the original bookmarks are still there (whether they will be or not depends on how you've set up the template and how your code works) and you no longer require them, delete them before inserting the Autotext.

     

    If the bookmarks are still there and you want to keep them, then you should rename each set of bookmarks as you go. In such cases, I usually use a counter and increment. So in the AutoText a bookmark name would be Bkm and in the document the bookmark name would change: Bkm1, Bkm2, etc.

     

    Tuesday, January 29, 2008 8:50 AM
  •  

    Hi Cindy,

     

    First of all please bear with me for having this posted under VSTO forum.

     

    To quickly respond to your query on  whether I considered Mail Merge. I did think of it as a good option but since formatting was required and I wanted to use a template rather than having to deal with programatically, I did not pursue it.

     

    MS Office: 2007 is what I am using.

     

    As of now, as I mentioned I create a word document using a template and I think I have done similar to what you mentioned above. I use that template page to populate data. I specify the range and then copy that to the next page.
    The copied content does not contain the bookmarks by default. So the next time I populate the template, its on page 1 again. So i continue the process of populate and then copy paste.(Of course I do get to end of page before doing the paste). I have kinda  achieved what I want but yeah want it to be the proper way of doing it.

     

    But again I might want to look closely at what you mentioned about "select that content page and save it to template as AutoText entry." - Not sure what this means but will work on it.

     

    Thank you for taking the time to respond.

     

     

     

     

    Wednesday, January 30, 2008 1:02 AM
  • Thank you for the additional details :-)

     

    In this case, I almost certainly would save the entire page as a Building Block entry, within the template. That way, it will travel with the template and always be available. Work in the Word 2007 application. Select the entire page, then click the "Insert" tab. In the group "Text" click the arrow of the "Quick Parts" button. Select the entry "Save selection to Quick Parts Gallery". In this dialog box, besides the Name, pay special attention to the "Save in" box (select your template) and "Options" fields. The other fields aren't as important, but you might want to choose "AutoText" rather than "Quick Parts" as the Gallery. And it would probably be helpful to enter a description.

     

    For the sake of completeness, here are a couple of alternate methods. But there would have to be special circumstances for me to choose any of them over using a Building Block:

    • Create a copy of the template file. Distribute it with your solution. Use the InsertFile method to bring it into the document you're building for each new page.
    • Don't use bookmarks. Use some kind of "placeholder" text to mark the beginning and end points of each "field". These would then remain in the document until you've built all the pages (so that they're still there when you copy). You'd use the Find property to locate them and, at the end, Find with Replace to remove them.
    •  

     

    Wednesday, January 30, 2008 10:18 AM