none
Create Word Documents By C#

    Question

  • Hello,

    I would like to create a Word doc directly from C# Windows Form codes.

    1) How to create and open the Word doc?

    After creating the Word doc and Open it, ...

    2) How to insert a text from a textbox to the Word doc that is already currently opened?

    3) How to check the current Range.Selection location?

    Please exlpain in full as I am a newbe in C# to create Word...

    Thanks in advance.

    Friday, March 10, 2006 1:11 PM

All replies

  • Hello

    I would like to know if you succeeded in writing your application, because I have the same problem, I am also a newbe, and I didn't figure it out how to do it. If so, can you paste in the code? I would be very grateful.

    Regards,

    Friday, March 10, 2006 9:53 PM
  • Hi, Monica..

    I would like to help you also.

    Here is the code...


    using System;
    using System.Threading;
    using System.Reflection;
    using Microsoft.Office.Interop.Word;
    public class MainClass
    {
     public static int Main (string[] argv)
     {
      Application app = new Application(); 
      app.Visible=true;
     
      // Set up to create a plain, empty text document.
      // Setting these variables to Missing.Value is comparable.
      // to not providing a value for an optional parameter in VB.
      // This gets the default behavior.
      object template=Missing.Value; //No template.
      object newTemplate=Missing.Value; //Not creating a template.
      object documentType=Missing.Value; //Plain old text document.
      object visible=true;  //Show the doc while we work.
      _Document doc = app.Documents.Add( ref template,
               ref newTemplate,
               ref documentType,
               ref visible);
     
      Thread.Sleep (5000);  //Display the empty document for 5 seconds.
      doc.Words.First.InsertBefore ("This document is no longer empty!");
      Thread.Sleep (5000);  //Wait for 5 more seconds
     
      //Save the file, use default values except for filename.
      object fileName = Environment.CurrentDirectory+"\\example2_new";
      object optional = Missing.Value;  //Take default values.
      
     #if OFFICEXP
      doc.SaveAs2000( ref fileName,
     #else
      doc.SaveAs ( ref fileName,
     #endif
       ref optional,
       ref optional,
       ref optional,
       ref optional,
       ref optional,
       ref optional,
       ref optional,
       ref optional,
       ref optional,
       ref optional);
     
      // Now use the Quit method to cleanup.
      object saveChanges = true;
      app.Quit(ref saveChanges, ref optional, ref optional);
      return 0;
     }
    }

     

    Anyonw can tell me how to insert text to a running & executed Word doc??

    Thanks in advance.
     

    Saturday, March 11, 2006 4:06 AM
  • Hi,

    Thank you very much, but I managed somehow and I have the answer to your question, because I had the same problem. (but I never give up :P)

    SO, here is the code that inserts data from a textbox from my C# project into a Word document:

    namespace turism

    {

    public partial class Form1 : Form

    {

    private Microsoft.Office.Interop.Word.ApplicationClass WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();//here I am declaring WordApp as a Word application

    formIntrodNume f = new formIntrodNume(); //this is the form that has a textbox on it and where Iam introducing a string, and you have to declare the new instance here to be seen globally

    public Form1()

    {

    InitializeComponent();

    }

    private void toolStripButton1_Click(object sender, EventArgs e) //this is the event that will open the form and I will have to introduce the string

    {

    if (f.ShowDialog(this) == DialogResult.OK)

    {

    this.Text = "Agentia de turism " + f.txtIntrodNume.Text;

    this.Invalidate();

    }

    }

    private void voucherToolStripMenuItem_Click(object sender, EventArgs e) //and this is the event that will produce me what I want to do

    {

    if (this.openFileDialog1.ShowDialog() == DialogResult.OK) // I am opening the file where I want to insert the string from my form

    {

    //here I keep in variable filename the filename of my document I've just opened

    object filename = openFileDialog1.FileName;

    object savefile = filename + ".doc";

    object readOnly = false;  //these are some variables that you will need

    object isVisible = true;

    object isfalse = false;

    object istrue = true;

    object isdynamic = 2;

    object missing = System.Reflection.Missing.Value;

    //here I make visible the Word application

    WordApp.Visible = true;

    WordApp.Activate(); // and I activate it to use it

    //I am opening the document I want to write in it, see the ref readOnly as a parametre here , it is declared false above

    Microsoft.Office.Interop.Word.Document my_doc = WordApp.Documents.Open(ref filename,ref missing, ref readOnly, ref missing, ref missing, ref missing,ref missing,ref missing,ref missing,ref missing, ref missing, ref isVisible,ref missing,ref missing,ref missing,ref missing);

    object start = 0;// start and end are 2 objects that will help you to define the space where you are going to fill you string

    object end = 0;

    Range range = my_doc.Range(ref missing, ref missing); // and here I am declaring my range

    start = 55; // now I am calculating the position of start and end(this means that you have to count every character and space in your document till the spot you want)

    end = 70;

    my_doc.Range(ref start, ref end).Text = f.txtIntrodNume.Text; // and this is how I am filling the string in my document

    }

    This is how the beginning of my document looks like:

    and after Agentia de turism (which means Tourism Agency I am filling that string)

    VOUCHER Nr.              

     

     

    Agenţia  de turism                                                                                    

     

    I hope this was usefull to you

     

    Regards,

     

    Saturday, March 11, 2006 11:58 AM
  • Based on what you need to do this can be simple or complex, however, you DEFINITELY you need to start with VSTO first and foremost.  There is no need at reinventing the wheel here.

    Take a look at this:

    http://msdn.microsoft.com/office/understanding/vsto/

    Download the Visual Studio plugin for Visual Studio Tools for Office and get going there.  There are tons of demos, web casts, samples, etc for you to review. 

    The nice thing about VSTO is you are writing managed code for Word, Excel or whatever.  And you are doing it in C# which is awesome. 

    I hope this gets you going in the right direction.

    -The Elder

    Monday, March 13, 2006 3:44 AM
  •  Keith Elder wrote:

    Based on what you need to do this can be simple or complex, however, you DEFINITELY you need to start with VSTO first and foremost.  There is no need at reinventing the wheel here.

    Take a look at this:

    http://msdn.microsoft.com/office/understanding/vsto/

    Download the Visual Studio plugin for Visual Studio Tools for Office and get going there.  There are tons of demos, web casts, samples, etc for you to review. 

    The nice thing about VSTO is you are writing managed code for Word, Excel or whatever.  And you are doing it in C# which is awesome. 

    I hope this gets you going in the right direction.

    -The Elder

    How I do that without VSTO? I don't have much options...

    Monday, March 13, 2006 6:36 AM
  • Why would you not be able to use VSTO?  It is a free download that plugs into Visual Studio.

    The bottom line is either you use VSTO or you have to use the interop libraries and go the long route of learning the word apis.  Refer to the post above with the links on where to get started going that route then.

     

    -The Elder

    Monday, March 13, 2006 5:48 PM
  •  Keith Elder wrote:

    Why would you not be able to use VSTO?  It is a free download that plugs into Visual Studio.

    The bottom line is either you use VSTO or you have to use the interop libraries and go the long route of learning the word apis.  Refer to the post above with the links on where to get started going that route then.

     

    -The Elder

     

    How to use the interop libraries and go the long route of learning the word app?

    How to insert a table using Interop DLL and add information to very cell?

     

    Thank in advance.

    Tuesday, March 14, 2006 1:01 PM
  • Sorry for small offtopic, but: Can I intergrate word in my application how it is did in Microsoft Office Outlook 2003 for letters writing ?  Thank you in advance.

    Wednesday, February 28, 2007 11:07 PM
  • There is a free solution to create exce, word,pdf  document without ole automation.

    http://www.codeproject.com/KB/cs/Excel_PDF_Word_ExportWiz.aspx

    Monday, May 23, 2011 2:34 AM
  • Hi friends,

    I have one question regarding this, I create the new word document file and copy the contents as per the needs. 

    1) I want to save the document with the name extension like Document.doc -(into)-> Documentfigures.doc

    2) The new document (Documentfigures.doc) need to be saved in a same folder.

    Any idea regarding this? 

    Wednesday, April 25, 2012 6:11 AM
  •  

    //This is to use your created word template.

    Object oTemplatePath = "(path to your template file)"; //path should be like "D:\\NewFolder1\\NewFolder2\\txtFile.docx" oDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

    //For saving the file with your extension name use some thing like this

     object oDestLoc = "D:\\NewFolder1\\NewFolder2\\" +Documentfigures+ ".docx";

     oDoc.SaveAs(ref oDestLoc, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

    --Refer this for more 

    http://support.microsoft.com/Default.aspx?scid=kb;en-us;316384&spid=2530&sid=47

    • Edited by Njoy Life Monday, October 22, 2012 11:12 AM
    Monday, October 22, 2012 11:06 AM