none
Excel 2010 - C# - Sometimes I get an error 0x800706BA RRS feed

  • Question

  • Hi,

    I have an application that generates Excel workbooks, sometimes I have to merge several workbooks in 1 workbook.

    It was working fine with previous version of Excel (2003, 2007), but since I'm trying to make it work with Excel 2010, I'm experiencing problems (protected view when I use temporary files, bug when you merge worksheet with images and close the source workbook prior saving the destination one).

    Now, I have an unpredictable problem, this error occurs: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

    I don't understand why this error happens. Sometimes, I can run the same merge operations 10 times without an error and sometimes the first time I start the merge operations, this exception is raised.

    Does anybody have an idea of what this error mean (searching the web doesn't helped)? Why this error occurs?

    Thanks,

    Julien


    Best regards, Julien
    Monday, October 18, 2010 3:45 PM

All replies

  • If you're reading from or writing to files on a remote computer and your network is unreliable, you can get this error.

    Alternatively, Excel may be trying to access one of the workbooks after it was closed, or you're using an unqualified reference to a global variable that changes over time. For example,

    Range("A1").Value

    Refers to cell A1 in the active sheet. If the active sheet changes without you knowing, you'll be referencing a cell in another worksheet. If the active sheet closes and there is no active sheet, the call will fail. See if this helps:

    http://support.microsoft.com/default.aspx?scid=kb;en-us;q293092

     

    Which line is causing the error?

     

     

    Monday, October 18, 2010 4:07 PM
  • Hi Elliot,

    Thank you for your answer.


    What do you mean exactly by remote computer? Does mapped network drives are in this category, or are you talking about another thing (using Excel on a remote computer for example, I don't know)?

    Because I'm writing files on a network drive...
    If it is a known scenario, how can I test that the network is available? can I wait network availability easily (with timeout and error if network is really lost)? How can I manage this kind of error (is it possible to close Excel properly, ...)? Actually, when these errors occurs, there is Excel process in the background. Even if I show Excel, they appears frozen...

    Here, I describe the operations I'm doing:

    0. Starting Excel
    1. I Open the destination workbook
    2. Start a loop on source workbooks
    2.1. I Open the source workbook
    2.2. I copy the sheets from source workbook to destination workbook
    2.3. I Save the destination workbook (to avoid the Excel 2010 bug that occurs if source contains images...)
    2.4. I Close the source workbook
    3. If there is still workbook to merge, go back to 2
    4. Save the destination workbook (it's saved twice)
    5. Close the application

    All of this can be in a main loop over different destination files (and their own source files). It means that Excel can start/stop many times...

    The error 0x800706BA can occurs at step 2.3, 4 and 5 (perhaps at other steps, seems unpredictable).

    Below, is the code used to merge worksheets:

    public void AddSheetsFromFile(string fileName) {
    	if (_ApplicationInstance == null)
    		throw new MyExcelException(ExceptionMessages.NoExcelInstanceStarted);
    	if (_CurrentWorkbook == null)
    		throw new MyExcelException(ExceptionMessages.NoDocumentOpened);
    	Excel.Workbook source = _ApplicationInstance.Workbooks.Open(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    	try {
    		source.Worksheets.Copy(Type.Missing, _CurrentWorkbook.Sheets[_CurrentWorkbook.Sheets.Count]);
    		_CurrentWorkbook.Save();
    	}
    	finally {
    		source.Close(false, Type.Missing, Type.Missing);
    	}
    }
    
    

    At step 2.2, I also noticed the following error (as the previous one, it doesn't occur every time...):  System.Runtime.InteropServices.COMException (0x80010105): Le serveur a généré une exception. (Exception de HRESULT : 0x80010105 (RPC_E_SERVERFAULT))

    source.Worksheets.Copy(Type.Missing, _CurrentWorkbook.Sheets[_CurrentWorkbook.Sheets.Count]);

    _CurrentWorkbook is a reference to the destination workbook opened at Step 1. If it was a reference to an already closed workbook, the error should happen every time I run the same operations. Do you think it is better to reference the destination workbook using another approach?

    Thanks for your help,
    Julien


    Best regards, Julien
    Tuesday, October 19, 2010 10:31 AM
  • I've made more test and even locally I have similar problems...


    Best regards, Julien
    Tuesday, October 19, 2010 11:28 AM
  • Hello Julien,

    From your latest post, you said this issue also happens when source workbooks and destination workbook are in local path. I think it may be because that these com objects are not probably released. Please try to use Marshal.ReleaseComObject or garbage collector (GC). For more information, please refer to this thread: VSTO and Office objects disposing.

    Also here is a thread which also might help you: http://social.msdn.microsoft.com/Forums/en-US/exceldev/thread/b97efd47-ea9c-4835-8d0c-6a5682498eae.

    If you have any concern on this post, just feel free to follow up. Have a nice day.

    Best regards,
    Bessie Zhao - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, October 20, 2010 3:25 AM
  • Hi Bessie,

    Thank you for your feedback. I've a wrapping class to encapsulate Excel interop and I'm not sure where I have to use these approaches (the examples are really simple scenarii...).

    My class keep a reference to Excel application instance and to the main workbook (the destination workbook in the scenario described in the first post).

    In a method like this one:

    public void AddSheetsFromFile(string fileName) {
    	if (_ApplicationInstance == null)
    		throw new MyExcelException(ExceptionMessages.NoExcelInstanceStarted);
    	if (_CurrentWorkbook == null)
    		throw new MyExcelException(ExceptionMessages.NoDocumentOpened);
    	Excel.Workbook source = _ApplicationInstance.Workbooks.Open(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    	try {
    		source.Worksheets.Copy(Type.Missing, _CurrentWorkbook.Sheets[_CurrentWorkbook.Sheets.Count]);
    		_CurrentWorkbook.Save();
    	}
    	finally {
    		source.Close(false, Type.Missing, Type.Missing);
    	}
    }
    
    
    

    Do I have to use the Marshal.ReleaseComObject() on the source workbook object?

    My class is marked as IDisposable, can I use Marshal.ReleaseComObject() in the Dispose method? GC.Collect / WaitForPendingFinalizers?

    Thanks again,

    Julien


    Best regards, Julien
    Wednesday, October 20, 2010 8:18 AM
  • Hi again Bessie,

    I've built a test application (command line) that takes the files to merge as arguments: TestApplication "Destination_1" "Source_1" "Destination_2" "Source_2" ...

    Where Destination and Source are excel files.

    Here is the code, I've tried to free all references using different approaches... but it still doesn't work properly, I think there is something wrong, but I don't know what.

    using System.Threading;
    using Microsoft.Office.Core;
    using System;
    using Excel = Microsoft.Office.Interop.Excel;
    using System.Globalization;
    
    namespace ExcelTestApplication
    {
    	class Program
    	{
    		static void Main(string[] args)
    		{
    			if (args.Length>0 && (args.Length % 2) == 0)
    			{
    				try
    				{
    					Console.Out.WriteLine("We are about to merge \"many\" Excel files.\r\n\r\nPress return key to continue...");
    					Console.In.ReadLine();
    					for (int i = 0; i < args.Length; i += 2)
    					{
    						ExcelInterop.MergeFiles(args[i], args[i + 1], true);
    					}
    				}
    				catch (Exception exc)
    				{
    					Console.Out.Write(exc.ToString());
    				}
    			}
    			else
    			{
    				Console.Out.WriteLine();
    				Console.Out.WriteLine();
    				Console.Out.WriteLine("At least 2 excel files are expected and the number of argument must be even.");
    				Console.Out.WriteLine("ExcelTestApplication.exe Destination_1 Source_1 [Destination_2 Source_2] [...] [Destination_N Source_N]");
    			}
    			Console.Out.WriteLine();
    			Console.Out.WriteLine();
    			Console.Out.WriteLine("Press return key to continue...");
    			Console.In.ReadLine();
    		}
    	}
    
    	public static class ExcelInterop
    	{
    		public static void MergeFiles(string fileName, string sourceFileName, bool visible)
    		{
    			// Save the current culture
    			CultureInfo backupCulture = Thread.CurrentThread.CurrentCulture;
    			Excel.Application excel = new Excel.Application { Visible = visible };
    
    			// Change the current thread culture to match Excel culture
    			LanguageSettings excelLanguageSettings = excel.LanguageSettings;
    			Thread.CurrentThread.CurrentCulture = new CultureInfo(excelLanguageSettings.get_LanguageID(MsoAppLanguageID.msoLanguageIDUI));
    			Excel.Workbooks workbooks = excel.Workbooks;
    
    			// Destination
    			Excel.Workbook destinationWorkbook = workbooks.Open(fileName, 2, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    			Excel.Sheets destinationSheets = destinationWorkbook.Sheets;
    			Excel.Worksheet lastDestinationSheet = destinationSheets[destinationSheets.Count] as Excel.Worksheet;
    
    			// Source
    			Excel.Workbook source = workbooks.Open(sourceFileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    			Excel.Sheets sourceSheets = source.Sheets;
    			try
    			{
    				// Copiy and save
    				sourceSheets.Copy(Type.Missing, lastDestinationSheet);
    				destinationWorkbook.Save();
    			}
    			finally
    			{
    				// Go back to default culture
    				Thread.CurrentThread.CurrentCulture = backupCulture;
    			}
    
    			excel.Quit();
    
    			//System.Runtime.InteropServices.Marshal.FinalReleaseComObject(sourceSheets);
    			//System.Runtime.InteropServices.Marshal.FinalReleaseComObject(source);
    			//System.Runtime.InteropServices.Marshal.FinalReleaseComObject(lastDestinationSheet);
    			//System.Runtime.InteropServices.Marshal.FinalReleaseComObject(destinationSheets);
    			//System.Runtime.InteropServices.Marshal.FinalReleaseComObject(destinationWorkbook);
    			//System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelLanguageSettings);
    			//System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excel);
    
    			// Set to null all references...
    			sourceSheets = null;
    			source = null;
    			lastDestinationSheet = null;
    			destinationSheets = null;
    			destinationWorkbook = null;
    			excelLanguageSettings = null;
    			excel = null;
    
    			GC.Collect();
    			GC.WaitForPendingFinalizers();
    			GC.Collect();
    			GC.WaitForPendingFinalizers();
    		}
    	}
    }
    

    More information on this sample :

    I'm using a fresh install of Windows 7 Pro + VS 2010 Ultimate + Office 2010 Pro Plus

    The sample target .Net 2.0

    I'm referencing the following assemblies (because I want to used it on Excel 2003, 2007 or 2010 platform):

    • Microsoft.Office.Interop.Excel 12
    • Microsoft.Vbe.Interop 12
    • office 12

    I tried this code with 8 workbook files as argument (XLS format). The destination files contains tables and source workbooks contains images. I tried locally and on a remote computer. Somteimes it works, sometimes it doesn't. And the RPC error is not always the same... 

    Please let me know what's wrong, I want to understand my mistakes to not reproduce the same errors in the future.

    More informations: Locally, the error is always 0x80010105 RPC_E_SERVERFAULT. I tested on another computer this sample, the same error happen.


    Best regards, Julien

    • Edited by JulienC75 Thursday, October 21, 2010 8:05 AM Additional informations
    Wednesday, October 20, 2010 4:51 PM
  • Hello again Julien,

    Please monitor the Processes in Task Manager. When you finish using the code, does the Excel.exe process exist in Task Manager? It will help us check if all com objects have been released successfully.

    If I have any problem on using this code on my side, I will follow up again. Also I will follow up if there is any useful information I could share with you. Thanks!

    Best regards,
    Bessie Zhao - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, October 21, 2010 9:28 AM
  • Bessie,

    When everything works, I have no instance of Excel remaining in task manager. When the error occurs, Excel remains in memory...

    What I'd like to do, is to handle properly an error to close Excel and advice the user that something happened.

    On my side, I've made more test:

    • I've tried with other workbooks and it works perfectly every time (Excel 2010).
    • I tried with Excel 2003 with the workbooks causing Excel 2010 to crash... it also crash

    After all this time spent on this issue, I finally think it comes from the workbooks I'm trying to merge. I'll try to reproduce the issue with workbooks without customer data and send you the files, perhaps you'll be able to reproduce the problem...

    Even if the problem seems to come from the workbooks, could you review the code I sent to tell me if everything is ok, and help me to correctly handle Excel errors?


    Best regards, Julien
    Thursday, October 21, 2010 12:34 PM
  • Hello again Julien,

    I use the project above to merge two excel files on my side. The code works fine. There is no an error throwing. So sorry, currently, I am not able to reproduce this issue in my side. Have a nice weekend.

    Best regards,
    Bessie Zhao - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, October 22, 2010 6:51 AM
  • Thank you for testing Bessie,

    As I said, I think it is more related to the files I'm trying to merge than linked to the code. However, if I perform the same steps manually, it works...

    Julien


    Best regards, Julien
    Friday, October 22, 2010 9:16 AM
  • Hello again Julien,

    There must be some special contents in the excel files you are want to merge. I am not sure if it could help you. However, I think you could try to compare these excel files you are using with the new excel files. Are the version of these excel files the same as the version of Office in the test machine?

    Best regards,
    Bessie Zhao - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, October 25, 2010 9:51 AM