none
How to insert multiple image in word document using Interop.Word in c#.net RRS feed

  • Question

  • I have a list of data to insert in Word document.I am using Microsoft Word Object Library dll for data insert in Word document.my data look like first column is image and other column is name,address etc..One record should be display in only one page in created document.when I insert data in word document using below code at that time all image display first and then other data display .But I need to display record wise data look like image,name,address etc...

    My C# code is :

    //Get report Data list

            List<Art> ListData = oArts.LoadListData();
    
            Paragraph para = document.Content.Paragraphs.Add(ref missing);
            para.Range.InsertParagraphBefore();
    
            string imgPath;
    
            for (int i = 0; i < ListData.Count; i++)
            {
               string path = Server.MapPath("~/ArtImages/") + ListData[i].ArtID;
               imgPath = path + @"\" + ListData[i].ImageMaster.ImagePath;
    
               InlineShape map = document.InlineShapes.AddPicture(imgPath, ref missing, ref missing, ref missing);
               map.Height = 350;
               map.Width = 350;
    
              para.Range.Text = "Name : " + ListData[i].Name + Environment.NewLine;
              para.Range.Text = "Address : " + ListData[i].Address + Environment.NewLine;
              para.Range.InsertParagraphAfter();
              object oPageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
              para.Range.InsertBreak(ref oPageBreak);
    
            }
    
            //Save the document
    
            object filename = "@C:\\ArtDetails.doc";
            document.Close(ref missing, ref missing, ref missing);
            document = null;
            winword.Quit(ref missing, ref missing, ref missing);
            winword = null;



    • Edited by Janki Chavda Wednesday, April 16, 2014 8:31 AM
    • Moved by Fred Bao Thursday, April 17, 2014 5:56 AM Office Related
    Wednesday, April 16, 2014 8:29 AM

Answers

  • Hi,

    According to your code and description, you wanted to insert records one by one, but in your code, you inserted all of the images, and then inserted the name and address.

    Consider above scenario,you can use the Range.InsertAfter() to Insert the specified text at the end of a range or selection.

    ...

    Word.InlineShape map = document.InlineShapes.AddPicture(imgPath); map.Height = 350; map.Width = 350; map.Range.InsertAfter("Name : " ListData[i].Name+ Environment.NewLine);

    ...

    In addition, did you run your application in server side?

    I don’t suggest you using Office automation in server-side.

    More information, you can refer to following KB article:

    http://support.microsoft.com/kb/257757/en-us

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Marvin_Guo Wednesday, April 23, 2014 6:17 AM
    Thursday, April 17, 2014 12:28 PM

All replies

  • Hello,

    I move it to the Word For Dev forum for getting better help.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, April 17, 2014 5:54 AM
  • Hi,

    According to your code and description, you wanted to insert records one by one, but in your code, you inserted all of the images, and then inserted the name and address.

    Consider above scenario,you can use the Range.InsertAfter() to Insert the specified text at the end of a range or selection.

    ...

    Word.InlineShape map = document.InlineShapes.AddPicture(imgPath); map.Height = 350; map.Width = 350; map.Range.InsertAfter("Name : " ListData[i].Name+ Environment.NewLine);

    ...

    In addition, did you run your application in server side?

    I don’t suggest you using Office automation in server-side.

    More information, you can refer to following KB article:

    http://support.microsoft.com/kb/257757/en-us

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Marvin_Guo Wednesday, April 23, 2014 6:17 AM
    Thursday, April 17, 2014 12:28 PM