VB: Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY) - excel

I am working on an app which needs to import data from Excel.
My solution is using Microsoft.office.Interop.Excel.
But I get this error when I debug:
Message=Unable to cast COM object of type
'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type
'Microsoft.Office.Interop.Excel._Application'. This operation failed
because the QueryInterface call on the COM component for the interface
with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the
following error: Error loading type library/DLL. (Exception from
HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY))
Below is my code:
Imports Excel = Microsoft.office.Interop.Excel
Private Sub BExcel1_Click(sender As Object, e As EventArgs) Handles BExcel1.Click
OpenFileDialog1.Filter = "Excel Files|*.xlsx; *.xls; *.xlsm"
If (OpenFileDialog1.ShowDialog() = DialogResult.OK) Then
ExcelPath1.Text = OpenFileDialog1.FileName
End If
Dim XlApp As New Excel.Application
Dim XlWorkBook As Excel.Workbook
Dim XlWorkSheet As Excel.Worksheet
XlWorkBook = XlApp.Workbooks.Open(ExcelPath1.Text)
End Sub
I have googled to find some solutions (as following), but they didn't work out:
I have repaired Office
I have repaired Visual Studio
I have used the registry editor to check the Computer\HKEY_CLASSES_ROOT\TypeLib\, but under the 00020813-0000-0000-C000-000000000046, I got only one version 1.9, it seems not like the conflict between two version?
Any ideas to fix this problem?
VS version:2017 community
Excel version:2016
Microsoft.Office.Interop.Excel version:15.0.0.0

