Dear Gurus
First thanks in advance for your help.
I have worked on a c++ project which has an automation connection with the excel app( create an excel app, then open a workbook, keep sending data to a range of this book every second). Some part of the code is as follows:
/********************************************/
if(!app.CreateDispatch("Excel.Application"))
{
AfxMessageBox("Couldn't start Excel and get an application 0bject");
return;
}
app.SetVisible(TRUE);
lpDisp = app.GetWorkbooks(); // Get an IDispatch pointer.
ASSERT(lpDisp);
books.AttachDispatch( lpDisp ); // Attach the IDispatch pointer
// Open a workbook.
lpDisp = books.Open("c:\\Data\\test.xls",
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional);
ASSERT(lpDisp);
// Attach to a Workbook object.
book.AttachDispatch( lpDisp ); // Attach the IDispatch pointer
// Get sheets.
lpDisp = book.GetSheets();
ASSERT(lpDisp);
sheets.AttachDispatch(lpDisp);
lpDisp = sheets.GetItem( COleVariant((short)(1)) );
ASSERT(lpDisp);
// Attach the lpDisp pointer to a Worksheet object.
sheet.AttachDispatch(lpDisp);
lpDisp = sheet.GetRange(COleVariant("A2"), COleVariant("A2"));
ASSERT(lpDisp);
range1.AttachDispatch(lpDisp); // Attach the IDispatch pointer
// to the range object.
range1.Clear();
range1.SetValue (COleVariant("test"));
/*********************************************/
The code works well if I didn't touch the excel workbook. However, when I try to edit a cell ( e.g. type just "111" in any cell), something happens: the cpu reaches 100%, Excel froze, and after a while crashes).
After I check it, I found that it may be because when I edit a cell in a workbook, the excel application is set as edit mode and then can't accept any other input data(locked ?). However, my c++ code didn't realize it and keeps sending messages. Thus, the cpu got busy and excel application become crashed. This is my thinking. Gurus, maybe you have other ideas on the real reason ?
If my guess is correct, I plan to find some API for the automation, for instance, getmode of the workbook. If the mode is editing, I will not send message to it. However, although I spent hours finding info about it, I didn't get any cues. Gurus, do you have any ideas whether I can retrieve the status/mode of the excel workbook ? If can, how can I do it ? What function should I call ? Or if my method is not right, could you give me some suggestions on how to get through it.
Thank your guys so much !
Tony
