inserting a picture in a shape in C#

R

robert

Hi,
I'm developing a Word 2007 Add-In in C# using VS2005 + VSTO. I'm
trying to insert an image into a shape object in an existing word
document. The shape object has a unique text string in it so that it
can be identified when the code is running.

Having recorded a VBA macro and used the generated code as a template
I thought that the lines below in C# would do the job, but to no
avail. Whenever, the AddPicture function is called it throws an
exception saying
that "index refers beyond end of list", whatever that means.

If anyone has a suggestion for why the heck this code doesn't work I'd
be grateful as I've spent 3 days so far getting nowhere.

Thanks,

Rob


// code snippet to insert an image into a shape object

foreach (Word.Shape ashape in doc.Shapes)
{
string otext = "insertpoint1"
object rp = Word.WdReplace.wdReplaceOne;
object oreplacement = "";
object omissing = Type.Missing;
object blink2file = true, bsavewithdoc = true;

Word.Range txtfrm = ashape.TextFrame.TextRange;
bpicfound = txtfrm.Find.Execute(ref otext, ref omissing,
ref omissing, ref omissing, ref omissing, ref omissing, ref
omissing,
ref owrap, ref omissing, ref oreplacement, ref rp, ref omissing,
ref omissing, ref omissing, ref omissing);

if (bpicfound)
{
picinsertpoint = doc.ActiveWindow.Selection.Range;
picframe = ashape.TextFrame;
object orng = picframe.TextRange;
picframe.TextRange.InlineShapes.AddPicture(selectedfiles[i - 1],
ref blink2file, ref bsavewithdoc, ref orng);

break;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top