First of all, try to run Visual Studio with the /ResetUserData command line argument. Read more about that in the Error "Unable to cast COM object..." when exporting to Microsoft Excel from Team Explorer 2008 article.
Obviously you are trying to connect to a wrong Excel version. Looks like you have some extra windows registry keys left after uninstalling an old version of Office or vice versa. Anyway, take a look at the How to solve “Unable to cast COM object of type Microsoft.Office.Interop.Excel.ApplicationClass’ to interface type ‘Microsoft.Office.Interop.Excel._Application’” blog post which describes exactly the same issue. Basically you need to find a wrong entry in the windows registry and then delete it.
BTW When you add a new COM reference to the project a missed PIA is generated automatically (if it doesn't exist any longer). So, that is a possible way to go. Also you may try to embed interop types into your own assembly like the following screenshot shows:

If you set the Embed Interop Types property to True (since .NET Framework 4.0) and the Specific Version property to False for the Office Interop assembly reference, then your code should work with any version of Excel.
However, you must compile the code with the same bitness as your Office. For a 32-bit version of Office, you must compile your code as x86, otherwise this exception can still occur on a 64-bit machine!

Related

Trying to get clipboard data

I'm trying to get clipboard data through this lines:
Sub GetClipboard()
Dim objData As New MSForms.DataObject
Dim strText
objData.GetFromClipboard
strText = objData.GetText()
MsgBox strText
End Sub
I get the error:
"User-Defined type not defined"
While "Microsoft Office 16.0 Object Library" is selected in Tools> Reference!
Appreciate any help on this.
It looks like you are missing the correct reference.
In this case for the MSForms namespace you need the Microsoft Forms Object Library (e.g. Microsoft Forms 2.0 Object Library reference).
On my installation of Office 2016 I do not have this library that I could reference. But as stated here (https://wellsr.com/vba/2015/tutorials/vba-copy-to-clipboard-paste-clear/) there are problems since Windows 8/10 anyway. The solution is to use the Windows API directly. Which should work on Windows 7 and newer.
In the first link there is an example only for setting data to the clipboard. You asked for retrieving data so a quick internet search revealed this example code which is described as working on Windows x86 and x86_64 and supports setting and getting data from the clipboard (https://francescofoti.com/2013/12/share-the-clipboard-with-vba-and-the-windows-api/).
I have tested the code from the second link to retrieve a string from the clipboard and it worked. I had to fix a minor thing. The Application object on my Office installation does not have a member called hWndAccessApp. One has to replace it with the default hwnd member:
Application.hWndAccessApp
replaced with:
Application.hwnd
You need below
set objData = New MSForms.DataObject

Workbook Subroutines cannot be called directly by name anymore from VB6

I have a vb6 program that calls Macros from an Excel file.
Until recently it worked fine (for years), but now it throws an error.
We get a runtime 438 "Object doesn't support this property or method" error.
As an example you can use this simple vb6 program:
Set App = CreateObject("Excel.Application")
Set wrkbook = App.Workbooks.Open(fileName)
App.visible = True
wrkbook.Test
The above code doesn't work anymore.
Instead, if we replace the last line with this:
wrkbook.Application.Run "ThisWorkbook.Test"
it works.
The Excel reference used for this, was "Microsoft Excel 16.0 Object Library"
This has been tested against Excel 2010, 2013, Excel365 with the same results.
Also security settings are all set off in Excel.
Before changing my entire codebase and use Application.Run, I would know why this is happening.
Also using Application.Run has also some downsides, like calling both private & public subs and exceptions thrown are not propagated back to vb6.
Are there other considerations for using Application.Run?
Did Microsoft changed something lately (Scurity update), or am I doing something wrong?
I found the problem, thanks to #UuDdLrLrSs comment.
Problem was the use of early binding.
All Excel objects should use late binding.
More specific, the workbook should be declared as object:
Dim wrkbook As Object 'New Excel.Workbook
Otherwise it cannot recognize/find your Macro method.

"User-defined type not defined" error in Excel VBA after upgrade to Windows 10

A co-worker developed a part of Excel VBA-code for me. This code I have integrated in my code.
Both parts of code separate runs fine at Windows 7.
Both parts of code separate runs fine at Windows 10.
Both parts of code integrated runs fine at Windows 7.
Although both parts of code integrated give an error at Windows 10.
The error is a Compile error "User-defined type not defined" directly after opening the file. It is caused by the line "Public objhttp As New XMLHTTP".
Option Explicit
Public objhttp As New XMLHTTP
Public url1 As String
Public src As String
'*******regualr Expression Variables*******
Public regx As New RegExp, matches As Object, match As Object
I have checked all the references under Tools in the Library from VBA.
The installed references are the same in all 4 situations. In all situations I use Excel 2016.
Do you know what can be the cause for this?
Thank you!
I had a very similar problem when Windows 10 rolled out where I work. Something to do with their initial OoTB build of Windows 10 not including a particular msxml dll.
I fixed a few workbooks that this broke by replacing all of these:
MSXML.XMLHTTP
MSXML.ServerXMLHTTP
MSXML.DOMDocument
with these:
MSXML2.XMLHTTP60
MSXML2.ServerXMLHTTP60
MSXML2.DOMDocument60

Compatibility Between MS-Access 2003 and MS-Excel 2013

This is similar to a question that I've asked yesterday, but I've managed to find a fair bit more information: enough to warrant a new question, I feel.
My office has recently moved to Windows 7 along with Office 2013 - with the except of Access which has stayed as 2003.
Access works absolutely fine except when I try to automate Excel. This is a feature of nearly all my applications.
When I load an old project, the reference is automatically updated to "Microsoft Excel 15.0 Object Library" - it is held as an EXE file in Program Files (x86). Note: it does not say the reference is missing.
When I try to run any code which uses early binding, it fails with the following error message: "Error in loading DLL"
I've since tried late binding by doing the following:
Dim app As Object
Dim wb As Object
Set app = CreateObject("Excel.Application")
app.Visible = True
Set wb = app.Workbooks.Add
This creates the Application, it becomes visible, a Workbook is added, but then I get the following error: "Run-time error '1004':
Application-definted or object-defined error"
If I try to access VBA in the new Workbook I get the following error: "An error occurred initializing the VBA libraries (1004)" The Workbook is loaded in "Compatibility Mode"
The above leads me to believe that the 2 applications may not be compatable, but the following is worth considering:
If I try to use early binding I don't get the Intellisense drop-downs but words like Workbook and Worksheet do auto-capitalise themselves as you would normally expect them to. The code won't compile at all if I don't have the reference set, but it does compile - and throw and error - when it is. I've since tried Word 2013 - who's Object Reference is an OLB file rather than EXE - and I get the full Intellisense features I would except, but it still throws the same error when trying to run.
In a nut shell, does anyone know if I stand any chance of having the 2 work together?
Thanks

Error Code 429: Active X Component Cannot Create Object

I have a .Net Class Library which I refer in VBA and all goes well. When I try to use the same Excel file on some other machine the error is: Error Code 429:Active X component Cannot create Object
The error is recd for line
set a = createobject("dllname.classname")
However I am registering the DLL at runtime and it gives no error for the line
Dim a as dllname.classname
This error happens on my Win-7 pc where as works just fine on Win-XP PC. Please help because I need to run my application using the Excel Sheet.
This article has a good writeup on ensuring your assembly is late-bindable, i.e. via createobject().
Takeaways:
Make sure your assembly includes a ProgID attribute
Use regasm on the target (deployment) machine to do the actual registration of the COM class

Resources