Waiting for Excel

Terry

Member
Joined
Aug 19, 2014
Messages
13
Programming Experience
3-5
I want to open an Excel workbook with a call from VB and have a procedure that works fine except that on rare occasions I get a system exception thrown, because I suspect that procedure is not giving enough time for Excel to open. On the assumption this is correct I wanted to make use of a method of the Process class called WaitForExit in order to pause procedure execution until Excel opening had completed.

Previously I had been successfully using:
xlWorkbook = xlWorkBooks.Open(filename)

To both OPEN the workbook AND create xlWorkbook (type Workbook) to allow me to go on to use...

xlWorkSheets = xlWorkbook.Sheets

This presumably internally provides the index needed to maintain the specific reference to the workbook so that allows the above statement to work.

However, if I now use a Process class method such as:

pXlStart= Process.Start("Excel", filename)

If I try to set the workbook reference with something like:

xlWorkbook = xlWorkBooks(filename)

I get an error message indicating "Index cannot be found". I have tried several ways to provide the Index unsuccessfully. In any case it seems awkward to have to chase up an index that is automatically supplied by the first case.

What would be the best approach to use Process.Start to open a new instance of Excel and then track the opening while allowing for usual workbook and worksheet settings to be made.

Thanks
 
Back
Top