0

Hey all I have experiencing an issue with getting a COM office Excel call to work out.

Here is my code that is pooping:

public async Task<bool> updateExcelSheets(string excelName, DateTime newDate, string excelPath)
{
   //Loop through all the data columns and update them
   int intX = 1;
   bool skipParseCol = true;
   Dictionary<string, string> resultsBack = new Dictionary<string, string>();
   Dictionary<string, string> _tmpParms = new Dictionary<string, string>();
   HELPer SQLHelper = new HELPer();
   DataSet allData = new DataSet();

   try
   {
       MSOIE.Application xlsApp = new MSOIE.Application();
       //                                          [FileName]
       //                                          |        [UpdateLinks]
       //                                          |        |    [ReadOnly]
       //                                          |        |    |    [Format]
       //                                          |        |    |    |     [Blanks]
       //                                          |        |    |    |    / \    [Ignore Read Only Recommended]
       //                                          |        |    |    |   |   |   |
       //                                          ▼        ▼    ▼    ▼   ▼   ▼   ▼
       MSOIE.Workbook wb = xlsApp.Workbooks.Open(excelPath, 0, false, 5, "", "", true);
       MSOIE.Sheets sheets = wb.Worksheets;
       MSOIE.Worksheet ws = (MSOIE.Worksheet)sheets.get_Item(2); //Selecting DDValues tab

       xlsApp.Visible = true;
       //More code here........
   }
   catch (Exception e)
   {
       Console.WriteLine("{0} Exception caught.", e.Message);
   }

   return Task.FromResult(true).IsCompleted;
}

Once the code reaches the MSOIE.Application xlsApp = new MSOIE.Application(); part it goes to the exception and the e.Message says:

Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

I've search a lot to see others fixes but non seem to work for me. I've tried:

1.In DCOMCNFG, right click on the My Computer and select properties.     
2.Choose the COM Securities tab.    
3.In Access Permissions, click Edit Defaults and add Network Service to it and give it Allow local access permission. Do the same for < Machine_name >\Users.    
4.In Launch and Activation Permissions, click Edit Defaults and add Network Service to it and give it Local launch and Local Activation permission. Do the same for < Machine_name >\Users.

Credit: HERE

1.From the Start menu, click Run and type Dcomcnfg.exe.
2.In Component Services, click Console root, expand Component Services, expand Computers, expand My computer,expand DCOMConfig.
3.Search for Microsoft EXCEL 15.0 Object Library. Click on it.
4.Right click and select Properties.
5.On security tab, select Customize in "Launch and Activation" section.
6.Click edit and add identity of app pool under which you application is running. 
7.Repeat above step for "Access Permission"

Credit: HERE

Go to your IIS Manager. → ApplicationPools → AdvancedSettings → Enable32BitApplication → True.

Credit: HERE

Now the odd thing is that I just did a normal winform and had the same code as in my ASP.net MVC project and it works just fine once it gets to that MSOIE.Application xlsApp = new MSOIE.Application(); line of code so i'm not really able to figure out what the difference is between the 2 that would cause one to work and the other (with the same code) not....

Any help would be great! Thanks!

StealthRT
  • 10,108
  • 40
  • 183
  • 342

0 Answers0