From a .net web app I need to:
Open a Word document - Document 1
Loop through the word documents in a folder - documents 2, 3 and 4 - extract some data from tables in documents 2, 2 and 4 and copy it all in to a table in Document 1.
I have it all working but it is as buggy as hell on the server. (All sorts of weird and wonderful COM errors come back).
Most of them revolve around the application being busy or the document being open and locked by another user.
So, I want to catch errors.
So, let's try to open the document to copy to:
Word._Document oDoc; //document(s) to copy from
Word._Document oDocAll; //document to copy to
oWord = new Word.Application();
bool Abandon = false;
oDocAll = oWord.Documents.Open(ref oFileAll, ref oMissing, ref omissing etc.)
//some error happened opening the file - give up
oWord.Quit(ref save, ref oMissing, ref oMissing);
Abandon = true;
if (Abandon) return;
But the above will not compile - 'Use of unassigned local variable oDocAll'
If you take the catch block out and put a finally block in, it compiles but what is the use of that?
I don't get it. How do you try to open a document, catch errors but then be able to reference that document afterwards (assuming no errors)
So, put simply - how can I open a Word document, catch errors if it does not open properly and be able to access the Word document if there are no errors?
Thanks for any help.