Adding Excel Object Library conflicts between Excel.TextBox & Forms.TextBox in Office Development - vb.net

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.

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

I solved this problem by using the following code:
Imports X = Microsoft.Office.Interop.Excel
Dim xlApp As X.Application = New X.Application

Related

Pass .net Form as parameter to dll

This is the vb6 dll function code which is built and added as com reference to vb.net exe
Public Function Access1(ByVal txt As String, ByRef res As Object)
MsgBox (TypeName(res))
MsgBox (res.Text)
End Function
Using below .net code I was able to pass component as parameter to vb6 dll.
Dim s As New dllname.class
s.Access1("abc", Textbox1)
The above code worked and msgbox display "Textbox" for typename(res).
But if I try to pass form instead of Textbox , while executing the .net code gives "Specified cast is not valid" error.
How to pass form object to dll function?
Unfortunately, it is not possible to pass a Form back from a COM object.
In order to do this you would need to create an ActiveX containing your form and then you can embed it in your current application.
Alternatively, If you create your form in your COM component, you can show it and then return whatever values you need to your calling code.

vba Run-time error '429': ActiveX can't create object, with user defined vb.net dll (VB.NET 2017, Excel 2016)

I have written a simple dll in VB.NET 2017, and I am trying to call it from VBA (Excel 2016). I have searched through Stack Overflow and MSDN, but despite many hits for this VBA error, I cannot find information related to a user defined dll that solves this particular problem.
I checked the box for the Register for COM Interop option when compiling the dll. I also verified that the Make assembly COM-Visible box was also checked.
Compilation was successful, and I was able to find my library (called TestLibrary2) in References inside the VBA editor. The class I created in it is called TestVBNET. I checked TestLibrary2 to include it in VBA, and then attempted to run the following code in VBA:
Public Sub mySub()
Dim myTest2 As TestLibrary2.TestVBNet
Set myTest2 = New TestLibrary2.TestVBNet
End Sub
When I ran the sub, I got the pop-up dialog with message: Run-time error '429': ActiveX can't create object
Based on what I have read on MSDN and other sites, the expected result is that the TestLibrary2.TestVBNet object should be instantiated successfully.
The code below is the VB.NET code that I compiled to the dll. The GUID's were generated using guidgen at the Windows 10 command prompt.
Imports System.Runtime.InteropServices
<Guid("2CEBF80D-D5FC-4C52-BCF1-E2F076318240")>
Public Interface ITestVBNet
Function addTrig(x As Double, y As Double) As Double
End Interface
<Guid("60983277-A724-4CDD-8ACE-B94CF9546F43")>
<ClassInterface(ClassInterfaceType.None)>
Public Class TestVBNet
Function addTrig(x As Double, y As Double) As Double
Return Math.Sin(x) + Math.Cos(x)
End Function
End Class
Any help and or references would be greatly appreciated. Thanks in advance.
EDIT (per comments below): I compiled the dll as admin, which in fact was required for the Register for COM Interop option to run. I also tried using regasm -- also as admin -- as follows, in the same directory as the compiled dll:
regasm /codebase /tlb TestLibrary2.dll
The following was returned:
RegAsm : warning RA0000 : Registering an unsigned assembly with /codebase can cause your assembly to interfere with other applications that may be installed on the same computer. The /codebase switch is intended to be used only with signed assemblies. Please give your assembly a strong name and re-register it.
Types registered successfully
RegAsm : error RA0000 : An error occurred while saving the exported type library: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
I apologize for being a newb here with respect to registering COM libraries, but what this seemed to indicate was the dll was registered successfully, but could not be transferred to the GAC. It was my intention not to transfer it. When I tried loading it in VBA, the same error as above occurred.
It turns out that the above code compiled to a dll that was indeed callable from a C# client. The problem with VBA, however, was that I neglected to put Implements ITestVBNet following the derived class declaration. In VB.NET, though, in addition to this, one must also explicitly indicate function overriding in the derived function declaration as well; viz (scroll all the way to the right to see the entire line),
Public Function addTrig(x As Double, y As Double) As Double Implements ITestVBNet.addTrig
By replacing the derived class above with the following, I was able to create an instance of the TestVBNet object in VBA and call the addTrig(.) method successfully:
<Guid("60983277-A724-4CDD-8ACE-B94CF9546F43")>
<ClassInterface(ClassInterfaceType.None)>
Public Class TestVBNet
Implements ITestVBNet
Public Function addTrig(x As Double, y As Double) As Double Implements ITestVBNet.addTrig
Return Math.Sin(x) + Math.Cos(x)
End Function
End Class

