web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Nishant Rana’s Weblog / Opening and inserting a pic...

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

Nishant Rana Profile Picture Nishant Rana 11,325 Microsoft Employee

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

 


This was originally posted here.

Comments

*This post is locked for comments