none
Look for the best approach on populating Word documents from a database RRS feed

  • Question

  • Hello,

    I’m helping an attorney friend convert/develop Word “templates.” The reason I put the quotes is because they are basically Word document boilerplates and then they just enter their client’s information. It works, but it is slow, tedious and prone to errors.  These are not mail merge documents.

    My goal right now is for them to enter all the client information in Access. Once that is done, they open Word, and using the add-in, choose the client and template they want to populate.

    Right now there appear to be three ways that data will can be populated:

    1.        Simple mail merge
    2.        Mail merge but change some text in paragraphs based on the client. Are they the wife or husband? “Child” or “children”
    3.        Add repeating (or don’t add at) sections of text. For example, listing their job(s), vehicle(s), home(s).

    My question is, what would the best approach be to populate the Word templates? If I am not using mail merge, what is a good way to change specific words based on the client's data and/or insert or remove sentences/paragraphs etc?

    Some additional background: her staff is not very computer illiterate (but OK) and I need to make it as easy as possible for them. I thought of doing these as Access reports, but then it would be difficult for them to change the text of the template.

    My experience is with SQL Server, C#, VSTO add-ins for Excel as well as VBA. I’ve done very little work with Word.

    Also, they do have a client database (a giant messy text file) that I am converting to Access.  So I have freedom to create the database anyway that works best.

    Thank you for the help.

    ~J

    Tuesday, January 8, 2013 8:11 PM

Answers

  • Hi Jenna

    One thing you forgot to mention is the version of Office used in this environment :-)?

    Assuming 2007 or later, and given your background, I'd look at using Content Controls as the "data targets". This gives the client maximum editing freedom combined with the securist (means can't be deleted by accident) "data target". What's more, the concepts used for managing the data can also be applied to Excel and PowerPoint (although these lack the equivalent of the Content Control that will display the data on the document surface).

    While you could work with the content controls "as is", mapping them to a Custom XML Part stored in the document has advantages:

    1. The data can be written/read without needing to open the document in Word
    2. You/the user can create "repeating" data targets (content controls mapped to the same node in the Custom XML Part will all show the same content / edited content will be reflected in all controls)
    3. The same code can be used to write data to the document (Custom XML Part) in Word, Excel and PowerPoint

    You'll find a series of articles on Greg Maxey's site that will give you the basics of working with content controls and Custom XML Parts, here are the most relevant for your requirement:
       http://gregmaxey.mvps.org/word_tip_pages/content_controls.html
       http://gregmaxey.mvps.org/word_tip_pages/mapped_content_controls.html
       http://gregmaxey.mvps.org/word_tip_pages/repeating_data.html
       http://gregmaxey.mvps.org/word_tip_pages/content_control_custom_events.html


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 9, 2013 9:08 AM
    Moderator
  • Hi Jenna

    I don't know of any Word->C# tutorials, beyond what you've found. Word's object model is huge, so beyond the absolute basics it would be difficult to do anything comprehensive in the form of a "tutorial". You'll find code samples here and in the VSTO forum for some things (such as Find). If you were in the German-language area I'd also recommend the Word programming book from Microsoft Press as that explains the Word object model with code samples in C# that demonstrate how to "convert" VBA to C#...

    Sorry about misunderstanding the repeating sections (1:many) requirement. No, Word doesn't really support that very well. About all you can do is concatenate the data into a string, then assign the string to a Range object. If you want it in table form, then convert the Range to a Table (there's a command in the object model for that). This will return a Table object and using that you can apply any formatting required. For tables you might find this article useful:
      http://msdn.microsoft.com/en-us/library/aa537149(office.11).aspx


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Jenna_Fire Wednesday, January 9, 2013 6:23 PM
    Wednesday, January 9, 2013 4:31 PM
    Moderator

All replies

  • Hi Jenna

    One thing you forgot to mention is the version of Office used in this environment :-)?

    Assuming 2007 or later, and given your background, I'd look at using Content Controls as the "data targets". This gives the client maximum editing freedom combined with the securist (means can't be deleted by accident) "data target". What's more, the concepts used for managing the data can also be applied to Excel and PowerPoint (although these lack the equivalent of the Content Control that will display the data on the document surface).

    While you could work with the content controls "as is", mapping them to a Custom XML Part stored in the document has advantages:

    1. The data can be written/read without needing to open the document in Word
    2. You/the user can create "repeating" data targets (content controls mapped to the same node in the Custom XML Part will all show the same content / edited content will be reflected in all controls)
    3. The same code can be used to write data to the document (Custom XML Part) in Word, Excel and PowerPoint

    You'll find a series of articles on Greg Maxey's site that will give you the basics of working with content controls and Custom XML Parts, here are the most relevant for your requirement:
       http://gregmaxey.mvps.org/word_tip_pages/content_controls.html
       http://gregmaxey.mvps.org/word_tip_pages/mapped_content_controls.html
       http://gregmaxey.mvps.org/word_tip_pages/repeating_data.html
       http://gregmaxey.mvps.org/word_tip_pages/content_control_custom_events.html


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 9, 2013 9:08 AM
    Moderator
  • Cindy,

    As always, you have the best answers! I cannot tell you how much I appreciate the links and information. It is such as great help.

    This should certainly get me started.

    At the risk of being greedy, do you know of tutorials (not necessarily from Greg Maxey or about Content Controls) about C# and Word? There seemed to be a lack of decent tutorials on Microsoft's site (that I could find).

    I am using Word 2010 but the repeating section control appears to be only in 2013. To be clear, when I am referring to a repeating section I mean there would be a list of rows, not that the data appears in multiple places in the document. For example, after populating the client's name and address (only appears once) it then populates a repeating section for their five children. 

    Thank you again for all the help.

    ~J

    EDIT: Found this: http://msdn.microsoft.com/en-us/library/bb398244(VS.90) for some tutorials.
    • Edited by Jenna_Fire Wednesday, January 9, 2013 4:07 PM Found useful link
    Wednesday, January 9, 2013 4:03 PM
  • Hi Jenna

    I don't know of any Word->C# tutorials, beyond what you've found. Word's object model is huge, so beyond the absolute basics it would be difficult to do anything comprehensive in the form of a "tutorial". You'll find code samples here and in the VSTO forum for some things (such as Find). If you were in the German-language area I'd also recommend the Word programming book from Microsoft Press as that explains the Word object model with code samples in C# that demonstrate how to "convert" VBA to C#...

    Sorry about misunderstanding the repeating sections (1:many) requirement. No, Word doesn't really support that very well. About all you can do is concatenate the data into a string, then assign the string to a Range object. If you want it in table form, then convert the Range to a Table (there's a command in the object model for that). This will return a Table object and using that you can apply any formatting required. For tables you might find this article useful:
      http://msdn.microsoft.com/en-us/library/aa537149(office.11).aspx


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Jenna_Fire Wednesday, January 9, 2013 6:23 PM
    Wednesday, January 9, 2013 4:31 PM
    Moderator
  • As far as the repeating sections, using a table that way is a great work-around. I appreciate the tip.

    Thank you again for the super-helpful information!

    Wednesday, January 9, 2013 6:25 PM