none
Docx report generation

    Question

  • Hello,

    I'm approaching Ms Office automation for the first time and I'm trying to figure out a good way in which to programmatically generate a Docx report from a Dotx template and some db data by using Ms Visual Studio 2008 (C#) and Ms Office 2007.

    I know that Docx separates presentation from data and that it uses XML, but I haven't found a good tutorial which explains exactly what I need.

    Can you recommend any good tutorials or tell me where to get started?

    Thank you in advance.

    Erion

    Wednesday, July 14, 2010 8:41 AM

Answers

  • Hi Erion

    One question you need to ask and answer for yourself is whether you want to generate these reports using Automation, or using Open XML. Generally working with the Office Open XML file format is going to be faster, and the Office application need not be installed on the machine.

    You'd only really want/need automation if interaction with the user after the report is generated is a requirement, or if you want to print the result. But in either case, the report could (and probably should) be created without automating the application.

    A good starting point for finding out more about the OpenXML file format would be OpenXMLDeveloper.org. There's also a OpenXML SDK available for Visual Studio, and a corresponding forum on this server.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by ErionPC Thursday, July 15, 2010 10:09 AM
    • Unmarked as answer by ErionPC Thursday, July 15, 2010 10:09 AM
    • Marked as answer by ErionPC Thursday, July 15, 2010 10:10 AM
    Thursday, July 15, 2010 9:47 AM

All replies

  • Hello Erion,

    I think you are looking for Documetns.Add method. By setting the first parameter to a template, this method will create a new document based on this template. For more information, please refer to this MSDN page: http://msdn.microsoft.com/en-us/library/bb237079(office.12).aspx.

    If you have any concern for this post, please feel free to follow up.

    Best regards,
    Bessie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, July 15, 2010 9:43 AM
  • Hi Erion

    One question you need to ask and answer for yourself is whether you want to generate these reports using Automation, or using Open XML. Generally working with the Office Open XML file format is going to be faster, and the Office application need not be installed on the machine.

    You'd only really want/need automation if interaction with the user after the report is generated is a requirement, or if you want to print the result. But in either case, the report could (and probably should) be created without automating the application.

    A good starting point for finding out more about the OpenXML file format would be OpenXMLDeveloper.org. There's also a OpenXML SDK available for Visual Studio, and a corresponding forum on this server.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by ErionPC Thursday, July 15, 2010 10:09 AM
    • Unmarked as answer by ErionPC Thursday, July 15, 2010 10:09 AM
    • Marked as answer by ErionPC Thursday, July 15, 2010 10:10 AM
    Thursday, July 15, 2010 9:47 AM
  • Thank you for your reply,

    I'm sorry I wasn't very specific regarding my needs. The reason why I mentioned office automation was because one of the requirements for my software project is that the user who generates the Docx report must be able to view it and eventually modify it. After this the user must be able to save the document as a pdf and send it by email.

    As a result of my research on the topic I came to the conclusion that using Open XML SDK 2.0 is the best solution to generate the document programmatically. I have already set up a working client/server architecture based on ASP.NET and WCF respectively and I have managed to generate a simple docx document based on dynamic data and custom xml binding - I extended and modified an existing sample project I found on the web.

    My question now is where to get a good tutorial about custom xml data binding - i.e. I need to be able to define Custom XML bindings on my document template "manually" before I process the template with my software and substitute the custom XML with my data.

    Best regards.

    Erion

    Thursday, July 15, 2010 10:09 AM
  • Hi Erion

    Could you please be more specific about why you mean by "custom XML bindings"? I suspect I know what you're talking about, but I need to be sure...

    Are you talking about the technique where you link a schema to a document, then insert XML Nodes defined in the schema onto the document surface?

    Or are you thinking in terms of Content Controls, linked to a Custom XML Part?

    Currently, the latter approach is the one recommended by Microsoft, due to a court decision that's capping a lot of the functionality of the first approach.

    And in what way is setting up the template a difficulty? Do you have to provide this facility for end-users, so that they can create templates whenever they wish? Or is this for you, personally, to set up templates?


    Cindy Meister, VSTO/Word MVP
    Thursday, July 15, 2010 1:21 PM
  • Hi Cindy,

    I did read about Microsoft's patent infringement issues with i4i and I'm thinking of binding content controls to custom XML parts in a docx document, rather than linking an XML schema. I thought I could do this by just using Ms Word, but for what I've understood, the safest way to do it is by using Visual Studio.

    I found this tutorial http://msdn.microsoft.com/en-us/library/bb398244.aspx , which explains just what I need.

    This is the context for my application:

    One of my companie's departments issues a dotx template with no content controls. I must then "manually" add content controls to the template to make it dynamic. My software must generate docx reports based on that template and some db data.

    The end user of the program logs into an ASP.NET web application, from which he can issue the generation of the docx reports. The reports are generated by an IIS-hosted WCF service and archived in the web application host. Another part of the system concerns sending documents by email and some other stuff which isn't inherent for now.

    I think my problems are solved for now.

    Thanks for your help.

    Thursday, July 15, 2010 2:52 PM
  • Hi Erion

    <<I thought I could do this by just using Ms Word, but for what I've understood, the safest way to do it is by using Visual Studio.>>

    Mmm, I'm not sure to what you're referring, here...

    It's perfectly fine to insert the content controls in Word (outside the Visual Studio Tools for Office Designer). And the CustomXMLPart with the links can be done through the object model or through manipulating the OpenXML file. VSTO is not required, nor is it any "safer". It appear easier for you (at least at first glance), which is fine.

    What you need to be aware of, however, is (1) that VSTO is primarily there to provide an extended UI and (2) that a VSTO document will always want to link up to its code (which is not stored in the document, but is a separate .NET dll). This could become "annoying" for the users (and any email recipients). If you decide to use VSTO for creating the "template" you certainly can do so. But you may also want to read up on how to remove the VSTO customization before passing the document on to the user or the archive or to email: http://msdn.microsoft.com/en-us/library/bb772073.aspx


    Cindy Meister, VSTO/Word MVP
    Thursday, July 15, 2010 3:02 PM
  • <<I thought I could do this by just using Ms Word, but for what I've understood, the safest way to do it is by using Visual Studio.>>

    Mmm, I'm not sure to what you're referring, here...

    Hi Cindy,

    what I mean by "safest way" is that by using Word 2007 I couldn't find how to create custom XML parts or link the content controls to them. I thought that once you generate a docx (or dotx) document from within the VSTO, you can just save it as a docx file in the file system and it preserves the custom xml parts and the links to the content controls. From what you've written it doesn't seem to work in that way.

    Certainly I don't want any links to code within my Word documents, I just want to create the custom XML parts and link them to content controls and I thought I could do this easily through the VSTO.

    In this case, what's the best approach for creating custom xml parts and linking them to content controls in a word document? I hope it's not the case of "manually" manipulating the docx archive.

    Thursday, July 15, 2010 3:56 PM
  • Hi Erion

    Check out this tool. I think it's what you're looking for:

    http://blogs.msdn.com/b/acoat/archive/2007/03/01/linking-word-2007-content-controls-to-custom-xml.aspx


    Cindy Meister, VSTO/Word MVP
    Thursday, July 15, 2010 4:11 PM
  • Hi Cindy,

    thanks for that, it's exactly what I was looking for. I find it strange that a similar type of tool hasn't been packaged with MsOffice 2007.

    I have another question now. Is it possible to have a Repeater type of content control in Word 2007? I'm thinking of a table which contains a dynamic number of rows of data. Using custom XML I can only define a static table. Is there a way to do this? - I'm too used to ASP.NET unfortunately ... :)

    Friday, July 16, 2010 7:28 AM
  • Hi Erion

    1. The tool: I suspect what's behind there not being a tool is similar to the thinking when developing the Office 2007 Ribbon. Content controls and CustomXMLParts supposedly target developers, and developers should be able to do the "mechanics" without Microsoft needing to supply any tools. Which is, of course, correct. But they sort of forget the learning curve, time and effort involved for (each) developer needing to do this. Plus they factor out "power users" who are going to want to do this, but will have more problems. In Office 2010 they did add better Ribbon customization :-) Perhaps sometime down the road they'll incorporate something for the content control mapping...

    2. 1:many tables. No, there's nothing in Word that supports this. Period. It's often requested, but so far the closest Word has come was XMLNodes. Prior to the court decision that put a damper on that technology, the user could TAB at the end of a table nested in a node, with a node in each cell. That would generate a new row with the nested nodes for each cell automatically duplicated. But that didn't propagate forward to the content control technology. (Nor is it part of any extensions VSTO makes to Word.)

    Back in the "olden days", when we used form fields (Word 2003 back to Word 6.0 in the early nineties, this would be), what I'd do would be to create an AutoText entry (Building Block, these days) that held the entire table row, with the form fields. My app would insert this at the end of the table when a new rows was required, and make any adjustments to the individual form fields (such as renaming them to avoid bookmark naming conflicts). You'd have to do something similar (make sure the controls are linked to the correct nodes in the CustomXMLPart, mainly).


    Cindy Meister, VSTO/Word MVP
    Friday, July 16, 2010 8:59 AM
  • That's great.

    Thank you very much.

     

    Erion

    Friday, July 16, 2010 9:13 AM
  • Hi Cindy,

    I need to convert my docx reports into pdf. Is there a freeware solution that permits me to do that without having to interface to the office COM component?

     

    Tuesday, July 20, 2010 8:10 AM
  • Please ask this in a new message thread, as it's a completely new topic. Easier for both of us, as well as others who may have ideas, to track the conversations :-)
    Cindy Meister, VSTO/Word MVP
    Tuesday, July 20, 2010 8:34 AM