excel.CreateDispatch(_T( Excel.Application ))失败

http://blog.163.com/keke_114071/blog/static/203253033201211111395839/


今天由于工作需要,用VC来编写一个操作Excel的功能,在打开Excel进程时,出现了如标题所示的问题。我上网找了一些答复,最终找到了解决方法,就是在创建COM对象之前没初始化COM所造成的。解决要在加载Excel进城之前,加入如下代码:

HRESULT hr; 
 hr = CoInitialize(NULL); 
 
 if(FAILED(hr)) 
 { 
  AfxMessageBox(_T("Failed to call Coinitialize()" )); 
 }


CoInitialize是Windows提供的API函数,用来告诉 Windows以单线程的方式创建com对象。应用程序调用com库函数(除CoGetMalloc内存分配函数)之前必须初始化com库。

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
源代码 void CExcelTestDlg::OnExceltest() { // TODO: Add your control notification handler code here _Application ExcelApp; Workbooks wbsMyBooks; _Workbook wbMyBook; Worksheets wssMysheets; _Worksheet wsMysheet; Range rgMyRge; //创建Excel 服务器(启动Excel) if (!ExcelApp.CreateDispatch("Excel.Application",NULL)) { AfxMessageBox("创建Excel服务失败!"); exit(1); } //利用模板文件建立新文档 wbsMyBooks.AttachDispatch(ExcelApp.GetWorkbooks(),true); wbMyBook.AttachDispatch(wbsMyBooks.Add(_variant_t("g:\\exceltest\\MyTemplate.xlt"))); //得到worksheets wssMysheets.AttachDispatch(wbMyBook.GetWorksheets(),true); //得到sheet1 wsMysheet.AttachDispatch(wssMysheets.GetItem(_variant_t("sheet1")),true); //得到全部的cells,此时,rgMyRge是cells的集合 rgMyRge.AttachDispatch(wsMysheet.GetCells(),true); //设置一行一列的单元的值 rgMyRge.SetItem(_variant_t((long)1),_variant_t((long)1),_variant_t("This is a Excel Test Program!")); //得到所有的列 rgMyRge.AttachDispatch(wsMysheet.GetColumns(),true); //设置第一列 rgMyRge.AttachDispatch(rgMyRge.GetItem(_variant_t((long)1),vtMissing).pdispVal,true); //设置列宽 rgMyRge.SetColumnWidth(_variant_t((long)200)); //调用模版预先存放的宏 ExcelApp.Run(_variant_t("CopyRow"),_variant_t((long)10),vtMissing,vtMissing, vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing, vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing, vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing, vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing); //打印浏览 wbMyBook.SetSaved(true); ExcelApp.SetVisible(true); wbMyBook.PrintPreview(_variant_t(false)); //释放内存 rgMyRge.ReleaseDispatch(); wsMysheet.ReleaseDispatch(); wssMysheets.ReleaseDispatch(); wbMyBook.ReleaseDispatch(); wbsMyBooks.ReleaseDispatch(); ExcelApp.ReleaseDispatch(); } MFC调用excel
用MFC 显示PPT //启动 PowerPoint: void CMainFrame::OnPowerpointStartpowerpoint() { /// Check if the IDispatch connection exists with PowerPoint, // if not create one. if (m_ppt.m_lpDispatch == NULL) { // Create IDispatch connection to PowerPoint. m_ppt.CreateDispatch("PowerPoint.Application"); }; // Bring the PowerPoint application to the front. m_ppt.Activate(); } void CMainFrame::OnPowerpointStartslideshow() { _Presentation oPresentation; SlideShowSettings oShow; // Attach to the Active Presentation. oPresentation.AttachDispatch(m_ppt.GetActivePresentation()); // Attach to the slide-show settings. oShow.AttachDispatch(oPresentation.GetSlideShowSettings()); // Run the slide show. oShow.Run(); } // 创建幻灯片: void CMainFrame::OnPowerpointCreateslide() { // Connect to the active presentation. There is no error trapping. // If the active presentation the framework traps // the error and displays a message box. _Presentation ActivePresentation(m_ppt.GetActivePresentation()); // Connect to the slides collection. Slides oSlides(ActivePresentation.GetSlides()); // This constant is defined in the PowerPoint Object model. // You can use the Object Browser, with Visual Basic Editor // (VBE), to look up the different constant values. const ppLayoutTitleOnly = 11; // Add a new slide to the presentation. This code adds the new // slide to the end of the presentation. oSlides.Add(oSlides.GetCount() + 1l, ppLayoutTitleOnly); } // 创建演示文稿: void CMainFrame::OnPowerpointCreatepresentation() { Presentations PresCollection; // Make sure there is a dispatch pointer for PowerPoint. if(m_ppt.m_lpDispatch == NULL) { // Display a message indicating that PowerPoint is not running. MessageBox("PowerPoint is not running.", "Start PowerPoint"); } else { // Bring PowerPoint to the front. m_ppt.Activate(); // Attach the presentations collection to the PresCollection // variable. PresCollection.AttachDispatch(m_ppt.GetPresentations()); // Create a new presentation. PresCollection.Add(1);

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值