Could not load file or assembly 'Office, Version=15.0.0.0'
Asked Answered
H

7

52

I use Vs2013. I have created application in which I use Excel file as an input and get contact from the file. Everything is working in my computer. I have Vs2013. Windows 8.1, Ms office 2007 & 2013.
When I run my application in any other computer, it throws

Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bc111e9429c' or one of its dependencies. The system cannot find the file specified

As per my application requirement, I need to use Excel file from Office 2007 to 2013.

I have referred few StackOverflow links but I am not getting result. I am stuck. Please suggest me how to solve this.

Hahn answered 4/9, 2015 at 13:32 Comment(7)
You targeted the wrong version of Office if you want to support 2007. Not targeting at least .NET 4.0 is also rather a mistake, the Embed Interop Types feature is very desirable. Anyhoo, the way you are doing it now you'll have to install the Office 2007 Primary Interop Assemblies both on your machine and on the target machine.Magdalen
Do you manipulate your excel file or only read data out of it?Contrivance
If i change Excel 12.0.0.0, will it work for all Ms office 2007 to 2013 version?Hahn
1- I get excel file from local drive, convert it in CSV and write it into database. 2- I write create excel file and write contacts of database in it. @ContrivanceHahn
If you only read and write data and don't do any "excel functionality" you could just connect to excel using Microsoft.ACE.OleDb, and work with excel file just like with DB table. Regardless, you seem missing Excel.dllContrivance
Can you please suggest me link which contain example of this? @ContrivanceHahn
codeproject.com/Tips/705470/…Contrivance
B
23

Your other machine needs to have the corresponding version of Office installed. 15.0.0.0 should correspond to Office 2013 - that needs to be installed on your target machine (other versions of Office may not work). This almost certainly means you're using MSOffice interop libraries, which only work if office is installed and against the same version.

Alternatively, you could refactor your code to just directly read the Excel XML.

Bienne answered 4/9, 2015 at 13:41 Comment(4)
I have tried in 2 computer. It is working fine in the computer having Ms office 2013. But It is not working for other computer having Ms office 2007. As application is on the edge, I can not change mechanism. I must find solution for this. Can you please advice me in this situation? @Dan FieldHahn
Right, it requires office 2013. If you want it to be compatible wit Office 2007, you have to make sure to build against those libraries (nto version 15.0.0.0)Bienne
Thanks. I have change Version to 12.0.0.0 and it is working fine now.Hahn
I get this error even though I have Office 2013 installed and created the two documents in question with Office 2013 on the same box. Using the latest 15.0.4797.1003.Toggery
B
48

I also got this error message even if I have Office 2010 and I don't have Microsoft.Office.Interop.Excel folder under GAC.
I found the solution for my case here: https://www.add-in-express.com/forum/read.php?FID=5&TID=15525
You have to reference two dll-files from these folders:
C:\Windows\assembly\GAC_MSIL\Microsoft.Vbe.Interop\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll
and
C:\Windows\assembly\GAC_MSIL\office\15.0.0.0__71e9bce111e9429c\OFFICE.DLL

Brakesman answered 10/1, 2021 at 22:13 Comment(4)
whoo! finally. thanks for this. It works on my end.Attractant
So annoying. Thanks @BrakesmanHoarsen
great! adding "OFFICE.DLL" reference resolved issue for me.Bon
why do I have to manually reference dlls from GAC_MSIL ? Do dlls from GAC_MSIL not be auto reference ?Delrio
B
23

Your other machine needs to have the corresponding version of Office installed. 15.0.0.0 should correspond to Office 2013 - that needs to be installed on your target machine (other versions of Office may not work). This almost certainly means you're using MSOffice interop libraries, which only work if office is installed and against the same version.

Alternatively, you could refactor your code to just directly read the Excel XML.

Bienne answered 4/9, 2015 at 13:41 Comment(4)
I have tried in 2 computer. It is working fine in the computer having Ms office 2013. But It is not working for other computer having Ms office 2007. As application is on the edge, I can not change mechanism. I must find solution for this. Can you please advice me in this situation? @Dan FieldHahn
Right, it requires office 2013. If you want it to be compatible wit Office 2007, you have to make sure to build against those libraries (nto version 15.0.0.0)Bienne
Thanks. I have change Version to 12.0.0.0 and it is working fine now.Hahn
I get this error even though I have Office 2013 installed and created the two documents in question with Office 2013 on the same box. Using the latest 15.0.4797.1003.Toggery
S
15

Use the COMRreference property. For example, here is the COMReference for Excel:

<ItemGroup>
    <COMReference Include="Microsoft.Office.Interop.Excel">
        <EmbedInteropTypes>true</EmbedInteropTypes>
        <Guid>00020813-0000-0000-c000-000000000046</Guid>
        <Isolated>false</Isolated>
        <Lcid>0</Lcid>
        <WrapperTool>primary</WrapperTool>
        <VersionMajor>1</VersionMajor>
        <VersionMinor>9</VersionMinor>
    </COMReference>
