Setting Built In and Custom Document Properties using C#

Create a new windows application project and add a button to it.

On click of that button, we will open a document and add values for a built in and custom document properties.

Than add reference to (Word 10.0 or 11.0 object library) and Microsoft.Office.Core.dll within COM tab of Add reference dialog box.

After adding reference, add this directive

using Microsoft.Office.Interop.Word

using System.Reflection;

Put the following code in the button click event handler

// For optional parameters create a missing object

object missing = System.Reflection.Missing.Value;

// Create an object for filename which is the file to be opened

object fileName = @”C:\MySecond.doc”;

// Create an object of application class

ApplicationClass WordApp = new ApplicationClass();

// open the document specified in the fileName variable

Document adoc = WordApp.Documents.Open(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);

 

// setting the document built in properties

object oDocBuiltInProps = adoc.BuiltInDocumentProperties;

Type typeDocBuiltInProps = oDocBuiltInProps.GetType();

// setting the Title property with value “My Proposal”

string strIndex = “Title”;

string strValue = “My Proposal”;

typeDocBuiltInProps.InvokeMember(“Item”,

BindingFlags.Default |

BindingFlags.SetProperty,

null, oDocBuiltInProps,

new object[] { strIndex, strValue });

 

 

// setting the custome document properties

object oDocCustomProps = adoc.CustomDocumentProperties;

Type typeDocCustomProps = oDocCustomProps.GetType();

// setting the ProposalSentDate custom date property with current date time

string strIndex1 = “ProposalSentDate”;

string strValue1 = DateTime.Now.ToShortDateString();

object[] oArg = { strIndex1, false, Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeDate, strValue1 };

typeDocCustomProps.InvokeMember(“Add”,

BindingFlags.Default |

BindingFlags.InvokeMethod, null,

oDocCustomProps,oArg);

 

WordApp.Visible = true;

 

Byee…

Creating and Updating table of contents in word document using C#

Create a new windows application project and add a button to it.

On click of that button, we will open a document and first add a heading 1 programmatically and then insert a table of content programmatically and update it

Than we will add reference to (Word 10.0 or 11.0 object library) within COM tab of Add reference dialog box.

After adding reference, we’ll add this directive using Microsoft.Office.Interop.Word than we’ll put the following code in the button click event handler

// For optional parameters create a missing object

object missing = System.Reflection.Missing.Value;

// Create an object for filename which is the file to be opened

object fileName = @”C:\MySecond.doc”;

// Create an object of application class

ApplicationClass WordApp = new ApplicationClass();

// open the document specified in the fileName variable

Document adoc = WordApp.Documents.Open(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);

Range myRange = adoc.Range(ref missing, ref missing);

myRange.InsertAfter(“Hello Mickey Mouse GG “);

object oStyleName = “Heading 1”;

myRange.set_Style(ref oStyleName);

object start=WordApp.ActiveDocument.Content.End – 1;

Range rangeForTOC = adoc.Range(ref start, ref missing);

TableOfContents toc=adoc.TablesOfContents.Add(rangeForTOC, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

toc.Update();

Range rngTOC = toc.Range;

rngTOC.Font.Size = 10;

rngTOC.Font.Name = Georgia;

WordApp.Visible = true;

Bye…

Opening and inserting a picture in word document programmatically using C#

Create a new windows application project and add a button to it.

On click of that button, we will open a document and insert a picture to it.

(In the doc file insert a table at the location where you want the picture to appear.)

Than add reference to (Word 10.0 or 11.0 object library) within COM tab of Add reference dialog box.

After adding reference, add this directive

using Microsoft.Office.Interop.Word

Put the following code in the button click event handler

// For optional parameters create a missing object

object missing = System.Reflection.Missing.Value;

// Create an object for filename, which is the file to be opened

object fileName=@”C:\MySecond.doc”;

// Create an object of application class

ApplicationClass WordApp = new ApplicationClass();

// open the document specified in the fileName variable

Document adoc = WordApp.Documents.Open(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);

// We can insert the picture using Range objects AddPicture method

// To insert a picture at a particular location in the word document

// insert a table over there and then refer that location through range object

Range rngPic = adoc.Tables[1].Range;

// we can even select a particular cell in the table

//Range rngPic = rng.Tables[1].Cell(2, 3).Range;

rngPic.InlineShapes.AddPicture(@”C:\anne_hathaway.jpg”, ref missing, ref missing, ref missing);

WordApp.Visible = true;

 

Bye

 

Inserting or appending document or file in a word document programmatically using C#

Create a new windows application project and add a button to it.

On click of that button, we will create a new document and append or insert content of two documents in it.

First add reference to (Word 10.0 or 11.0 object library) within COM tab of Add reference dialog box.

After adding reference, add this directive

using Microsoft.Office.Interop.Word

Put this code on button click

// For optional parameters create a missing object

object missing = System.Reflection.Missing.Value; 

// Create an object of application class

ApplicationClass WordApp = new ApplicationClass();  

// add a document in the Application

Document adoc=WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing); 

// declare variables for setting the position within the document

object start = 0;

object end = 0; 

// create a range object which starts at 0

Range rng = adoc.Range(ref start, ref missing);

// insert a file

rng.InsertFile(@”C:\MyFirst.doc”, ref missing, ref missing, ref missing, ref missing);

// now make start to point to the end of the content of the first document

start = WordApp.ActiveDocument.Content.End – 1; 

// create another range object with the new value for start

Range rng1 = adoc.Range(ref start, ref missing);

// insert the another document

rng1.InsertFile(@”C:\MySecond.doc”, ref missing, ref missing, ref missing, ref missing);

// now make start to point to the end of the content of the first document

start = WordApp.ActiveDocument.Content.End – 1; 

// make the word appliction visible

WordApp.Visible = true;

 

Bye

Creating Word document using C#

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)

DLL

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