Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
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)

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VSTO 2005 Excel picturebox control question Doug[_16_] Excel Programming 6 August 4th 06 06:41 AM
VSTO 2005 Excel picturebox question doug Excel Programming 0 July 25th 06 02:15 PM
VSTO 2005 Le Excel Programming 2 March 16th 06 04:46 PM
Calling functions developed in VSTO 2005 from Office Excel 2003 Neil Janabi Excel Programming 0 March 16th 06 10:50 AM
reference vsto Excel ListObject in Vb.Net 2005 project/app softengine Excel Programming 0 September 27th 05 08:21 PM


All times are GMT +1. The time now is 04:17 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"