Are InlineShapes and ContentControls collection unique, singletons or neither?

R

robert

Hi all,
I wonder if someone can shed light on this problem which I have somehow
solved/circumvented but without exactly understanding why or how. I'm
programming an add-in for Word 2007 in C# using VS2005 + VSTO. It's the
first project I've done in Word so I'm still a Word programming novice.

The task is to populate a table with images from disk as well as
specific text that can be read from each image file with some other
code. Later on I want to read a saved word document and retrive images
and text strings from that document into member data of the Add-In.
This is particular easy with Word 2007 docs as they are zip files and
InlineShape images are stored in \word\media subfolder within the zip
files. To that end the Add-In is bookmarking each InlineShape with the
image file name and is using an unzip routine whenever I reopen the
word file.

Without going into too much details suffice it to say that the code for
the Add-In amounts to messing about with the documents InlineShapes
collection and the ContentControls collection. I thought I could keep
my code simple by creating private instances of an InlineShapes
collection and a ContentControls collection within my C# add-in which I
would then create in the relevant table cells and populate with my
images and text.

However, I got errors amounting to literally every other instance of
the supposedly populated InlineShape members of my collection being a
null value later on in the executing code. This applied as well to the
ContentControls collection I created.

Eventually I rewrote my code and am now exclusively using the
InlineShapes and ContentControls collection inherent in the word
document. This appears to work perfectly. So I suspect that creating
any of the collection objects already present in a word document is
asking for trouble, right?

It would be great if anyone could clarify what the flippin heck is
going on with those InlineShapes and ContentControls collections as it
has really been doing my head in (anyone got an aspirin?)


Rob Oeffner
 
C

Cindy M.

Eventually I rewrote my code and am now exclusively using the
InlineShapes and ContentControls collection inherent in the word
document. This appears to work perfectly. So I suspect that creating
any of the collection objects already present in a word document is
asking for trouble, right?
Not that I'm aware of. I've assigned objects to both arrays and
collections (most recently ContentControls to a collection - although it
was VBA, but that shouldn't make a difference) - no problems.
It would be great if anyone could clarify what the flippin heck is
going on with those InlineShapes and ContentControls collections as it
has really been doing my head in (anyone got an aspirin?)
Impossible without seeing the code structures that cause the problem
(the ones that build then use the collection).

The one thing you problem description does remind me of is what happens
if I run through a collection and the actions taken actually change the
members of the collection (delete, re-order, something of that nature).

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
R

robert

Thanks for replying Cindy,

Below is a code snippet that gives me headache. The task I'm trying to
accomplish is to replace certain images inserted as InlineShapes in a
Word document. The C# code below works, sort of, as long as I enclose
it with a try-catch exception handler. When the Word AddIn runs, the
UpdateTiff routine will be executed many times. What happens is that
the exception handler frequently prompts that the mypic object has been
deleted. Despite sprinkling the code with breakpoints I haven't so far
been able to verify this with the debugger as VS2005 tends to freeze
for about 15-20 seconds when trying to examine the mypic object.
Eventually when VS2005 becomes alive again it simply tells me that:
"Function evaluation disabled because a previous function evaluation
timed out. You must continue execution to reenable function
evaluation".

If I comment out the messagebox in the catch handler. The code will run
but ever so often not assign the Shadow.Visible property to the mypic
object. I thought I could use the get_IsObjectValid() function to test
for the validity of the mypic object but to no avail.

So I'm interested in learing how to fix the problem with the supposedly
deleted mypic InlineShape as the code as it stands does not seem robust
enough for general usage.


Robert Oeffner




using Word = Microsoft.Office.Interop.Word;
..
..
private void UpdateTiff(string tiffname)
{
try
{
Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
if (doc == null)
return;

int i;
for (i = 0; i < doc.InlineShapes.Count; i++)
{
if (tiffname == fname)
break;
}

object b1 = false, b2 = true, refunit = Word.WdUnits.wdCharacter;
Word.Range rng;
float swidth = doc.InlineShapes[i + 1].Width;
doc.InlineShapes[i + 1].Select();
Word.Selection sln = Globals.ThisAddIn.Application.Selection;

sln.Cut();
object dirend = Word.WdCollapseDirection.wdCollapseEnd as object;
sln.Collapse(ref dirend);

object orng = sln.Range as object;
Word.InlineShape mypic = doc.InlineShapes.AddPicture(tiffname, ref
b1, ref b2, ref orng);
if (Globals.ThisAddIn.Application.get_IsObjectValid(mypic))
{
fname = tiffname;
mypic.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue;
mypic.Shadow.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
mypic.Width = swidth;
}
mypic = null;
}
catch (SystemException ex)
{
MessageBox.Show(ex.ToString());
}

}
 

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