none
Code snippets for populating newsletter document RRS feed

  • Question

  • Hello - I'm trying to design an application which uses an HTML form front-end to populate a database, and then takes the text contents of individual database records and enters these into a Word document as either text boxes or table cells. This is used to create a newsletter, both in HTML format and as hard copy.

    As I imagine this type of application is fairly common, I'm wondering if anyone has any code snippets to implement the process of grabbing the data from the database and placing it into the Word document.

    In particular, I'm looking for code which breaks the text at the end of the page, and flows the remaining text onto the next page.

    Many thanks in advance if anyone has any ideas!


    DragonForest

    Thursday, February 20, 2014 12:35 AM

Answers

  • In particular, I'm looking for code which breaks the text at the end of the page, and flows the remaining text onto the next page.

    Word will handle that on its own, without the need for a manual page break.

    Furthermore, inserting a manual page break in the middle of a paragraph can compromise the paragraph formatting. For example, if the paragraph Style has a first line indent, a bullet and/or justification, the manual page break is liable to leave the first part with a last line that doesn't fill the column and the second part with a new first line indent or a bullet on the first line.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, February 20, 2014 1:55 AM
  • If your application is .NET app then you can use Docentric Toolkit. It is a template based document generation toolkit and will allow you to create a template document (docx) and then merge it with your (database) data from .NET without relying on MS Word application (Word does not need to be installed on the production machine).

    If you are looking for a free solution, then you will have to jump into Open XML SDK.

    Monday, February 24, 2014 12:23 PM
  • Hi Dragon Forest

    Paul has addressed the question about page breaks...

    Word has a built-in facility for merging data from a database into a document: Mail Merge. It would probably make sense to look at that, first, before writing code to "reinvent the wheel". Note that this requires the presence of the Word Application, so if your requirement should run server-side (as appears might be the case) it's not ideal.

    In that case, you should consider leveraging the Open XML file format, as mentioned elsewhere in this thread. This is the appropriate approach for server-side document creation/editing. The usual way to set it up is to insert Content Controls as the data target points in the document. These can be mapped to a Custom XML Part in the document, meaning the data you're writing would be written to the Custom XML Part and appear automatically in the document. Or the data can be written directly to the Content Controls. The Open XML SDK is useful for such tasks. A good starting point for working with the technology is OpenXMLDeveloper.org


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, February 24, 2014 1:30 PM
    Moderator

All replies

  • In particular, I'm looking for code which breaks the text at the end of the page, and flows the remaining text onto the next page.

    Word will handle that on its own, without the need for a manual page break.

    Furthermore, inserting a manual page break in the middle of a paragraph can compromise the paragraph formatting. For example, if the paragraph Style has a first line indent, a bullet and/or justification, the manual page break is liable to leave the first part with a last line that doesn't fill the column and the second part with a new first line indent or a bullet on the first line.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, February 20, 2014 1:55 AM
  • If your application is .NET app then you can use Docentric Toolkit. It is a template based document generation toolkit and will allow you to create a template document (docx) and then merge it with your (database) data from .NET without relying on MS Word application (Word does not need to be installed on the production machine).

    If you are looking for a free solution, then you will have to jump into Open XML SDK.

    Monday, February 24, 2014 12:23 PM
  • Hi Dragon Forest

    Paul has addressed the question about page breaks...

    Word has a built-in facility for merging data from a database into a document: Mail Merge. It would probably make sense to look at that, first, before writing code to "reinvent the wheel". Note that this requires the presence of the Word Application, so if your requirement should run server-side (as appears might be the case) it's not ideal.

    In that case, you should consider leveraging the Open XML file format, as mentioned elsewhere in this thread. This is the appropriate approach for server-side document creation/editing. The usual way to set it up is to insert Content Controls as the data target points in the document. These can be mapped to a Custom XML Part in the document, meaning the data you're writing would be written to the Custom XML Part and appear automatically in the document. Or the data can be written directly to the Content Controls. The Open XML SDK is useful for such tasks. A good starting point for working with the technology is OpenXMLDeveloper.org


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, February 24, 2014 1:30 PM
    Moderator