</ItemGroup>

A little help on the exact sub-properties from this discussion: Incorrect COMReference entry when using Office interop #5735

Spherule answered 10/1, 2023 at 14:23 Comment(1)
This worked in a .NET 6 Winform app for which I was using the Nuget package. When I uninstalled the Nuget package, it worked.Lightship
H
9

I got solution by changing Excel.dll version. I was using 15.0.0.0 and now I changed it to 12.0.0.0 and its working fine. I got dll from Add reference > Browse > C: > Windows > assembly > GAC > Microsoft.Office.Interop.Excel > 12.0.0.0_etc > Microsoft.Office.Interop.Excel.dll

Hahn answered 7/9, 2015 at 7:53 Comment(2)
I can't find Microsoft.Office.Interop.Excel inside GAC folder. What do I miss ?Eveleen
@TranB.V.Son mine was in GAC_MSILTry
A
4

I created a batch file to fix this issue. See script below:

    echo off
        cls
        color 1f
        echo Checking for Administrator elevation.
        openfiles>nul 2>&1

            if %errorlevel% EQU 0 goto isadmin

                COLOR 4f
            echo.    You are not running as Administrator.
            echo.    This tool cannot do it's job without elevation.
            echo.
            echo.    You need run this tool as Administrator.
            echo.

            echo.Press any key to continue . . .
            pause>nul
        exit
        :isadmin
        if exist c:\windows\assembly\GAC_MSIL\office\16.0.0.0__71e9bce111e9429c\OFFICE.DLL set officever=16
    if exist c:\windows\assembly\GAC_MSIL\office\15.0.0.0__71e9bce111e9429c\OFFICE.DLL set officever=15
    if exist c:\windows\assembly\GAC_MSIL\office\14.0.0.0__71e9bce111e9429c\OFFICE.DLL set officever=14

    md c:\windows\assembly\GAC_MSIL\office\12.0.0.0__71e9bce111e9429c
    xcopy c:\windows\assembly\GAC_MSIL\office\%officever%.0.0.0__71e9bce111e9429c c:\windows\assembly\GAC_MSIL\office\12.0.0.0__71e9bce111e9429c /s/y
pause
Alagez answered 4/1, 2018 at 14:29 Comment(2)
Please provide an explanation for your solution. We are not a code writing service, please share your knowledge and describe how you created that solution. Thank you very much.Lecturer
This looks horrible, you are copying a later version of the .dll to a filename which pretends it's the old version. This is going to cause all kinds of hard-to-debug problems.Rove
P
3

I have found that if you are using a newer .NET version, like 6.0+, then you may see this error, if you have this reference:

<PackageReference Include="Microsoft.Office.Interop.Excel" Version="15.0.4795.1001" />

And adding this reference is one way to fix it:

<PackageReference Include="MicrosoftOfficeCore" Version="15.0.0" NoWarn="NU1701" />

Using a COMReference with EmbedInteropTypes can also fix it, but that requires Excel to be installed on the machine that's compiling the project.

Polloch answered 24/4 at 20:53 Comment(1)
Worked for me on .NET 8 and Office365. Thank youTumescent
S
1

I had the same issue you should include the following package (Syncfusion.XlsIO) instead of (Microsoft.Office.interop.Excel) cuz it only support excel 2013, but the first one 'Syncfusion.XlsIO' does support Excel 2016;

=> using Syncfusion.XlsIO;

Here is the following code :

  using (ExcelEngine excelEngine = new ExcelEngine())
                {
                        IApplication application = excelEngine.Excel;
                        application.DefaultVersion = ExcelVersion.Excel2016;
                        IWorkbook workbook = application.Workbooks.Create(1);
                        IWorksheet worksheet = workbook.Worksheets[0];
                        //Adding text to a cell
                        for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
                        {
                           worksheet.Range[1, i].Text = dataGridView1.Columns[i - 1].HeaderText;
                        }

                        for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
                        {
                            for (int j = 0; j < dataGridView1.Columns.Count; j++)
                            {
                                worksheet.Range[i + 2, j + 1].Text = dataGridView1.Rows[i].Cells[j].Value.ToString();
                            }
                        }
                        //Saving the workbook to disk in XLSX format
                        Stream excelstream = File.Create(Path.GetFullPath(@"MyExcelFile.xlsx"));
                        workbook.SaveAs(excelstream);
                        excelstream.Dispose();
                }
Stinson answered 5/4, 2021 at 12:44 Comment(2)
isnt Syncfusion.XlsIO a Paid nuget package.?Ariadne
@arihanthjain yes, I tried this example, in the final step showed a message about pay. So sadHaldeman

© 2022 - 2024 — McMap. All rights reserved.