文章編號: 237554 - 上次校閱: 2007年5月24日 - 版次: 5.1

如何藉由使用 MFC,Visual C++ 5.0 中和在 Visual C++ 6.0 中自動化 PowerPoint

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。
全部展開 | 全部摺疊

結論

本文將告訴您如何自動化 Microsoft PowerPoint 使用 Microsoft 基礎類別 (MFC),Microsoft Visual C++ 5.0 中和在 Visual C++ 6.0 中。

自動化 PowerPoint,並在簡報中執行包含的巨集,您可以使用 MFC。

其他相關資訊

通常,您可以使用類別精靈來包裝型別程式庫中的所有函式。不過,類別精靈無法包裝函式的需要是 SAFEARRAY 的型別引數,會在標頭檔中產生如以下所示的訊息。
  // method 'MyMethod' not emitted because of invalid return type or    parameter type
				
當您使用類別精靈來產生包裝函式 Microsoft PowerPoint Application::Run 函式的類別將非包裝因為需要花上的引數鍵入 SAFEARRAY。您仍然可以使用 0x7e6 DISPID InvokeHelper 函數呼叫 Application::Run。如需 執行 的函式的詳細資訊,您可以檢查 PowerPoint 型別程式庫使用 OLE/COM 檢視器] 工具。

下列步驟說明如何自動化呼叫 Application::Run,並執行一個 PowerPoint PowerPoint 巨集。

