Using VS2022 and Net 8 to make a console app referencing excel interop RCW.
On my development machine this interop file is version 16 (MS 365) and works just fine, however on another machine with same OS, same dotnet runtime (runtime shouldn't matter as long as I publish self-contained), but Office 2010 (which is version 14?) it does not work.
I am pretty sure this has to do with the office version on the target machine, but I don't know how to develop against an older interop.
Note that this sample case (real app is way more complex) is a console app and VSTO is not relevant.
Anyone knows the right way to go? (and of course it must work on machine with office 2013, 2016,2019 and whatever newer including the 365)
My little program is this simple:
using Microsoft.Office.Interop.Excel;
using System;
using System.IO;
Application excelApp = new();
string path = Directory.GetCurrentDirectory();
Workbook workbook = excelApp.Workbooks.Open(Path.Combine(path, "Testbook.xlsx"));
foreach (Worksheet ws in workbook.Worksheets)
{
Console.WriteLine(ws.Name);
}
Error is invalid cast exception on line 4 (new'ing excelApp)