Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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) |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
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) |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
VSTO 2005 Excel picturebox control question | Excel Programming | |||
VSTO 2005 Excel picturebox question | Excel Programming | |||
VSTO 2005 | Excel Programming | |||
Calling functions developed in VSTO 2005 from Office Excel 2003 | Excel Programming | |||
reference vsto Excel ListObject in Vb.Net 2005 project/app | Excel Programming |