明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

自動(dòng)運(yùn)行的MS-OFFICE應(yīng)用程序

[摘要]這個(gè)指南幫助你學(xué)習(xí)自動(dòng)運(yùn)行的基礎(chǔ)。用這個(gè)代碼,你可以在你的應(yīng)用程序中控制POWERPOINT。你可以程序化的打開(kāi)POWERPOINT,打開(kāi)任何展示,到你想觀看的幻燈片,運(yùn)行幻燈片放映等等。通過(guò)下面給...
這個(gè)指南幫助你學(xué)習(xí)自動(dòng)運(yùn)行的基礎(chǔ)。用這個(gè)代碼,你可以在你的應(yīng)用程序中控制POWERPOINT。

你可以程序化的打開(kāi)POWERPOINT,打開(kāi)任何展示,到你想觀看的幻燈片,運(yùn)行幻燈片放映等等。

通過(guò)下面給出的同樣的步驟,你可以自動(dòng)運(yùn)行WORD,EXCEL,或者任何MS-OFFICE應(yīng)用程序。

(1)       創(chuàng)建一個(gè)基于應(yīng)用程序的對(duì)話框,在appwizard的第三步,選擇AUTOMATION復(fù)選框。

(2)       為START , RUN, CLOSE, FIRST SLIDE, LAST SLIDE, PREVIOUS SLIDE和NEXT SLIDE函數(shù)創(chuàng)建按鈕,從而使用下列函數(shù)。

(3)       在你的應(yīng)用程序的InitInstance添加下列語(yǔ)句:

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

(4)       在你的對(duì)話框類中,打開(kāi)classwizard , 選擇AUTOMATION標(biāo)簽,選擇
ADD CLASS --> FROM A TYPE LIBRARY.,然后從C:\Program Files\Microsoft Office\Office\中選擇msppt8.olb。

(5)       在你的對(duì)話框的頭文件中,添加語(yǔ)句:

#include "msppt8.h"
(6)在你的對(duì)話框頭文件中添加下列變量:
     _Application app;  // app is the Powerpoint _Application object



         
         Presentations Presentations;
         _Presentation Presentation;

         SlideShowView View;

         SlideShowWindow SlideShowWindow;
         SlideShowSettings slideshow;
         Slides slides;     
        _Slide slide;
        現(xiàn)在讓我們到POWERPOINT應(yīng)用程序函數(shù)。
(6)       要啟動(dòng)POWERPOINT,你必須在START函數(shù)中寫出下列代碼:
       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)       在硬盤中打開(kāi)一個(gè)展示,在Open按鈕函數(shù)呼叫中添加代碼:
         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)       為關(guān)閉POWERPOINT,在CLOSE按鈕函數(shù)呼叫中添加代碼:
         void CPowerPntDlg::OnBtnClose()
         {
             if (CanExit())
                  app.Quit();
         }
        在步驟7,8,9中的函數(shù)是你需要知道應(yīng)用程序的基本處理。
        現(xiàn)在我們有了運(yùn)行的POWERPOINT且有了準(zhǔn)備,我們需要用它做一些事情像運(yùn)行幻燈片放映和執(zhí)行其他行為。
        現(xiàn)在讓我們運(yùn)行幻燈片放映。
(9)       為運(yùn)行幻燈片放映,在RUN按鈕函數(shù)呼叫中添加代碼:
       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)   有時(shí),你可能想從第一張幻燈片中啟動(dòng)全部。要到第一張幻燈片你可以使用這些代碼:
         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)   既然你有了運(yùn)行的幻燈片放映,你顯然會(huì)在一些時(shí)間點(diǎn)上回到上一張幻燈片。要這樣做,使用下列代碼:
         void CPowerPntDlg::OnBtnPrevious()
         {
                  Presentation = app.GetActivePresentation();
                  SlideShowWindow = Presentation.GetSlideShowWindow();
                  View = SlideShowWindow.GetView();
                  View.Previous();
         }

(13)   現(xiàn)在有興趣到下一張幻燈片?在這種情況下,這個(gè)函數(shù)會(huì)幫助你:
       void CPowerPntDlg::OnBtnNext()
         {
                  Presentation = app.GetActivePresentation();
                  SlideShowWindow = Presentation.GetSlideShowWindow();
                  View = SlideShowWindow.GetView();
                  View.Next();
         }
這就是了,伙計(jì)。檢查出其他轉(zhuǎn)變,動(dòng)畫(huà)制作等的可用函數(shù)。你可以用你特有的方式進(jìn)行。
這是基本框架,你可以看出它處理POWERPOINT是多么容易。用在WORD,EXCEL和其它MS-OFFICE上也是一會(huì)事。祝好運(yùn)和快樂(lè)!