使用巨集建立 PowerPoint 簡報的步驟

  1. 啟動 Microsoft PowerPoint,有三個空白投影片建立一份新簡報。
  2. 按下 ALT + F11 即可啟動 [Visual Basic 編輯器]。
  3. 插入] 功能表按一下 [模組],以在新的簡報中插入模組]。
  4. 將下列程式碼加入模組。
       Sub ChangeBackColor()  'Change backcolor of slides 1, 2 and 3
        With ActivePresentation.Slides.Range(Array(1, 2, 3))
            .FollowMasterBackground = msoFalse
            .Background.Fill.ForeColor.SchemeColor = ppAccent2
        End With
       End Sub
    
       Sub ChangeText(vArray As Variant)
        'Add a text box to each slide and use the text from the
        'array passed into this procedure
        Dim s As Slide
        For i = 1 To ActivePresentation.Slides.Count
            With ActivePresentation.Slides(i).Shapes.AddTextbox( _
                            msoTextOrientationHorizontal, 186#, 54#, 336#, 36#)
                .TextFrame.TextRange.Text = vArray(i - 1)
                .TextFrame.TextRange.Font.Size = 60
            End With
        Next
       End Sub
    						
  5. 將簡報儲存為下列,和收盤 PowerPoint 的其中一個:
    • 在 PowerPoint 2007 簡報為啟用巨集的簡報 (*.pptm) 使用來儲存檔案名稱,例如 C:\Pres.pptm。
    • 在 PowerPoint 2003 和舊版的 PowerPoint 中,將簡報儲存為一份簡報 (*.ppt) 使用如 C:\Pres.ppt 的檔案名稱。

建立 MFC 應用程式的步驟

  1. 建立一個新 MFC 對話方塊應用基礎] 方塊中程式名為 AutoPPT
  2. 從 [檢視] 功能表 (或按 CTRL + W),選取類別] 精靈,並請依照下列步驟執行:
    1. 按一下 [自動化] 索引標籤。
    2. 按一下 [加入類別],然後再按一下 [從型別程式庫
    3. 找不到 PowerPoint 文件庫,然後再新增 PowerPoint 型別程式庫:
      • 對於 PowerPoint 2007 新增 Msppt.olb
      • 對於 PowerPoint 2003 新增 Msppt.olb
      • PowerPoint 2002 加入 Msppt.olb
      • 對於 PowerPoint 2000 新增 Msppt9.olb
      • 對於 PowerPoint 97 新增 Msppt8.olb
  3. 選取 [使用資源識別碼 IDD_AUTOPPT_DIALOG] 對話方塊。將按鈕加入至對話方塊並再將下列程式碼加入至按鈕的處理常式。
       _Application	oApp;
       if(!oApp.CreateDispatch("Powerpoint.Application"))
       {
            AfxMessageBox("Could not get Powerpoint application.");
            return;
       }
       oApp.SetVisible(TRUE);
    
    
       //Get the Presentations collection and open a presentation
       Presentations oPresSet(oApp.GetPresentations());
       CString strFilename;
       //For PowerPoint 2007, change the file name to "c:\\pres.pptm"
       strFilename = "c:\\pres.ppt";
       _Presentation oPres(oPresSet.Open(strFilename, // Filename
                                         true,        // Readonly
                                         false,       // Untitled
                                         true         // WithWindow
                          ));
    		
       //*************** How to Run PowerPoint Macros *********************
    
       // Run "ChangeBackColor" macro in the presentation -- note that the
       // "ChangeBackColor" macro requires no arguments
    
       {
           COleVariant vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
           static BYTE parms[] = 
                VTS_BSTR  VTS_VARIANT;
           LPCTSTR lpszMacroName = "Pres.ppt!ChangeBackColor";
    
           oApp.InvokeHelper(0x7e6, DISPATCH_METHOD, VT_EMPTY, NULL,  parms,
                             lpszMacroName, //Macro Name
                             &vOpt          //No arguments, for example, ignore
                            );
       }
    			
    
       // Run "ChangeText" macro in the presentation;
       // The macro requires three arguments -- the first two are strings
       // and the last one is a long	
    		
       {
    
            COleSafeArray saArgs;  //Create a 1-dimensional array with three 
                                   //elements
    
            DWORD numElements[1];
            numElements[0]= 3;	
            saArgs.Create(VT_VARIANT, 1, numElements);	
            long index[1];
            VARIANT v;
    
            index[0]=0;		//Fill 1st element
            CString sName("ABC");
            VariantInit(&v);
            v.vt= VT_BSTR;
            v.bstrVal = sName.AllocSysString();
            saArgs.PutElement(index, &v);
            SysFreeString(v.bstrVal);
            VariantClear(&v);
    
            index[0]=1;		//Fill 2nd element
            CString sCompany("XYZ");
            VariantInit(&v);
            v.vt= VT_BSTR;
            v.bstrVal = sCompany.AllocSysString();
            saArgs.PutElement(index, &v);
            SysFreeString(v.bstrVal);
            VariantClear(&v);
    
            index[0]=2;		//Fill 3rd element
            VariantInit(&v);
            v.vt = VT_I4;
            v.lVal=123;
            saArgs.PutElement(index, &v);
            VariantClear(&v);
    
            static BYTE parms[] = 
                 VTS_BSTR  VTS_VARIANT;
            LPCTSTR lpszMacroName = "Pres.ppt!ChangeText";
            oApp.InvokeHelper(0x7e6, DISPATCH_METHOD, VT_EMPTY, NULL, parms,
                              lpszMacroName,   //Macro Name
                              (VARIANT*)saArgs //Array of macro parameters 
                             );
    
            saArgs.Detach();
       }
    
    						
    附註 如果在 PowerPoint 2002 型別程式庫包裝在類別將新增額外的引數 (假) 給 Open 方法的呼叫。
  4. 新增適當包含陳述式為 [Powerpoint 標頭檔。
    #include "msppt8.h" // use msppt9.h for 2000 or msppt.h for 2002, 2003, and 2007
    					
  5. 下列程式碼行加入 CAutoPPTApp::InitInstance() 函式的開頭。
         AfxOleInit();
    						
  6. 編譯並執行。選取您在執行自動化程式碼] 對話方塊中加入的按鈕。一旦程式碼完成,PowerPoint 會保持可見,以便您可以觀察巨集所作的簡報中所做的變更。

?考

如需有關如何自動化 PowerPoint 使用 Visual C++ 的詳細資訊,請參閱 「 Microsoft 知識庫 」 中下列文:
180616? (http://support.microsoft.com/kb/180616/ ) 如何使用 MFC 建立並顯示 PowerPoint 簡報

這篇文章中的資訊適用於:
  • Microsoft Office PowerPoint 2007
  • Microsoft Office PowerPoint 2003
  • Microsoft PowerPoint 2002 Standard Edition
  • Microsoft PowerPoint 2000 Standard Edition
  • Microsoft PowerPoint 97 Standard Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Foundation Class Library 4.2
關鍵字:?
kbmt kbexpertiseinter kbautomation kbhowto KB237554 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:237554? (http://support.microsoft.com/kb/237554/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。