![]() |
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) |
VSTO 2005 - How to use ThisWorkbook as an Excel.Workbook
As you are outside of Excel's VBA, "ThisWorkbook" does not exist, because
your code is not "in" the workbook. What you really need is "ThatWorkbook", beacuse you are on the outside, looking in. <VB Code Dim ThatWB As Excel.Workbook set ThatWb=XLApp.Workbook.Open(PathAndFileName) </VB Code You can use the various "ActiveXXX" objects and "Selection" though, if necessary. NickHK "dbKemp" wrote in message ... 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) |
All times are GMT +1. The time now is 10:39 AM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com