16,472
社区成员
发帖
与我相关
我的任务
分享
class _Application : public COleDispatchDriver
{……}
(2)在对话框头文件中#include "msppt.h"
(3)程序自动实现初始化。
BOOL CTestDlgApp::InitInstance()
{
// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox(IDP_OLE_INIT_FAILED);
return FALSE;
}
AfxEnableControlContainer();
……
}
(4)在按钮响应中,添加了CreateDispatch如下:
void CTestDlgDlg::OnBtnrun()
{
// TODO: Add your control notification handler code here
_Application m_app;
COleException e;
if(!m_app.CreateDispatch(_T("Powerpoint.Application"),&e))
{
e.ReportError();
AfxMessageBox("Couldn't start PowerPoint.");
}
else // Make PowerPoint visible and display a message
{
m_app.SetVisible(TRUE);
TRACE("PowerPoint is Running!");
}
}
编译成功,运行时点击按钮出错:
e.ReportError();//报错“无效的类字符串”
我查了网上都是使用的这个类字符串的,怎么就是无效的呢?
我用的环境是:笔记本Y470 win7 VC6.0 office2007
void CTestView::OnBatterylife()
{
COleDispatchDriver disp;
COleException *e = new COleException;
try {
// Create instance of Microsoft System Information Control
// by using ProgID.
if (disp.CreateDispatch("SYSINFO.Sysinfo.1", e))
{
//Call BatteryLifePercent.
short nBatteryLifePercent;
disp.InvokeHelper(0x03, DISPATCH_PROPERTYGET, VT_I2,
(void*)&nBatteryLifePercent, NULL);
if (nBatteryLifePercent == 255)
AfxMessageBox("Battery Life % unknown");
else
{
CString cStr;
cStr.Format("Battery Life is at %d%%",
nBatteryLifePercent);
AfxMessageBox(cStr);
}
}
else
throw e;
}
//Catch control-specific exceptions.
catch (COleDispatchException * e)
{
CString cStr;
if (!e->m_strSource.IsEmpty())
cStr = e->m_strSource + " - ";
if (!e->m_strDescription.IsEmpty())
cStr += e->m_strDescription;
else
cStr += "unknown error";
AfxMessageBox(cStr, MB_OK,
(e->m_strHelpFile.IsEmpty())? 0:e->m_dwHelpContext);
e->Delete();
}
//Catch all MFC exceptions, including COleExceptions.
// OS exceptions will not be caught.
catch (CException *e)
{
TRACE("%s(%d): OLE Execption caught: SCODE = %x",
__FILE__, __LINE__, COleException::Process(e));
e->Delete();
}
}