void CListMacrosDlg::OnRun()
{
USES_CONVERSION;
UpdateData(TRUE);
COleVariant vOpt(DISP_E_PARAMNOTFOUND, VT_ERROR); //for Optional args
//Determine the PROGID for the filename supplied
CLSID clsid;
HRESULT hr = GetClassFile(T2OLE(m_sFilename), &clsid);
if (FAILED(hr))
{
CString sMsg;
sMsg.Format("Unable to determine progid for file: %s", m_sFilename);
AfxMessageBox(sMsg);
return;
}
LPOLESTR lpOleStr;
ProgIDFromCLSID(clsid, &lpOleStr);
CString sProgID = CString(lpOleStr);
sProgID.MakeUpper();
//If the version independent progid is EXCEL.SHEET, WORD.DOCUMENT, or
//POWERPOINT.SHOW, then open the file in its associated application.
//Once the file is open, call GetMacros to place the list of macro
//names in the list box.
if(sProgID.Find("EXCEL.SHEET",0)>=0) //Microsoft Excel Workbook
{
Excel::_Application oApp;
oApp.CreateDispatch("Excel.Application");
Excel::Workbooks oBooks = oApp.GetWorkbooks();
Excel::_Workbook oBook = oBooks.Open(m_sFilename, vOpt, vOpt, vOpt,
vOpt, vOpt, vOpt, vOpt, vOpt,
vOpt, vOpt, vOpt, vOpt);
LPDISPATCH lpDisp = oBook.GetVBProject();
GetMacros(lpDisp);
oBook.Close(COleVariant((short)FALSE), vOpt, vOpt);
oApp.Quit();
}
else if(sProgID.Find("WORD.DOCUMENT",0)>=0) //Microsoft Word Document
{
MSWord::_Application oApp;
oApp.CreateDispatch("Word.Application");
MSWord::Documents oDocs = oApp.GetDocuments();
MSWord::_Document oDoc = oDocs.Open(COleVariant(m_sFilename), vOpt,
vOpt, vOpt, vOpt, vOpt, vOpt,
vOpt, vOpt, vOpt, vOpt, vOpt);
// For Office Word 2003 and Office Word 2007, 16 parameters are required.
/* MSWord::_Document oDoc = oDocs.Open(COleVariant(m_sFilename), vOpt,
vOpt, vOpt, vOpt, vOpt, vOpt,
vOpt, vOpt, vOpt, vOpt, vOpt,
vOpt,vOpt,vOpt,vOpt);
*/
LPDISPATCH lpDisp = oDoc.GetVBProject();
GetMacros(lpDisp);
oDoc.Close(COleVariant((short)FALSE), vOpt, vOpt);
oApp.Quit(vOpt, vOpt, vOpt);
}
else if(sProgID.Find("POWERPOINT.SHOW",0)>=0) //Microsoft PowerPoint
//Presentation
{
PPT::_Application oApp;
oApp.CreateDispatch("Powerpoint.Application");
PPT::Presentations oAllPres = oApp.GetPresentations();
PPT::_Presentation oPres = oAllPres.Open(m_sFilename, 0, 0, 0);
LPDISPATCH lpDisp = oPres.GetVBProject();
GetMacros(lpDisp);
oPres.Close();
oApp.Quit();
}
else if(sProgID.Find("ACCESS.APPLICATION", 0)>=0) //Microsoft Access DB
{ //** Special Consideration for Access Code Project
MSAccess::_Application oApp;
oApp.CreateDispatch("Access.Application");
MSAccess::DoCmd oDoCmd = oApp.GetDoCmd();
oApp.OpenCurrentDatabase(m_sFilename, FALSE);
// For Office Access 2003 and Office Access 2007, 3 parameters are required.
/*oApp.OpenCurrentDatabase(m_sFilename, FALSE,NULL);*/
MSAccess::_CurrentProject oProj = oApp.GetCurrentProject();
MSAccess::AllObjects oObjs = oProj.GetAllModules();
CString sModName, sProcName, sItem;
long lProcKind = 0;
for(long i=0;i<=oObjs.GetCount()-1;i++)
{
MSAccess::AccessObject oObj = oObjs.GetItem(COleVariant(i));
sModName = oObj.GetName();
//Module must be open:
oDoCmd.OpenModule(COleVariant(sModName), vOpt);
MSAccess::Modules oMods = oApp.GetModules();
MSAccess::Module oMod = oMods.GetItem(COleVariant(sModName));
long lLineCount = oMod.GetCountOfLines();
long j=1;
while(j<lLineCount)
{
sProcName = oMod.GetProcOfLine(j, &lProcKind);
if(!sProcName.IsEmpty()){
sItem.Format("%s\t\t%s", sModName, sProcName);
m_MacroList.AddString(sItem);
j = j + oMod.GetProcCountLines(sProcName, lProcKind); }
else {
j++; }
}
}
oApp.Quit(0);
}
else //Other...
{
CString sMsg;
sMsg.Format("Unable to extract macro names for files with progid %s",
sProgID);
AfxMessageBox(sMsg);
}
}