Adding Excel Object Library conflicts between Excel.TextBox & Forms.TextBox in Office

scttech

New member
Joined
Nov 7, 2013
Messages
3
Programming Experience
1-3
Adding Excel Object Library conflicts between Excel.TextBox & Forms.TextBox in Office

I have implemented several basic Excel functions in my vb.net application (opening an xls workbook & accessing worksheet). However, it appears that when I add the Reference "Microsoft Excel 14.0 Object Library" the application is getting confused with the standard System.Windows.Forms objects and has the following error: InvalidCastException occured: Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'Microsoft.Office.Interop.Excel.TextBox'. The Invalid Cast error is happening when calling a subroutine & passing the application form controls (check boxes, GridView, TextBox, etc) from my main "Form1" class into a UtilityFunction module. UtilityFunction has a reusable Windows Registry sub routine which retrieves/saves values at application startup/closing. This worked in the past, but now is crashing at the subroutine call in the Form1 class. The exception occurs during application runtime, not during the compile/build.


Also, I deleted the text controls from the form and created them manually, but I'm still getting the same exception error.
 
Add a namespace alias for one of the namespaces, in a Form class I think I would leave System.Windows.Forms as standard and alias the interop namespace, for example:
Imports X = Microsoft.Office.Interop.Excel

In automation code you then qualify the interop types with the alias, for example X.Application, X.Worksheet etc.
 
Thank you JohnH! I solved this problem by using the following code:
Imports X = Microsoft.Office.Interop.Excel

Dim xlApp As X.Application = New X.Application
 
Back
Top