inserting an InlineShape picture into a Shape object 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;
bool bpicfound;

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("mypicture.jpg", ref
blink2file, ref bsavewithdoc, ref orng);

break;
}
}
 
R

robert

Here's some more weird info.
When I put a breakpoint on the line before the AddPicture function and
list the properties of the picinsertpoint variable in the debugger's
immediate-window with the command
?picinsertpoint
then the code doesn't throw the exception mentioned in my first post
but correctly inserts the InlineShape into the Shape object. It almost
seems like the debugger somehow initialises the picinsertpoint
variable.
Why is this and does anyone know how to fix the problem?

Rob
 

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