Word using C#
how to create a Word document using C# ?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
saurabh mittal152 reputationPosted Apr 22, 2014, 11:45 PM EDT
Refer to this link.may be it will help u
http://www.c-sharpcorner.com/UploadFile/muralidharan.d/how-to-create-word-document-using-C-Sharp/
Farhan ShariffRank #2,004 · 725 reputationPosted Apr 22, 2014, 11:43 PM EDT
using System.IO;
using Word = Microsoft.Office.Interop.Word;
wordApp.Visible = false;
Word.Document doc = wordApp.Documents.Add();
string dirPath = @"C:\";
//You might want to add some more code here for inserting pages text adding header and footer
doc.Close();
wordApp.Quit();
MessageBox.Show("Word file created");
}
mackie duenas23 reputationPosted Apr 23, 2014, 12:50 AM EDT
mackie duenas23 reputationPosted Apr 23, 2014, 12:51 AM EDT
mackie duenas23 reputationPosted Apr 23, 2014, 6:25 PM EDT
using System;
Lorine Appleby38 reputationPosted Jan 7, 2015, 11:22 PM EST
Hi mackie, first regarding this line:
// Just to kill WINWORD.EXE if it is running
killprocess("winword");
Don't do that, I believe the reason why you are using it because you noticed that after you run your application a MS Word is still running. This occurs because of the inappropriate closing of that application, you see when working with word automation in C# you need to be careful about correctly releasing those COM resources, for example something link the following should work:
aDoc.Close();
wordApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
But if you want to avoid that hassle with COM interface I would recommend you to try out this C# component for Word documents. You see you can easily read, modify and then write your DOCX files in C# with it.
Also you even have a direct solution for your requirement, instead of using a find and replace approach you should take a look at a mail merging process in C#.
And last thing you even have a direct API for printing your Word documents in C#.