One way around this may be to use Open XML if working with .xlsx version of Excel. An easy way is with SpreadSheetLight which is free, downside all code samples are in C#.
I have some simple examples for VB in the following repository
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Original question posted to Office 365 forum.
I wanted to post that every time there is a Cumulative Update to Windows 10, I get a VS2019 program error when it attempts to programmatically open an Excel spreadsheet. I have to subsequently go into programs and Repair my installation of Microsoft 365. After doing the repair, the program runs normally.
Here is the statement it fails on.
Dim excelApp As Excel.Application = New Excel.Application With {
.Visible = True
}
Response from Office 365 forum.
Thank you for posting back and sharing the detailed information with us.
Considering you can directly open the Excel spreadsheet without any issue and the error message only appears when you use Visual Studio 2019 to open the spreadsheet, I'm afraid this issue may need to be investigated from Visual Studio's side.
In this situation, to make sure you get the dedicated assistance, I sincerely recommend you post the question in Q&A - Visual Studio Community, it is the specific channel handling this kind of issues, engineers and experts can help you check the code and assist you further.
In the meantime, I also tried to search for the error code you shared but cannot find similar threads which have same environment as yours. So I still recommend you ask the question in Q&A - Visual Studio Community, members will share specific suggestions based on the specific scenario on your side. I sincerely hope your question can be addressed by experienced assistance.
Your patience and understanding will be highly appreciated.
Have a nice day and stay safe.
One way around this may be to use Open XML if working with .xlsx version of Excel. An easy way is with SpreadSheetLight which is free, downside all code samples are in C#.
I have some simple examples for VB in the following repository
The whole idea is to use Visual Studio reference libraries to automate Outlook, Excel and Access operation in .NET code. For example, I can create Outlook appointments from a .NET program as well as open Excel documents that are listed in a database. Why would I want to use Open XML when I have .NET?
I'm not aware of any issues with VS and Office and it partially doesn't make any sense. I've not had any issues but to be honest the only time I use Office interop is for answering questions related to them. Nevertheless I've never seen a Windows update cause problems with Office interop.
Your app is using a PIA that literally just calls COM code. The COM code is the same thing that the Office apps use. Even if the Windows update breaks these objects then Office would break as well. Given that repairing Office solves the problem it sounds like your Office installation has an issue and not VS or Windows. I'm curious what error you're actually getting though. If it is a registration error then that leads me down the path to believing your Office installation is bad.
I might recommend that you run the sfc /scannow
command to see if there is something wrong with system files. While this has probably never solved my problems it is generally the recommended approach when you are having problems like this.
The last time I had this problem I had done a re-install. I'm getting indications that this is a registry problem.
I realized that I retained the error message.
System.InvalidCastException: '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: Element not found. (Exception from HRESULT: 0x8002802B (TYPE_E_ELEMENTNOTFOUND)).'