Insert Picture with hyperlink in word file using C#


HTML clipboard

Here you can learn how to add picture with hyperlink in word file using C#.

Output:

1.gif

Code and explanation:

            // first we are creating application of word.
            Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();

            // now creating new document.
            WordApp.Documents.Add();

            // see word file behind your program

            WordApp.Visible = true;

            // get the reference of active document
            Microsoft.Office.Interop.Word.Document doc = WordApp.ActiveDocument;
 
            // get the range
            Microsoft.Office.Interop.Word.Range drange = doc.Range();

            // now add the picture in active document reference and store the reference of picture in inlineshape object
            Microsoft.Office.Interop.Word.InlineShape picture = drange.InlineShapes.AddPicture("c:\\logo.gif", Type.Missing, Type.Missing, Type.Missing);

            // noew add the hyperlink to object of inlineshape
            drange.Hyperlinks.Add(picture, "http:\\www.c-sharpcorner.com", Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            // file is saved.
            doc.SaveAs("c:\\hello.doc", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            // application is now quit.
            WordApp.Quit(Type.Missing, Type.Missing, Type.Missing);


Hope you understand it.

Thank you.
 


Similar Articles