.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Insert Images in word file using c#

Posted By:Hiren Soni       Posted Date: August 17, 2010    Points: 15    Category: C#    URL: http://www.dotnetspark.com  

here i will show you how to insert image in word file using C#
 

Subject: Insert Multiple Image in word file using c#

First add the reference from the com tab, and add Microsoft word 12.0 object library.

 

Code and explanation:

    private void button1_Click(object sender, EventArgs e)
    {

        // 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;

        // set openfiledialog to select multiple image files
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF";
        ofd.Title = "Select Image To Insert....";
        ofd.Multiselect = true;


        // if user select OK, then process for adding images
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            // iterating process for adding all images which is selected by filedialog
            foreach (string filename in ofd.FileNames)
            {
                // now add the picture in active document reference
                doc.InlineShapes.AddPicture(filename, 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.

 


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend