Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 665473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:42:42+00:00 2026-05-13T23:42:42+00:00

I have an application that writes many times to a formula/macro-laden workbook. It loops

  • 0

I have an application that writes many times to a formula/macro-laden workbook. It loops through some data 3 times creating, filling, saving, then closing an excel file in each iteration. While it works fine when it’s the only version of itself running, if there are multiple instances of it running it has trouble.

Specifically, I’m getting a 0x800AC472 error when writing data to certain cells. It isn’t the same cell or value each time but it has seemed to be on the second pass through each time. This is the relevant code:

public void SetCellValue(int row, int col, string val)
{
    if (_currWorkSheet != null)
    {
        string parms = string.Format("row={0}; col={1}; val={2}", row.ToString(), col.ToString(), val);
        for (short i = 0; i < _maxRetries; i++)
        {
            try { (_currWorkSheet.Cells[row, col] as Range).Value2 = val; return; }
            catch (Exception ex) { HandleError(ex, parms); }
        }
        Exception newExc = new Exception("Too many retries attempting to set cell value.");
        newExc.Data.Add("parms", parms);
        throw newExc;
    }
}


    private void HandleError(Exception exc, string parms)
    {
        if (exc != null && exc.Message != null)
        {
            // Excel error that just needs more time to complete. http://social.msdn.microsoft.com/forums/en-US/vsto/thread/9168f9f2-e5bc-4535-8d7d-4e374ab8ff09/
            if (exc.Message.Contains("0x800AC472"))
                Thread.Sleep(_threadSleepMs); // Give excel a chance to catch up, then keep processing.
            else
            {
                Exception newExc = new Exception("Unexpected Error", exc);
                newExc.Data.Add("parms", parms);
                throw newExc;
            }
        }
    }

I’ve set the _maxRetries to 10 and the _threadSleepMs to 500 and continue to get the error, so I don’t think that increasing it anymore makes sense.

I was wondering if there are alternatives to sleeping the thread to give it a chance to get “unstuck” as it were.

And maybe this would qualify as a second question but I’m not as concerned about this but, when it crashes I still perform a Close() on it in the finally block, but I still have instances of it hanging around. This is how I close it:

public void Dispose()
{
        if (!_disposed)
        {
            if (_currWorkBook != null)
                for (short i = 0; i < _maxRetries; i++)
                {
                    try { _currWorkBook.Close(false, _missing, _missing); break; }
                    catch (Exception ex) { HandleError(ex, ""); }
                }

            if (_app != null)
            {
                if (_app.Workbooks != null)
                    for (short i = 0; i < _maxRetries; i++)
                    {
                        try { _app.Workbooks.Close(); break; }
                        catch (Exception ex) { HandleError(ex, ""); }
                    }

                for (short i = 0; i < _maxRetries; i++)
                {
                    try { _app.Quit(); break; }
                    catch (Exception ex) { HandleError(ex, ""); }
                }

                if (_currWorkSheet != null)
                {
                    Marshal.ReleaseComObject(_currWorkSheet);
                    _currWorkSheet = null;
                }
                if (_currWorkBook != null)
                {
                    Marshal.ReleaseComObject(_currWorkBook);
                    _currWorkBook = null;
                }
                Marshal.ReleaseComObject(_app);
                _app = null;
            }

            GC.Collect();
            _disposed = true;
        }
}

It doesn’t throw and error, so I was just wondering if there were any holes in it?

Thank you,
Jeff

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:42:43+00:00Added an answer on May 13, 2026 at 11:42 pm

    The only solution I was able to come up with was to create a lock on the thread once it needs to use the excel functionality. This just ensured I only have one process using excel at one time. It’s not perfect, especially if unrelated processes also try to use excel, but it was the only fix I could come up with.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UNIX application written in ansi C that writes data directly to
I'm working on an application. I have a servlet (writeDataBase.class) that writes some information
I have an application that streams through 250 MB of data, applying a simple
I have an application that writes to a folder on the C:\ drive. The
I have a server application that writes to a popen(myCommand, w) file descriptor in
I have a small homework application that writes random numbers from 5 to 77
I am writing an application that writes to SQL Compact files that have a
I have an application that write data in the programdata folder in vista. The
I'm writing an application that will have to be able to handle many concurrent
This is a particular problem that I have come across many times, but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.