Excel open workbook fails with COMException 0x800AC472

Stanislav Ilchenko 16 Reputation points
2023-11-15T14:43:18.9266667+00:00

Hello. We have an automation .NET app service running on IIS and using Excel COM Interop. It has been working for several years. After moving to another server, it doesn't work. It fails just on Microsoft.Office.Interop.Excel.Workbooks.Open with COMException 0x800AC472.

What Excel workarounds has been done and it still doesn't work:

  • Configure access rights in "Console Root\Component Services\Computers\My Computer\DCOM Config\Microsoft Excel Application"
  • Create "C:\Windows\System32\config\systemprofile\Desktop" directory and give user access to it
  • Ensure there is no pop up messages. When I open the same file in UI - there is no any pop and no any ComException.
  • Microsoft.Office.Interop.Excel.Application.DisplayAlerts is set to false
  • Microsoft.Office.Interop.Excel.Application.Interactive is set false
  • Loop with Thread.Sleep and retry open document
  • Loop with restart Microsoft.Office.Interop.Excel.Application
  • Ensure that no other instances of Excel is running
  • Ensure that no dialog window is displaying by Excel on opening that file
  • Reinstall Office
  • Use another Excel libraries such as OpenXml, ClosedXml, Aspose etc., only Microsoft Office can handle our document structure
public class COMDocumentFactory : IDocumentFactory
    {
        private readonly string _tempFolder;
        private Application _app;

        public COMDocumentFactory(string tempFolder)
        {
            InitializeCom();

            var foldersToCreate = new Stack<DirectoryInfo>();
            var dirInfo = new DirectoryInfo(tempFolder);
            _tempFolder = dirInfo.FullName;
            while (dirInfo != null && !dirInfo.Exists)
            { 
                foldersToCreate.Push(dirInfo);
                dirInfo = dirInfo.Parent;
            }

            foreach(var folder in foldersToCreate)
            {
                folder.Create();
            }
        }

        private void InitializeCom()
        {
            _app = new Application();
            _app.Interactive = false;
            _app.DisplayAlerts = false;
            _app.Visible = true;
        }

        public Workbook ReadDocument(Stream stream)
        {
            var tempFile = "C:\\TempDirectory\\" + Guid.NewGuid().ToString("N") + ".xlsx";
            using(var fileStream = File.Open(tempFile.FileName, FileMode.Create))
            {
                stream.CopyTo(fileStream);
                fileStream.Flush();
                fileStream.Close();
            }

            int retries = 3;
            while (retries > 0)
            {
                try
                {
                    //this line fails
                    var workbook = _app.Workbooks.Open(tempFile.FileName);
                    return workbook;
                }
                catch (COMException comException)
                {
                    if (retries > 1)
                    {
                        Thread.Sleep(500);
                        retries--;
                        continue;
                    }
                    throw;
                }
            }
            var lastTryWorkbook = _app.Workbooks.Open(tempFile.FileName);
            return new COMExcelDocument(tempFile, lastTryWorkbook, _tempFolder);
        }

        public void Reinitialize()
        {
            _app?.Quit();
            InitializeCom();
        }

    }

What another configuration I could forget or skip to make it work?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,400 questions
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,179 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,289 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,517 questions
{count} votes