Excel open workbook fails with COMException 0x800AC472

Stanislav Ilchenko 21 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?

Microsoft 365 and Office | Development | Other
Windows for business | Windows Server | User experience | Other
Developer technologies | .NET | Other
Developer technologies | C#
{count} vote

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.