自动运行的MS-OFFICE应用程序

发表于:2007-07-14来源:作者:点击数: 标签:
这个指南帮助你学习自动运行的基
这个指南帮助你学习自动运行的基础。用这个代码,你可以在你的应用程序中控制POWERPOINT。

你可以程序化的打开POWERPOINT,打开任何展示,到你想观看的幻灯片,运行幻灯片放映等等。

通过下面给出的同样的步骤,你可以自动运行WORD,EXCEL,或者任何MS-OFFICE应用程序。

(1)       创建一个基于应用程序的对话框,在appwizard的第三步,选择AUTOMATION复选框。

(2)       为START , RUN, CLOSE, FIRST SLIDE, LAST SLIDE, PREVIOUS SLIDE和NEXT SLIDE函数创建按钮,从而使用下列函数。

(3)       在你的应用程序的InitInstance添加下列语句:

       // Initialize OLE libraries
         if (!AfxOleInit())
         {
                  AfxMessageBox("Failed to initialize OLE");
                  return FALSE;
         }

(4)       在你的对话框类中,打开classwizard , 选择AUTOMATION标签,选择
ADD CLASS --> FROM A TYPE LIBRARY.,然后从C:\Program Files\Microsoft Office\Office\中选择msppt8.olb。

(5)       在你的对话框的头文件中,添加语句:

#include "msppt8.h"
(6)在你的对话框头文件中添加下列变量:
     _Application app;  // app is the Powerpoint _Application object



         
         Presentations Presentations;
         _Presentation Presentation;

         SlideShowView View;

         SlideShowWindow SlideShowWindow;
         SlideShowSettings slideshow;
         Slides slides;     
        _Slide slide;
        现在让我们到POWERPOINT应用程序函数。
(6)       要启动POWERPOINT,你必须在START函数中写出下列代码:
       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!");
                   }
         }

(7)       在硬盘中打开一个展示,在Open按钮函数呼叫中添加代码:
         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);
                  }
         }
(8)       为关闭POWERPOINT,在CLOSE按钮函数呼叫中添加代码:
         void CPowerPntDlg::OnBtnClose()
         {
             if (CanExit())
                  app.Quit();
         }
        在步骤7,8,9中的函数是你需要知道应用程序的基本处理。
        现在我们有了运行的POWERPOINT且有了准备,我们需要用它做一些事情像运行幻灯片放映和执行其他行为。
        现在让我们运行幻灯片放映。
(9)       为运行幻灯片放映,在RUN按钮函数呼叫中添加代码:
       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();
         }
(10)   有时,你可能想从第一张幻灯片中启动全部。要到第一张幻灯片你可以使用这些代码:
         void CPowerPntDlg::OnBtnFirst()
         {
                  Presentation = app.GetActivePresentation();
                  SlideShowWindow = Presentation.GetSlideShowWindow();
                  View = SlideShowWindow.GetView();
                  View.First();
         }

(11)   相似地,到最后一张幻灯片:
       void CPowerPntDlg::OnBtnLast()
         {
                  Presentation = app.GetActivePresentation();
                  SlideShowWindow = Presentation.GetSlideShowWindow();
                  View = SlideShowWindow.GetView();
                  View.Last();
         }
(12)   既然你有了运行的幻灯片放映,你显然会在一些时间点上回到上一张幻灯片。要这样做,使用下列代码:
         void CPowerPntDlg::OnBtnPrevious()
         {
                  Presentation = app.GetActivePresentation();
                  SlideShowWindow = Presentation.GetSlideShowWindow();
                  View = SlideShowWindow.GetView();
                  View.Previous();
         }

(13)   现在有兴趣到下一张幻灯片?在这种情况下,这个函数会帮助你:
       void CPowerPntDlg::OnBtnNext()
         {
                  Presentation = app.GetActivePresentation();
                  SlideShowWindow = Presentation.GetSlideShowWindow();
                  View = SlideShowWindow.GetView();
                  View.Next();
         }
这就是了,伙计。检查出其他转变,动画制作等的可用函数。你可以用你特有的方式进行。
这是基本框架,你可以看出它处理POWERPOINT是多么容易。用在WORD,EXCEL和其它MS-OFFICE上也是一会事。祝好运和快乐!

原文转自:http://www.ltesting.net