views:

285

answers:

0

I'm automating Excel in Visual Studio 2008 in my 64-bit MFC application. I've added the MFC wrappers and have followed the articles on MSDN on how to implement. Bascically I'm just reading the values of a range. The problem is the Excel process stays loaded in the task manager. Does it have something to do with a 64-bit app running on 64 bit Windows or am I doing something wrong?

CApplication oExcel;  // the Excel _Application object

if(!oExcel.CreateDispatch("Excel.Application"))
{
 AfxMessageBox("Cannot start Excel and get Application object.");
 return false;
}
else
{
         //Make the application invisible
 oExcel.put_Visible(FALSE);

 VARIANT varFalse;
 varFalse.vt = VT_BOOL;
 varFalse.boolVal = false;

 // VARIANT used in place of optional-parameters.
 VARIANT varOpt;
 varOpt.vt = VT_ERROR;
 varOpt.scode = DISP_E_PARAMNOTFOUND;

 CWorkbooks oWorkbooks = oExcel.get_Workbooks();

 //Crazy MFC needs all optional parameters present
 CWorkbook oWorkBook(oWorkbooks.Open( m_sExcelFilename, varOpt, varOpt, varOpt, varOpt, varOpt, varOpt, varOpt, varOpt, varOpt, varOpt, varOpt, varOpt, varOpt, varOpt ));
 CWorksheets oWorkSheets = oWorkBook.get_Worksheets();

 VARIANT varSheetNum = COleVariant((short)1, VT_I2);
 CWorksheet oWorkSheet = oWorkSheets.get_Item(varSheetNum);
 oWorkSheet.Activate();

 CString sCellStart, sCellEnd;
 VARIANT varCellStart, varCellEnd;
 int i = 16;
 sCellStart.Format("A%i", i );
 sCellEnd.Format("AN%i", i );
 varCellStart = COleVariant(sCellStart);
 varCellEnd = COleVariant(sCellEnd);

 CRange oRange = oWorkSheet.get_Range(varCellStart, varCellEnd);

 VARIANT varVal = oRange.get_Value2();

 oRange.Clear();
 oRange.ReleaseDispatch();

 oWorkSheet.ReleaseDispatch();

 oWorkSheets.ReleaseDispatch();

 oWorkBook.Close(varFalse, varOpt, varOpt );
 oWorkBook.ReleaseDispatch();

 oWorkbooks.Close();
 oWorkbooks.ReleaseDispatch();

 oExcel.Quit();
 oExcel.ReleaseDispatch();

}