View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
dbKemp[_2_] dbKemp[_2_] is offline
external usenet poster
 
Posts: 1
Default VSTO 2005 - How to use ThisWorkbook as an Excel.Workbook

How do I create an instance of the VBA 'ThisWorkbook' so I can refer to it
later in code?
Having problem with this line of code I found in book 'From VBA to VSTO'

Dim thisWB As Excel.Workbook = CType(Globals.ThisWorkbook, Excel.Workbook)

I have tried it in form class & worksheet class and always get the following
error message:

System.InvalidCastException was unhandled by user code
Message="Unable to cast object of type 'ExcelWorkbook1.ThisWorkbook' to
type 'Microsoft.Office.Interop.Excel.Workbook'."
Source="ExcelWorkbook1"
StackTrace:
at ExcelWorkbook1.Form1.Form1_Load(Object sender, EventArgs e) in
C:\Documents and Settings\l222254\Local Settings\Application Data\Temporary
Projects\ExcelWorkbook1\Form1.vb:line 4
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Mes sage& m)
at System.Windows.Forms.ContainerControl.WndProc(Mess age& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)