Shailesh Rama
Shailesh Rama

Reputation: 158

add images after the last image/text in a word document using interop C#

i am having an issue trying to add images to a word document after the last image/word, trying to add a new page and adding the image to this would work as well.

this is the code i have so far for inserting the image.

Document aDoc;
            object isVisible = false;
            object readOnly = true;
            wordApp.Visible = false;
            aDoc = wordApp.Documents.Open(
                ref filename, ref MISSING,
                ref readOnly, ref MISSING, ref MISSING, ref MISSING,
                ref MISSING, ref MISSING, ref MISSING, ref MISSING,
                ref MISSING, ref isVisible, ref MISSING, ref MISSING,
                ref MISSING, ref MISSING);
            aDoc.Activate();
object range = aDoc.Content.End ;
aDoc.InlineShapes.AddPicture(@"C:\fullImagePath\Image.jpg", MISSING,MISSING, range)

i keep getting a type mismatch error

i also tried adding a new page but this deletes the data already existing on the word document adds a new page but adds the image on the 1st page.

aDoc.Content.InsertBreak(WdBreakType.wdPageBreak);
aDoc.InlineShapes.AddPicture(@"C:\fullImagePath\Image.jpg");

i am quite new to using Interop so i am a bit confused on how to do things

Upvotes: 2

Views: 7696

Answers (1)

Shailesh Rama
Shailesh Rama

Reputation: 158

aDoc.Application.ActiveDocument.Shapes.get_Item("Rectangle 6").Select(); //finds the 1st image
            aDoc.Application.Selection.MoveDown(WdUnits.wdScreen, 2);// adds tow pagedown presses
            aDoc.Application.Selection.InlineShapes.AddPicture(@"C:\fullImagePath\Image.JPG");//adds image on new page
            aDoc.Application.Selection.InsertBreak(WdBreakType.wdPageBreak);//adds a page break
            aDoc.Application.Selection.InlineShapes.AddPicture(@"C:\fullImagePath\Image.gif");//adds image on new page

i used the above to get the images in the places i needed them to be

Upvotes: 3

Related Questions