none
How to create .docx document and print it? RRS feed

  • Question

  • Hi ,

    I am working on a project in which there is a need of create a Document using c# in ".docx" format which do get the predefined format from a template document".dotx" and change the various fields in the document at run-time. After that it need to be printed.

     

    Thanks in Advance

    Anubhav

    Monday, January 17, 2011 2:48 AM

Answers

  • The various field means that i want to change the different characters from the base template like as the name, occupation,time, etc.These field need to be those which are filled by the user in winform. After the user entered the fields it should generate a document with the base of the given template but the name field, occupation field etc changed according to the Winform Input.When Done there should be print preview with printing command to print that generated Document.

    Given that you're targeting the *.docx file format, the most efficient approach for you to use would be to use the Open XML file format. Your app would use the standard Packaging and XML namespaces to open the document, insert the data, then close things up again. You'll find information on this at OpenXMLDeveloper.org and there's an Open XML SDK forum.

    Choosing the "data target" is your most challenging task. Since you're working with Word 2007 / 2010, you should consider inserting ContentControls and linking them to a Custom XML Part. This would mean your WinForms code need only access the Custom XML Part (*.xml file) in the docx package and write the data there. It will show up in the content controls as soon as the document is opened.

    Printing would still need to be done via the object model. That would go something like (pseudocode)

    using Word = Microsoft.Office.Interop.Word;

    //inside a method
    Word.Application wdApp = new Word.Application();
    Word.Document doc = wdApp.Documents.Open(//params here);
    //Or show print preview and let the user take care of printing
    doc.PrintOut(//params here);
    doc.close(//params here);
    wdApp.Quit(//params here);
    doc = null;
    wdApp=null;
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    GC.WaitForPendingFinalizers();


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bruce Song Tuesday, February 15, 2011 9:56 AM
    Monday, January 31, 2011 8:16 AM
    Moderator

All replies

  • What do you mean by "various fields"

    However, if you insert { DOCVARIABLE [variablename] } fields in the template, then you can use

    Dim doc as document
    Set doc = Documents.Add("Templatepath\name")
    With doc
       .Variables("variablename").Value = "somevalue
       'etc for each variable
       .Range.Fields.Update
    End With

    However, if you have such fields in the headers or footers of the document
    you will need to use

    Dim i as Long, j as Long
    Dim doc as document
    Set doc = Documents.Add("Templatepath\name")
    With doc
       .Variables("variablename").Value = "somevalue
       'etc for each variable
       .Range.Fields.Update
       For i = 1 to .Sections.Count
           With Sections(i)
               For j = 1 to .Headers.Count
                   .Headers(j).Range.Fields.Update
               Next j
               For j = 1 to .Footers.Count
                   .Footers(j).Range.Fields.Update
               Next j
           End With
       Next i
    End With

    End With


    Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "bluecomb" wrote in message news:7df11d83-94e7-4562-829e-a0de19aba8d4@communitybridge.codeplex.com...

    Hi ,

    I am working on a project in which there is a need of create a Document ".docx"format which do get the predefined format from a template document".dotx" and change the various fields in the document at run-time. After that it need to be printed.



    Thanks in Advance

    Anubhav


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    • Proposed as answer by Bruce Song Friday, January 21, 2011 10:22 AM
    • Marked as answer by Bruce Song Friday, January 28, 2011 9:55 AM
    • Unmarked as answer by Bruce Song Sunday, January 30, 2011 7:10 AM
    • Unproposed as answer by Cindy Meister MVPModerator Thursday, August 13, 2015 2:05 PM
    Monday, January 17, 2011 2:55 AM
  • Hay Doug,

    Thanks for the responce. The "various fields" means the part of the documents which are need to be replaced by the database like as the Name , Designation etc.

    I need to display the actual .dotx document in my project for final veiwing and editing by the user any method to display the final document within the .net project I am using C#.

    Thanks Again.


    @nubh@v
    Monday, January 17, 2011 8:00 AM
  • Sorry, I do not work with .net or C#.
     -- Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "bluecomb" wrote in message news:608634e2-f009-4ccf-b775-d434e29bbfab@communitybridge.codeplex.com...

    Hay Doug,

    Thanks for the responce. The "various fields" means the part of the documents which are need to be replaced by the database like as the Name , Designation etc.

    I need to display the actual .dotx document in my project for final veiwing and editing by the user any method to display the final document within the .net project I am using C#.

    Thanks Again.


    @nubh@v


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Monday, January 17, 2011 8:13 AM
  • Hi Bluecomb,

    Thank you for posting and we are glad to help with you.

    Have you got a solution to resolve your issue? I think Doug's reply may help you though it is VBA code, it should can be changed to be C# code.

    In addition, could you show more information about your requirement? Such like how about the template document .dotx, what are the fields in it? And how will you change the various fields at runtime? We will try to do further research base on these information.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Bruce Song Friday, January 28, 2011 9:55 AM
    • Unmarked as answer by Bruce Song Sunday, January 30, 2011 7:10 AM
    Friday, January 21, 2011 10:21 AM
  • Hay Bruce,

     Thanks for reply.The various field means that i want to change the different characters from the base template like as the name, occupation,time, etc.These field need to be those which are filled by the user in winform. After the user entered the fields it should generate a document with the base of the given template but the name field, occupation field etc changed according to the Winform Input.When Done there should be print preview with printing command to print that generated Document.

    Thanks Again


    @nubh@v
    Sunday, January 30, 2011 6:28 AM
  • The various field means that i want to change the different characters from the base template like as the name, occupation,time, etc.These field need to be those which are filled by the user in winform. After the user entered the fields it should generate a document with the base of the given template but the name field, occupation field etc changed according to the Winform Input.When Done there should be print preview with printing command to print that generated Document.

    Given that you're targeting the *.docx file format, the most efficient approach for you to use would be to use the Open XML file format. Your app would use the standard Packaging and XML namespaces to open the document, insert the data, then close things up again. You'll find information on this at OpenXMLDeveloper.org and there's an Open XML SDK forum.

    Choosing the "data target" is your most challenging task. Since you're working with Word 2007 / 2010, you should consider inserting ContentControls and linking them to a Custom XML Part. This would mean your WinForms code need only access the Custom XML Part (*.xml file) in the docx package and write the data there. It will show up in the content controls as soon as the document is opened.

    Printing would still need to be done via the object model. That would go something like (pseudocode)

    using Word = Microsoft.Office.Interop.Word;

    //inside a method
    Word.Application wdApp = new Word.Application();
    Word.Document doc = wdApp.Documents.Open(//params here);
    //Or show print preview and let the user take care of printing
    doc.PrintOut(//params here);
    doc.close(//params here);
    wdApp.Quit(//params here);
    doc = null;
    wdApp=null;
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    GC.WaitForPendingFinalizers();


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bruce Song Tuesday, February 15, 2011 9:56 AM
    Monday, January 31, 2011 8:16 AM
    Moderator
  • I know this thread is necro, but it seems the code posted above is now obsolete for VS 2017.

    Possible to get an update? Thanks!

    Thursday, March 7, 2019 10:05 PM