Dr_Gonzo 0 Newbie Poster

I've recently written a C# application that used Word input files and outputted a single Excel file. The output part worked flawlessly, yet the input part caused me a lot of headaches.

It were a hundred or so Word files, and every so often the program would crash (on a random Word file) with an exception along the lines of:

- LIB_NOT_REGISTERED
- RPC_E_DISCONNECTED
- Word could not fire the event

The first 2 happened in the Document.Open() function or in the code right thereafter (which are just integer assignments, and each Word file had a Table), the third one seemed to be an error if Word had not shut down correctly (after aforementioned crashes) and it wouldn't read from a document, until I manually opened it and confirmed that I wanted to continue to open it.

I reinstalled the PIA, and used the 12.0 COM objects (through Add Reference).

These crashes nearly made me go mad, until I discovered that if I set a breakpoint (VS2008 IDE) on the Document.Open function, and waited a second or 2 before continuing, these errors would not occur.

I've been tapping F5 like a monkey on a typewriter as a result of this.

My guess is that I'm perhaps addressing something that isn't valid anymore (the Word app or Word doc, although the docs get closed after the info is extracted (and set to null), and the Word app closes if they're all done). As I guess my 'waiting' allows the program to stay stable, it doesn't quite add up. Adding a (programmatical) timer was an idea, but does not address the core of the problem.

Is there something I'm missing, or are there flags I can check before opening a file?

Thanks for reading, any help or insights are welcome, as I fear it's something stupid I did or forgot about!

I used the following code for the Word part (I left out the loop code and extraction functions):

private Word.ApplicationClass _wordApp;
private Word.Document _doc;


public Form1()
        {
            InitializeComponent();

            _wordApp = new Word.ApplicationClass();
            _doc = null;
        }

//after user clicks button, do the following
btnGo_Click(object sender, Eventargs e){
_doc = _wordApp.Documents.Open(
                ref file, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj, ref nullobj);

int numTables = _doc.Content.Tables.Count;
int numRows = _doc.Content.Tables[1].Rows.Count;
int numCols = _doc.Content.Tables[1].Columns.Count;

//get stuff from doc

_doc.Close(ref nullobj, ref nullobj, ref nullobj);
_doc = null;

//quit Word
_wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
}