Why do I get "not defined" on all the Excel objects even after importing Microsoft.Office.Interop.Excel (VB.NET)?

I've got this code:
Imports Excel = Microsoft.Office.Interop.Excel
Public Class FormExcelTest_VB
Private Sub ButtonCreateExcelFile_Click( sender As Object, e As EventArgs) Handles ButtonCreateExcelFile.Click
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
. . .
I got it from here.
It doesn't compile, though; I get:
Type 'Excel.Application' is not defined.
Type 'Excel.Workbook' is not defined.
Type 'Excel.Worksheet' is not defined.
Being unfamiliar with VB[.NET], I thought to myself, "Self, you probably forgot to add a required reference." But then I saw that there is no "References" folder in Solution Explorer for the project (coming from the land of C#, that seems awfully strange to me).
The "helpful" msgs I get when hovering over the rejected code are:
The first one doesn't seem the likely remedy, and the following ones even less so. How am I supposed to know how to resolve (no pun intended) these undefined types?
This still seems bizarre to me, but I discovered that you add references via Project > Add Reference...
I added "Microsoft Excel 12.0 Object Library" and now it compiles.
And View > Object Browser is the path to see which References you have added, I guess; the C# way seems far "friendlier" to me.
It happens sometimes.
Right click on your project name in "Solution Explorer"
Select "Add Reference"
At ".NET" tab scroll and find "Microsoft Excel 12.0 Object Library"
Select it and click on "OK" button.
That's all.

Reference to a non-shared member requires an object reference Error When Opening Form

I got the following error when Trying to Open a Form in Visual Basic
Error 1 Reference to a non-shared member requires an object reference. c:\users\alex96\documents\visual studio 2013\Projects\Home Work Calendar\Home Work Calendar\StartUp.vb 25 13 Home Work Calendar
and Here is the code I used to open the form (the not working line)
frmCourses.Show()
Normally, this would work, but now it isn't and I don't think I did anything different when creating the other form
Create an instance of the object.
Dim xForm as new frmCourses
xForm.Show

Invalid Cast Exception when filling datatable wth table adaptor

I am using VB.NET 2010 (Visual Basic 2010 Express) on a WPF-based project. I am also using the SQL Server Express built-in to Visual Basic 2010 express.
I have just about finished refining my code for hooking up my wpf-based form to an existing SQL database (agentroster.sdf). I have a global data source (AGENT_ROSTER) connected to this database. Connections are confirmed to work properly.
This is the first part of the code I'm using, irrelevant code omitted,
Dim table_adaptor As New AGENT_ROSTERTableAdaptors.AGENT_ROSTERTableAdaptor
Dim roster_table As New DataTable("roster_table")
Dim rowposition As Integer
Private Sub ROSTER_Loaded...
table_adaptor.Fill(roster_table)
End Sub
I am getting the following errors:
(In Immediate Window)
A first chance exception of type 'System.InvalidCastException' occured in VBP-WORD4WORD.exe
(In Message, pointing to the line: "table_adaptor.Fill(roster_table)')
InvalidCastException was unhandled
Unable to cast object of type 'System.Data.DataTable' to type 'AGENT_ROSTERDataTable'.
What am I doing wrong, and furthermore, how do I fill roster_table with table_adaptor (or alternate method)?
Assuming that your strongly typed DataSet is called "AGENT_ROSTER":
Dim table_adaptor As New AGENT_ROSTERTableAdaptors.AGENT_ROSTERTableAdaptor
Dim roster_table As New AGENT_ROSTER.roster_table
table_adaptor.Fill(roster_table)
Have a look at Efficient Coding With Strongly Typed DataSets.