How to get data from SQL database in Word 2010?

I would like to populate a Word document with data from our MS SQL database.

Is this possible, and if so, how?

+7
source share
3 answers

In the past, I have done this in many ways. It depends on whether the user initiates an action from OUTSIDE Microsoft Word or from INSIDE Microsoft Word.

INSIDE Microsoft Word, you can use one of the following methods:

  • Open the placeholder template and use VBA or VSTO to iterate over them using the data source, using copy and paste. Please note that there is also a crime. This approach resembles approach 1 below with EXTERNAL. The disadvantage is that it is relatively slow (copy and paste) and autocorrection Microsoft Word likes to kick when it is least needed.
  • Open the placeholder template and use VBA or VSTO to parse the XML representation, and then replace them. It's faster but harder to write. Moreover, the XML representation may contain XML fragments in placeholders (for example, " &amp;lt;&amp;lt;PUT_<xxx/>IT_HERE&amp;gt;&amp;gt; " and more complex cases). In addition, you need to make sure that you have saved a valid XML document and are well balanced.

In EXTERNAL Microsoft Word (for example, a web interface) you can use one of the following methods:

  • Store the template somewhere using RTF (which is much easier to handle than the native Word structure). Put ' &lt;&lt;PLACEHOLDER-FOR-NAME&gt;&gt; 'or similar easily recognizable texts where you want to replace them. When a user requests a Word document, retrieves RTF, retrieves data, replaces placeholders, an RTF server for the user. RTF has some limitations, but some advantages. Advantages: ease of creating new templates, and also works with Microsoft Wordpad and other Office suites. The disadvantages are that tables are a real mess for processing and that not all Microsoft Word designs are possible. Repeating rows in a table is even less recommended. The problem is with a large volume.
  • Use the report package, which also outputs docx, doc, or RTF documents. Write a report. Overall ideal for high volume. It’s less suitable if you want the end user to type a lot of additional text, because report packages usually work on the basis of pages instead in the text, where an explicit or implicit page break is sometimes inserted. But if you only need the end user to enter one or two sentences, that's enough.
  • Fat client. Put the SQL data somewhere. Open Word. Read the data and see additional methods for INSIDE Microsoft Word.

If you need to populate a Word document from SQL Server (or any other database or data platform), I can offer a free version of Invantive Composition to populate Word Documents from a database (note that I was associated with this product). He opens the templates and combines them from Word, but is more focused on non-developers; just specify the template and data blocks (possibly nested) and publish. Developers can only add C # to plugins. I think this is a good product when you have a lot of templates (over 50), because it scales more easily.

+5
source

You can use a Microsoft query to retrieve data from an SQL database into your document, this video can be useful https://vimeo.com/83983247

+2
source

You can also try using MS-Excel, because it communicates with XML better than Word. It's also easy to get Excel to generate Word-style output.

-4
source

All Articles