Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / October 2004

Tip: Looking for answers? Try searching our database.

Error: Invalid Cast Exception?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Robin Tucker - 14 Oct 2004 14:31 GMT
Hi people,

I'm using Word automation, embedding some OLE objects into a document and
then later on creating links to object within them.  Anyway, the embedding
phase goes through just fine.  But when I attempt the second pass, accessing
the InlineShape object and attempting to cast it to my given object, it
fails.

   A first chance exception of type 'System.InvalidCastException' occurred
in CR.exe

   Additional information: No such interface supported

I can SEE the objects embedded in Microsoft Word.  I can select, activate,
manipulate and otherwise play around with them.  The InlineShapes collection
contains all of the objects too.  But when I try and cast the object to the
given type, I get the above error:

   ' Fetch the shape (success)

   theShape = theDocument.ResultingDocument.InlineShapes.Item(1)

   ' Fetch the Thermo.Image. (fails)

   theOLEObject = CType(theShape.OLEFormat.Object, Thermo.Image)

Now, this cast from theShape.OLEFormat.Object to "Thermo.Image" (our own OLE
object class) fails although the "ProgID" of the InlineShape.OLEFormat is
"Thermo.Image"!

Can anyone explain what is going on here?  Perhaps I am doing something
wrong with COM and my interop?  The odd thing is, I iterate through 90 odd
objects, some of them succeed and some don't.  Any ideas?

Thanks.
Robin Tucker - 18 Oct 2004 13:52 GMT
I fixed this one.  It was quite simple in the end - Word doesn't keep all of
its embedded objects in the running state all the time.  In order to access
the interface you first have to ensure the object is in the running state,
ie: OLEFormat.Activate() (or something).  This ensures the object is in the
running state and the interfaces can now be accessed.  Slower but it works
:)

> Hi people,
>
[quoted text clipped - 31 lines]
>
> Thanks.
Robin Tucker - 18 Oct 2004 15:11 GMT
Well actually, executing the verb "wdOLEVerbHide" seems to work just as well
without the overhead of activation.

>I fixed this one.  It was quite simple in the end - Word doesn't keep all
>of its embedded objects in the running state all the time.  In order to
[quoted text clipped - 38 lines]
>>
>> Thanks.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.