void CAutoProjectDlg::OnSeexlquit() // Message handler function.
{
char buf[1024]; // General purpose message buffer.
_Application oExcel; // oExcel is an _Application object.
Workbooks oBooks;
LPDISPATCH lpDisp;
// Common OLE-variants... Easy variants to use for calling arguments.
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
// Start Excel and get Application object.
if(!oExcel.CreateDispatch("Excel.Application"))
{
AfxMessageBox("Couldn't CreateDispatch on Excel");
return;
}
// Set visible.
oExcel.SetVisible(TRUE);
oExcel.SetUserControl(TRUE); // This is a property of the
// _Application object. Set it so you
// can Release the oExcel and
// oBooks objects without killing
// Excel.
// Get Workbooks collection...
lpDisp = oExcel.GetWorkbooks(); // Get an IDispatch pointer
ASSERT(lpDisp); // or fail.
oBooks.AttachDispatch( lpDisp ); // Attach IDispatch pointer to
// oBooks object.
// Open a workbook.<?xm-deletion_mark author="v-thomr" time="20070327T063248-0600" data=".."?><?xm-insertion_mark_start author="v-thomr" time="20070327T063324-0600"?> If this code is run in Microsoft Office Excel 2007,
// change the file name to MybookTest.xlsx.<?xm-insertion_mark_end?>
lpDisp = oBooks.Open("C:\\MybookTest.xls", // Change for your .xls.
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional // Excel 2000
);
/*
// <?xm-insertion_mark_start author="v-thomr" time="20070327T063406-0600"?>If you want to <?xm-insertion_mark_end?><?xm-deletion_mark author="v-thomr" time="20070327T063415-0600" data="O"?><?xm-insertion_mark_start author="v-thomr" time="20070327T063415-0600"?>o<?xm-insertion_mark_end?>pen an Excel 2002 <?xm-insertion_mark_start author="v-thomr" time="20070327T063420-0600"?>or an Excel 2003 <?xm-insertion_mark_end?>workbook<?xm-insertion_mark_start author="v-thomr" time="20070327T063429-0600"?>,
// use this code<?xm-insertion_mark_end?>.
lpDisp = books.Open("C:\\Test",
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional ); // Excel 2000 requires
*/ // only 13 arguments<?xm-insertion_mark_start author="v-thomr" time="20070327T063451-0600"?>
// If you want to open an Excel 2007 workbook, use this code.
lpDisp = oBooks.Open("C:\\Test.xlsx",
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional);<?xm-insertion_mark_end?>
ASSERT(lpDisp); // It better have worked?
HWND hWnd;
hWnd = ::FindWindow("XLMain", // Pointer to class name.
NULL // Pointer to window name option.
);
if(NULL==hWnd)
{
long lErr = GetLastError();
sprintf(buf, "FindWindow error code = %d", lErr);
AfxMessageBox(buf);
}
DWORD pid; // Variable to hold the process ID.
DWORD dThread; // Variable to hold (unused) thread ID.
dThread = GetWindowThreadProcessId(hWnd, // Handle to window.
&pid // Address of variable
// for process identifier.
);
HANDLE hProcess; // Handle to existing process
hProcess = OpenProcess(SYNCHRONIZE | PROCESS_ALL_ACCESS, // access
// flag
TRUE, // handle inheritance flag
pid // process identifier
);
oBooks.ReleaseDispatch(); // Release the object-IDispatch binding.
oExcel.ReleaseDispatch();
oBooks = NULL; // Destroy the object references.
oExcel = NULL;
DWORD dwReason; // Variable to receive signal.
dwReason = WaitForSingleObject(hProcess, // Handle to object to
// wait for its end.
INFINITE // Time-out interval in
// milliseconds.
);
sprintf(buf, "Reason for Wait to terminate is %d", dwReason);
// Zero is good.
AfxMessageBox(buf);
}