2

I am trying to read an .xls file that is downloaded from a 3rd party site. I will not be processing the file everyday, so I can't change the format to .xlsx before uploading the file.

I keep getting the following error: 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 am using a vb.net site with IIS7. The Application Pool logs in with my user and my user is an Administrator on the server with access to everything.

I have done the following:

  • Office is installed on the HOST machine
  • I have gone into DCOMCNFG and made sure that the Launch and Activation Permissions, Access Permissions and Configuration Permissions have full control with my login account.
  • The server has x64 architecture, and I also have Office x64 installed
  • I've confirmed that the folder C:\Windows\SysWOW64\config\systemprofile\Desktop as well as C:\Windows\SysWOW64\config\systemprofile\Desktop does exist.
  • I know that C:\Windows\SysWOW64\config\systemprofile\Desktop is for x86 architecture but I made sure it was there just in case the system wanted to use it.

This is what my code looks like:

Imports genlib

Imports Excel = Microsoft.Office.Interop.Excel

Partial Class HeadOffice_OpperationalParameters_FailedBranches
    Inherits System.Web.UI.Page

    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub

    Protected Sub btnRunReport_Click(sender As Object, e As EventArgs) Handles btnRunReport.Click
        If fuReport.HasFile = False Then
            ClientScript.RegisterClientScriptBlock(Me.GetType, "", "alert('Upload a file to process.');", True)
            Exit Sub
        End If

        Dim ReportFileName As String = Server.MapPath("~") & "FilesUploaded\" & fuReport.FileName.Replace(".XLS", "_" & Today.ToString("yyyy.MM.dd") & "_" & TimeOfDay.ToString("HH.mm.ss") & ".xls")
        fuReport.SaveAs(ReportFileName)

        Dim xlApp As Excel.Application 'Here
        Dim xlWorkbook As Excel.Workbook
        Dim xlWorksheet As Excel.Worksheet

        Try
        '=================================================
        ' ERROR OCCURS ON NEXT LINE
        '=================================================
            xlApp = New Excel.ApplicationClass
            xlWorkbook = xlApp.Workbooks.Open(ReportFileName)
            xlWorksheet = xlWorkbook.Worksheets("RptTransactionDetail")

            BrowserWrite(xlWorksheet.Cells(2, 2).value, True)
            xlWorkbook.Close()
            xlApp.Quit()
        Catch ex As Exception
            ClientScript.RegisterClientScriptBlock(Me.GetType, "", "alert('" & ex.Message.Replace("'", "\'").Replace(vbCr, "\r").Replace(vbLf, "\n") & "');", True)
            Exit Sub
        End Try

        releaseObject(xlApp)
        releaseObject(xlWorkbook)
        releaseObject(xlWorksheet)

    End Sub
End Class

Can anyone tell me how to resolve this error, or what steps to take to identify the issue?

I have read most of the posts on stack overflow but there are too many to add each answer that I have looked at.

dbasnett
  • 11,334
  • 2
  • 25
  • 33
Jacques Koekemoer
  • 1,378
  • 5
  • 25
  • 50
  • What line does the error occur? – dbasnett May 05 '17 at 10:24
  • @dbasnett on line 33: xlApp = New Excel.ApplicationClass – Jacques Koekemoer May 05 '17 at 10:27
  • Have you tried xlApp = New Excel.Application – dbasnett May 05 '17 at 10:30
  • @dbasnett I just tried that code, its still giving me the same error: `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)).` – Jacques Koekemoer May 05 '17 at 10:50
  • Microsoft does not recommend using Interop on web servers. I suspect you are running into one of those issues. See https://support.microsoft.com/en-us/help/257757/considerations-for-server-side-automation-of-office – dbasnett May 05 '17 at 11:17
  • Maybe ProcMon can give you a hint, which file/registry key IIS is trying to read and fail. – dummy May 05 '17 at 13:35
  • Although Excel may not work in the environment you want, you could try using the code in the answer at [What name space to use in VB.net](http://stackoverflow.com/a/41634089/1115360) to give your code a fair chance of working. – Andrew Morton May 09 '17 at 13:10

1 Answers1

1

I am using a vb.net site with IIS7

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

As a possible workaround you may consider using the Open XML SDK if you deal with open XML documents only, see Welcome to the Open XML SDK 2.5 for Office. Or just any third-party component designed for the server-side execution (for example, Aspose).

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45