Specified Cast is not Valid

C

calvinX

Sorry about the cross-post but I am in a bit of a bind.

I am developing an Add-in for Visio 2002 using VS.NET and I am having
trouble getting a reference to an excel worksheet object that exists on the
page. Here is the code I am having a problem with (doc is declared as
Microsoft.Office.Interop.Visio.Document in the function parameters):


sel = doc.Application.ActiveWindow.Selection '>> Works
obj = sel.Item(1).Object '>> Works
xlSheet = CType(obj, Microsoft.Office.Interop.Excel.WorkSheet) '>>
Throws "Specified Cast is not Valid" exception
MsgBox("Got sheet = " & xlSheet.Name) '>> never gets here


I tried casting to a Workbook object but this did not work either. How can
I get my worksheet object out of the obj variable?


Thanks

Calvin
 
O

One Handed Man [ OHM# ]

Start by turning OPTION STRICT = ON in your project, then checking the
types. If the types are the same but you are still getting the problem, try
DirectCast instead.

Regards - OHM
 

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