Hello again folks. Here's an update.
First of all, khcharles is correct about what I am trying to do: I have
a Word document containing embedded objects, potentially of mixed type
and origin. I need to extract these and save them out to a folder.
Next, I have a skeleton VS.Net solution which I will share with anyone
who wants it (khcharles, I will send you a copy).
It seems to be possible to access embedded OLE objects in two quite
separate ways, using the Word automation API. First by iterating the
Word document's InlineShapes, and testing for
wdInlineShapeEmbeddedOLEObject, and second by iterating the document's
Fields and testing for wdFieldEmbed.
In both cases, the OLEFormat property of the associated Word object
(i.e. InlineShape or Field object, respectively) yields a
Word.OLEFormat object. This object contains an 'Object' property which
appears to be the way you are supposed to access the embedded OLE
object.
This works fine (for me) when the embedded object is another Word
document, but fails when it is (e.g.) an Excel worksheet. I receive an
InvalidCastException even if I simply extract the Object value into
another (CLR) Object variable, thus:
// Calling context defines: Word.OLEFormat oOLE
Object theEmbeddedObject = oOLE.Object; // throws
InvalidCastException
This does seem odd: you might expect the InvalidCastException if you
tried to cast from System.Object to something more specific, but this
assignment is essentially redundant, i.e. there is no re-interpretation
here.
The OLEFormat object appears to be a wrapper around a
System.__ComObject (i.e. a runtime-callable-wrapper for a COM object).
In the VS.NET debugger you can see that Word correctly handles the COM
object for an embedded Word document, and assigns a Word.DocumentClass
object to the OLEFormat.Object property. When the embedded Excel sheet
is encountered, however, you see that the Object property value is not
assigned - it raises the exception.
The other way to handle this, it seems to me, is to use the OLE ProgID
string to start a COM server of the correct type, then ask that server
to handle the object. All I want to do is call the 'SaveAs' OLE verb
on the object. I haven't located the list of Excel's OLE verbs (can
anyone help with that?) so I don't know the correct syntax to use. But
all this seems really silly when I should be able to pull the OLE
object straight out of the enclosing document!
Cindy, it's good to see a Word MVP working on this issue! Can you help
to resolve this?
Many thanks to all,
Roger Searjeant.