Discussion:
Automation Excel too slow..
(too old to reply)
bbg
2005-09-11 23:27:19 UTC
Permalink
I like to get some help from Excel-automation-experienced guys:
I started from article http://support.microsoft.com/kb/186122/ which shows
how to open, read some contents, close excel file in MFC.
I copied codes from article and pasted to OnInitDialog() with slight
modification. Every time I click button, it opens, reads, displays Excel
file contents in poped-up dialog, then when OK button clicked, it closes
Excel file and application. It seems works and I have two problems here(I am
using VC++6.0, win2000, excel 2000):

1. Too slow.. about 8 seconds after button clicked, 'Server Busy' message
box poped-up. It provides two button 'SwitchTo' and 'Retry'. I always need
to click 'Retry' button to proceed. What am I doing wrong here?

BOOL CMyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// ...............
_Application objApp;
_Workbook objBook;
Workbooks objBooks;

// Instantiate Excel and open an existing workbook.......... it takes
too long???
objApp.CreateDispatch("Excel.Application");
objBooks = objApp.GetWorkbooks();
objBook = objBooks.Open("C:\\Work\\Test.xls",
VOptional, VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional, VOptional);

// read, fill, find, whatever .......

//Close the workbook without saving changes
//and quit Microsoft Excel.
objBook.Close(COleVariant((short)FALSE), VOptional, VOptional);
objApp.Quit();

2. Can I start and quit Excel application in main frame rather than in my
dialog's OnInitDialog() so that multiple parts of my program can play with
Excel file? What I have tried was make _Application objApp, _Workbook
objBook, Workbooks objBooks as global variable and the debugger is giving
errors:

First-chance exception in Medisafe.exe (OLE32.DLL): 0xC0000005:
Access Violation.

Thanks in advance
Bob
Headache
2005-09-12 14:33:10 UTC
Permalink
Post by bbg
Too slow.. about 8 seconds after button clicked, 'Server Busy' message
box poped-up. It provides two button 'SwitchTo' and 'Retry'. I always need
to click 'Retry' button to proceed. What am I doing wrong here?
Nothing from a procedural point of view. If you need to get rid of the
dialog box or extend the time before it displays then look at
AfxOleGetMessageFilter and the class COleMessageFilter.
Post by bbg
Can I start and quit Excel application in main frame rather than in my
dialog's OnInitDialog() so that multiple parts of my program can play with
Excel file? What I have tried was make _Application objApp, _Workbook
objBook, Workbooks objBooks as global variable and the debugger is giving
errors
Yes no problem but even better for a global Excel is create it in your
applications InitInstance and quit it in your applications
ExitInstance. What you have is a memory access violation so you need to
figure out which COM call is giving it. There should be no problems
with cross-thread marshalling as your dialog will most probably be
created on your main thread. To test either use the thread window or
print out the value of GetCurrentThreadId() in both your main thread
and the function where you call DoModal - check they are the same.
Continue reading on narkive:
Loading...