0

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.

1
  • Fully qualify the namespace to your object your wanting. Commented Feb 5, 2014 at 23:05

2 Answers 2

0

When you have a Namespace conflict you need to fully qualify the object you want.

Dim excelTb As New Microsoft.Office.Interop.Excel.TextBox
Dim formTb As New System.Windows.Forms.TextBox
Sign up to request clarification or add additional context in comments.

Comments

0

I solved this problem by using the following code:

Imports X = Microsoft.Office.Interop.Excel

Dim xlApp As X.Application = New X.Application

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.