vc 操作ppt的简单例子

vs2008

2007ppt


网上大多例子是其他版本的, 导致不少人没法使用。 类型库不一样。


我照着 codeproject的这篇文章写的,源码已经上传到“我的资源”里,可自行下载。原文:http://www.codeproject.com/Articles/2506/Automating-MS-Office-applications

Steps to follow

By following the same steps given below, you can automate , word, excel or any Microsoft office application.

  1. Create a dialog based application and in the App-wizard's step 3 of 6, select the automation checkbox.
  2. Create buttons for Start , Run, Close, First Slide, Last Slide, Previous Slide and Next Slide functions and use the following functions accordingly.
  3. In your application's InitInstance function , add the following lines.
    // Initialize OLE libraries
    if (!AfxOleInit())
    {
        AfxMessageBox("Failed to initialize OLE");
        return FALSE;
    }
  4. In your dialog's class , open class-wizard , select the automation tab, select "Add Class" ... "From a type library" and select msppt8.olb from "C:\Program Files\Microsoft Office\Office\"
  5. In your header file of your dialog, include the following line.
    #include "msppt8.h"
  6. Add the following variables in your dialog's header file.
    _Application app; // app is the PowerPoint _Application object
    
    Presentations Presentations;
    _Presentation Presentation;
    
    SlideShowView View;
    
    SlideShowWindow SlideShowWindow;
    SlideShowSettings slideshow;
    Slides slides; 
    _Slide slide;
  7. To start PowerPoint, you have to write this code in the Start button's function.
    void CPowerPntDlg::OnBtnStart()
    {
        // Start PowerPoint and get Application object...
        if(!app.CreateDispatch("Powerpoint.Application"))
        {
            AfxMessageBox("Couldn't start PowerPoint.");
        }
        else // Make PowerPoint visible and display a message
        {
            app.SetVisible(TRUE);
            TRACE("PowerPoint is Running!");
        }
    }
  8. To open a presentation from the hard disk, add this code in the Open button's function call.
    void CPowerPntDlg::OnBtnOpen()
    {
        static char BASED_CODE szFilter[] = "PowerPoint Files (*.ppt)|*.ppt||";
        CFileDialog FileDlg(TRUE,"PPT",NULL,OFN_FILEMUSTEXIST|OFN_NONETWORKBUTTON
                    |OFN_PATHMUSTEXIST,szFilter);
        FileDlg.DoModal();
    
        // To get the selected file's path and name
        CString strFileName;
        strFileName = FileDlg.GetPathName();
    
        if(!strFileName.IsEmpty())
        {
            Presentations = app.GetPresentations();
            Presentation = Presentations.Open(strFileName,0,0,1);
        }
    }
  9. To close PowerPoint add this code in the Close button's function call.
    void CPowerPntDlg::OnBtnClose() 
    {
        if (CanExit())
            app.Quit();
    }
  10. To run the slideshow use this code in the Run button's function call
    void CPowerPntDlg::OnBtnRun() 
    {
        Presentations = app.GetActivePresentation();
        slides = Presentation.GetSlides(); 
        // Show the first slide of the presentation 
        slide = slides.Item(COleVariant((long)1)); 
    
        //Run the show 
        slideshow = Presentation.GetSlideShowSettings(); 
        slideshow.Run();
    }
  11. Sometimes, you might want to start all over from the first slide. To go to the first slide you can use this code.
    void CPowerPntDlg::OnBtnFirst() 
    {
        Presentation = app.GetActivePresentation();
        SlideShowWindow = Presentation.GetSlideShowWindow();
        View = SlideShowWindow.GetView();
        View.First();
    }
  12. And similarly, to go to the last slide
    void CPowerPntDlg::OnBtnLast() 
    {
        Presentation = app.GetActivePresentation();
        SlideShowWindow = Presentation.GetSlideShowWindow();
        View = SlideShowWindow.GetView();
        View.Last();
    }
  13. Now that you have the slideshow running, you would obviously want to go to the previous slide at some point of time. To do just that, you can use this code.
    void CPowerPntDlg::OnBtnPrevious() 
    {
        Presentation = app.GetActivePresentation();
        SlideShowWindow = Presentation.GetSlideShowWindow();
        View = SlideShowWindow.GetView();
        View.Previous();
    }
  14. Interested to go to the next slide now ? In that case, this function will help you.
    void CPowerPntDlg::OnBtnNext() 
    {
        Presentation = app.GetActivePresentation();
        SlideShowWindow = Presentation.GetSlideShowWindow();
        View = SlideShowWindow.GetView();
        View.Next();
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值