From VBA to VSTO - ctype(Globals.thisworkbook...error

D McLellan

New Member
Joined
Sep 16, 2008
Messages
3
I am using Visual Studio 2008 Professional Trial Edition, when i use the following code -
PublicClass Form1
Dim thisWB As Excel.Workbook = CType(Globals.ThisWorkbook, Excel.Workbook)

EndClass

i receive the following error message. "1. Runtime errors might occur when converting 'Excelwork1.thisworkbook' to 'Microsoft.Office.Interop.Excel.Workbook'"

Can you help?
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
I take it you are using a Workbook project (rather than an add-in project)? Do you have the right version of the Office PIAs installed?
 
Upvote 0
That’s right, I am using Workbook project with Excel 2007. I’m not sure which version off Office PIA I’m using, how do I tell?<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
 
Last edited:
Upvote 0
Actually, it's not a PIA issue (just been checking it out). The problem is that the Globals.ThisWorkbook object does not inherit from the Microsoft.Office.Interop.Excel.Workbook, but from the Microsoft.Office.Tools.Excel.Workbook type.

You would need to use:
Code:
Dim thisWbk As Microsoft.Office.Tools.Excel.Workbook = CType(Globals.ThisWorkbook, Microsoft.Office.Tools.Excel.Workbook)

or shorten it to:
Code:
Imports XL = Microsoft.Office.Tools.Excel
Public Class Form1
    Dim thisWbk As XL.Workbook = CType(Globals.ThisWorkbook, XL.Workbook)

End Class
 
Upvote 0
This works a treat, <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
I’ve been banging my head about this one for a while now.<o:p></o:p>
Thanks for all your help.<o:p></o:p>
<o:p></o:p>

Regards,
D.McLellan <o:p></o:p>
 
Upvote 0
Glad to help. It was a good incentive to finally get around to installing VS 2008 on my laptop!
 
Upvote 0

Forum statistics

Threads
1,213,497
Messages
6,113,998
Members
448,539
Latest member
alex78

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top