3

I'm using Microsoft.Office.Interop.Excel to convert excel to pdf. But when I start Excel application, this error happened. I already installed Excel 2013 on my computer. (I'm using VS2019, Window 10).

My Excel's location is at C\Program Files (x86)\Microsoft Office\Office 15\Excel.

Could not load file or assembly ‘office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxx’. The system cannot find the file specified

Any suggestions are welcomed!

This is my code:

using Microsoft.Office.Interop.Excel;
using System;
using System.Runtime.InteropServices;

namespace ExcelToPdf
{
    public class ExcelApplicationWrapper : IDisposable
    {
        public Application ExcelApplication { get; }

        public ExcelApplicationWrapper()
        {
            ExcelApplication = new Application(); // start excel application
        }

        public void Dispose()
        {
            // Each file I open is locked by the background EXCEL.exe until it is quitted
            ExcelApplication.Quit();
            Marshal.ReleaseComObject(ExcelApplication);
        }
    }
}
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;

namespace ExcelToPdf
{
    public class ExcelInteropExcelToPdfConverter
    {
        public void ConvertToPdf(IEnumerable<string> excelFilesPathToConvert)
        {
            using (var excelApplication = new ExcelApplicationWrapper()) // got error here
            {

            }
        }
    }
}
Tran B. V. Son
  • 759
  • 2
  • 12
  • 31
  • _[There is zero need to call `Marshal.ReleaseComObject`](https://stackoverflow.com/a/25135685/585968)_ –  Nov 27 '19 at 09:48
  • 1
    I don't think the problem comes from `Dispose()` function. I just only call `ExcelApplication = new Application(); ` – Tran B. V. Son Nov 27 '19 at 09:50
  • The Interop version installed has to match the version of excel that is being used. Have you tried opening excel 2013 at least once before running application. First time you open excel properties are set up in windows. The Interop is installed in the c:\Program File folder and version 15 (excel 2013) interop is not being found. – jdweng Nov 27 '19 at 10:02
  • @jdweng I do not fully understand what you said. But excel 2013 on my computer is working good. And excel's location is at `C\Program Files (x86)\Microsoft Office\Office 15\Excel` – Tran B. V. Son Nov 27 '19 at 11:09
  • Where is the Interop (not excel)? The application is looking for the Interop. The error is indicating either 1) The interop is not found 2) the permissions are wrong so app cannot read the interop folder 3) the interop wasn't registered properly so the PublicKeyToken is bad. – jdweng Nov 27 '19 at 11:27
  • https://stackoverflow.com/a/21018418/17034 – Hans Passant Nov 27 '19 at 11:38
  • @TranB.V.Son Can you please clone this repository, https://github.com/FrancescoBonizzi/Xls2Pdf, build and run it, and tell me if you have the same problem? – Francesco Bonizzi Nov 27 '19 at 13:27
  • Usually, this error is because the set of PIAs referenced in the project does not match the PIAs available on the machine. By default Visual Studio will install the set of PIAs for the current version of Office, which in this case is probably 2016/365. The project should point to the PIAs in the GAC, instead, which are installed by Office. See [Could not load file or assembly 'Interop.Microsoft.Office.Core, Version=2.4.0.0, ...' when changing from .NET 3.5 to 4.5.1](https://stackoverflow.com/questions/28349576/could-not-load-file-or-assembly-interop-microsoft-office-core-version-2-4-0-0) – Cindy Meister Nov 27 '19 at 17:11
  • See also the link Hans Passant provided, which has the same information. – Cindy Meister Nov 27 '19 at 17:15

1 Answers1

6

here is the answer https://github.com/dotnet/project-system/issues/5735

in order to fix the situation, the project system needs to add True.

Old:

<ItemGroup>
    <COMReference Include="Microsoft.Office.Excel.dll">
        <Guid>00020813-0000-0000-c000-000000000046</Guid>
        <VersionMajor>1</VersionMajor>
        <VersionMinor>7</VersionMinor>
        <WrapperTool>tlbimp</WrapperTool>
        <Lcid>0</Lcid>
        <Isolated>false</Isolated>
    </COMReference>
</ItemGroup>

New:

<ItemGroup>
    <COMReference Include="Microsoft.Office.Interop.Excel">
        <Guid>{00020813-0000-0000-C000-000000000046}</Guid>
        <VersionMajor>1</VersionMajor>
        <VersionMinor>7</VersionMinor>
        <Lcid>0</Lcid>
        <WrapperTool>primary</WrapperTool>
        <Isolated>False</Isolated>
        <EmbedInteropTypes>True</EmbedInteropTypes>
    </COMReference>
</ItemGroup>
fcdt
  • 2,371
  • 5
  • 14
  • 26
CHIPPY
  • 97
  • 1
  • 7