VSTO 2005 - How to use ThisWorkbook as an Excel.Workbook

D

dbKemp

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(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)
 
N

NickHK

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 said:
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(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
 

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