Word interop: how to gaet the name of an InlineShape object?

Joined
Jul 20, 2020
Messages
11
Programming Experience
5-10
Hey guys, happy Friday! :)

As usual, Microsoft's propensity to create ultra-complex hierarchies of objects upon objects upon objects has me completely baffled. So in Word, there are 2 "shape" (picture) object types that I know of: Shapes and InlineShapes. Shapes have a "Name" property that corresponds with what users have called them, or default placeholders like "Picture 1". InlineShapes are significantly more mysterious, much better obfuscated from the view of most developers. I've tried things like InlineShapes[1].Range.ShapeRange[with or without numeric index].Name, InlineShapes[1].Title, I've tried casting from an InlineShape to a Shape, and all through this hair-jerker of a bug battle it's been error after error after error, an infinite loop of trial / error / DuckDuckGo. I've been all over MSDN, Stack Overflow, and other usual places like this. Anyone know where in the name of William Q. Gates they hid that darn name/title/label/McWhatsits? :D

EDIT: I get normally you'd want to see some code (apart from the examples above) but the only code I have is the code that gets the reference to the object (and I know I have a valid object). Sorry in my complete frustration I forgot that (didn't think it was relevant, but of course it is since I didn't tell u what the errors were lol). Here's that, in case you want it:
C#:
// Create a new Word window
var word = new Application();
counter.Add(word);  // Counter is a bugfix - they forgot to automatically free the unmanaged COM pointers in the interop API, so if you don't do C++-style manual memory management you get a Word/Excel process hanging in limbo until you reboot or discover it.  So retro problems call for retro solutions - this is a reference-counter. :D
word.Visible = true;

// Open the doc I'm using for testing
var documents = word.Documents;
counter.Add(documents);
var doc = documents.Open(@"C:\whatever.docx");
counter.Add(doc);

var shapes2 = doc.InlineShapes;  // "shapes2" because I did something similar with regular "shapes"
counter.Add(shapes2);
for (var i = 1; i <= shapes2.Count; i++)
{
    var shape = shapes2[i];
    counter.Add(shape);
    Console.WriteLine(shape.This.That.Object.Interface.Property.Method.Collection.Blah.Blah.Blah.Blablabloogendah.ObviouslyThisIsTheProblemLOL);
}

// EDIT 2: Forgot a line :D
while(true) {
    hair--;
    sanity = 7 * "cheese" / (0x0C0005 | "pepperoni"); // hmmm, must be lunchtime. :D
}
 
Last edited:
lol yeah, thank you Captain Obvious :D
Seriously tho, thanks for trying! I read that as well, but here's the thing: in Word, they still have something "name-like": Word gives them names (titles? labels? pseudonyms? pen names? lol idk, they're names but they're not called a Name). That's what I'm trying to find. Non-numeric indices like "Picture 1", which (for non-inline shapes) they called a Name, but for inline shapes they call a {...?}. Buried deep somewhere unknown is the equivalent for inline shapes, or otherwise Word would not have "names" for them. Any ideas on that?
 
If they have it must be one of the listed properties. In addition to reading the docs you can also inspect the object in debugger and read through the properties and their values.
 
That's just the trouble - which properties? Which objects? It's a bit of a maze, cuz it's not like a simple data structure where its properties are its properties; one of its properties is a Range (or was it ShapeRange?) object which has another set of properties and another object and so on and all these have properties too (and I think one of these points back to the Word window, so I know it's not the one lol). That would be way too easy. :D

I just figured there must be some known workaround, or that maybe this is a common problem for newcomers to Word interop or something like that. Guess that's not the case, so looks like I'll have to spend another day pouring over MSDN to see if I can somehow track down which sub-sub-sub-sub-object it is; back to the infinite loop of trial/error/DuckDuckGo :D
 
The properties that was listed in docs page you were provided. Loop all? foreach (Word.InlineShape shape in doc.Content.InlineShapes)
 
Yeah, the docs "I was provided" (meaning MSDN) are as dense, vague and confusing as the API itself. Everything I've managed to dig up from there seems to crash my program with vague errors (even if I try the same thing in VBA), with zero explanation of why.

For example, if (in VBA) I do this:

C#:
Debug.Print ActiveDocument.InlineShapes(1).Range.ShapeRange.Name

I get something like "invalid procedure or argument". If by "procedure" they mean "function" than what the heck do they mean? I didn't call any functions/methods. And what "argument"? Again, I didn't call any functions/methods, so what does "argument" even mean in this context? And if I try it in C# I get a low-level COM error (and although I do write a lot of C++, I haven't actually memorized every HRESULT in the book - and even if I looked that up on MSDN, it would probably translate to "invalid procedure or argument" :D ). There is no explanation on MSDN, or at least none that I've been able to find. Unless of course I'm supposed to read every single page on their entire website. :D

Okay, all joking aside, I really like your idea of doing some kind of "loop all" - is it possible to loop over all properties as well as all objects in the collection? Like the keys and values? That might help find out where they hid the "name" by brute force. :D
 
On the left hand side of the documentation I linked to in post #2 you will find "Title" and "AlternativeText" under properties.
 
Yeah, Title threw "System.NotImplementedException: The method or operation is not implemented." Nice and vague (and again, what method/operation?). So an inline shape that Word identifies as "Picture 1" doesn't have a title, because... titles haven't been fully implemented yet? Then why do Visual Studio/the VBA editor support them as properties of the object? Must be some COM object's getter/setter, spewing this garbage under the hood - if nothing else, Word interop has taught me Marshal.ReleaseComObject - the C# API is a suuuuuuper-thin layer over the unmanaged code IMO :D

On a side note, it has no alt text, but I knew that already (part of the program is actually checking for that, to help users fix that). In fact in my code I have a check for String.IsNullOrEmpty before trying to get its "nickname" - so I know it's either null or empty).

So it's not Title cuz they haven't finished implementing that feature yet, and it's not AlternativeText... something in the "Field" property/object maybe?
 
Interesting... I'm surprised the VBA editor and Visual Studio don't catch that. But at this point I think the only logical move is to give up. Wherever the mysterious "Picture 1" comes from, it's clearly not worth any more aggravation than it's already caused. Guess it's something only Word can see, not available to the API. Thanks anyway.
 
InlineShape has a Range property of course since it is anchored to one, a Range on the other hand is very generic type that represents an "area". The Range object itself has many properties that doesn't relate to the InlineShape that is attached to it.
 
The VBA Editor was "catching" it for me. When I typed in ActiveDocument.InlineShapes(1), the next option given to me was AlternativeText as well as Title among other properties.
 
I was trying to do the same thing. The name in the Selection Pane seems to be generated from the .Type and the .Item number.
 

Latest posts

Back
Top Bottom