Create a new windows application project and add a button to it.
On click of that button, we will create a new document(word) and write a simple Hello World text in it.
To create a word document using C# we need to first reference the following DLL(com)
After adding reference, add this directive
using Microsoft.Office.Interop.Word;
Put this code on button click
private void button1_Click(object sender, EventArgs e)
{
object missing = System.Reflection.Missing.Value;
object Visible=true;
object start1 = 0;
object end1 = 0;
ApplicationClass WordApp = new ApplicationClass();
Document adoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
Range rng = adoc.Range(ref start1, ref missing);
try
{
rng.Font.Name = “Georgia”;
rng.InsertAfter(“Hello World!”);
object filename = @”D:\MyWord.doc”;
adoc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
WordApp.Visible = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
The easiest way to write code for office interoperability is to make use of VBA code.
Say you want to insert a picture in a word document what you can do is
open the word document – Go to Tools ->Macro-> Record New Macro
Now click on insert menu and insert the picture. Stop the recording, again go to Macro -Macros-> Select your Macro and click on edit
You will find the vba code over there
Sub Macro1()
Selection.InlineShapes.AddPicture FileName:= _
“C:\Documents and Settings\nishantr1\My Documents\My Pictures\untitled.bmp” _
, LinkToFile:=False, SaveWithDocument:=True
End Sub
Now to write the same code in c# you will do something like this
Range rngPic = adoc.Tables[1].Range;
rngPic.InlineShapes.AddPicture(@”C:\anne_hathaway.jpg”, ref missing, ref missing, ref missing);
Bye
It’s a pity you don’t have a donate button! I’d without a doubt donate to this superb blog! I suppose for now i’ll settle for
book-marking and adding your RSS feed to my Google account.
I look forward to new updates and will talk about this site with my Facebook group.
Chat soon!
LikeLike
I am really impressed with your writing
skills as well as with the layout on your blog. Is this a paid theme or did
you customize it yourself? Anyway keep up the nice quality
writing, it’s rare to see a nice blog like this one today.
LikeLike
A person essentially assist to make significantly articles I’d state. That is the very first time I frequented your web page and thus far? I surprised with the analysis you made to create this actual put up incredible. Wonderful job!
LikeLike
but the above code is not working on hosting server
LikeLike
Is it possible to insert a mergefield (e.g. -«mergefield1») into a word document through C#?
LikeLike
If you use COM on server side (e.g. ASP.NET) you would probably have problems like this: http://support.microsoft.com/kb/257757.
You can try out this library http://flexdoc.codeplex.com/ which is free or some good but affordable third party like http://www.docentric.com.
LikeLike
You can create word document using c# language with this C# Libray for Word , it can create, edit, read or even convert your word documents to many other formats. I hope it will solve everyone’s problem who is having trouble creating word file in c#.
LikeLike
Your style is really unique compared to other people I’ve read stuff from.
Thank you for posting when you’ve got the opportunity, Guess I will just bookmark this
page.
LikeLike
This is very interesting, You are a very skilled blogger.
I’ve joined your rss feed and look forward to seeking more of your wonderful post.
Also, I’ve shared your web site in my social networks!
LikeLike
Οn voitt directement que vous connaissez bien ce ԛue vous avancez
LikeLike
Great post!! But in my situation ,Microsoft Office is not allowed to be installed on either developer or client machine, so I can’t use Mircrosoft.Word.Object Library in my project. Anyway, I found C# Word component very helpful in creating/editing Word file without MS Automation.
LikeLike
Je n’ai pɑss terminé de liге par contre je passe dans la journée
LikeLike
Un considérable mercі à l’admin duu site
web
LikeLike
Un ρoste rempli de vérité
LikeLike
Je suis pressée de lire un autrе ρоst
LikeLike
Incroyable post, persistez de сette façon
LikeLike
Cе poste est vrɑiment ploein de conseils
LikeLike
Ԛuel hasarɗ je compttais faire un petit post identique aս tiens
LikeLike
Good post. I learn something new and challenging on sites I stumbleupon everyday.
It’s always useful to read content from other writers
and use something from other sites.
LikeLike
If you want to grow your experience just keep visiting this website and be updated with the newest news update posted here.
LikeLike
You really make it seem so easy with your presentation but
I in finding this topic to be actually one thing that I believe I’d by
no means understand. It sort of feels too complex and extremely huge for me.
I’m taking a look forward for your subsequent publish, I will
try to get the hold of it!
LikeLike