I'm trying to resolve the following Remote Procedure Call (RPC) error that occurs when an unattended Windows Service application attempts to create a new Excel file via Interop.
Exception: System.Runtime.InteropServices.COMException (0x800706BE): The remote procedure call failed. (Exception from HRESULT: 0x800706BE)
at Microsoft.Office.Interop.Excel.Workbooks.Add(Object Template)
Here's the code throwing the exception. For context, I included a few lines before and after the culprit code.
' Create Excel objects
Dim objExcelApp As New Excel.Application
Dim objExcelBooks As Excel.Workbooks = objExcelApp.Workbooks 'This is the specific line it fails on every time.
Dim objExcelBook As Excel.Workbook = objExcelBooks.Add
Dim objExcelSheet As Excel.Worksheet = objExcelApp.ActiveSheet
Although this code worked without any issues for the past 7 years, this error started happening immediately upon migrating the service from Windows Server 2008 R2 to Windows Server 2016. Through much trial, error, and hair pulling, I finally discovered that this error is only thrown when someone is connected to the server via Remote Desktop. If no one is connected via Remote Desktop, everything works flawlessly.
What I can't figure out is why does this error only occur when someone is connected to the server via Remote Desktop?
Here are a few things I've tried so far.
Inspired by Caius Jard's suggestions, I eventually settled on ClosedXML. It's fast, lightweight, free to use for commercial purposes, and doesn't rely on the installation of Excel or the Excel.Interop library to function. The best part is not only did it resolve my issue, fewer lines of code are required to accomplish the same things.
User contributions licensed under CC BY-SA 3.0