如何使用 Office 2003、Office XP Developer 和 Office 2000 Developer 中的 Visual Basic 來自動化 PowerPoint

文章翻譯 文章翻譯
文章編號: 222929 - 檢視此文章適用的產品。
全部展開 | 全部摺疊

結論

本文將告訴您,如何使用 Microsoft Office 2003、Microsoft Office XP Developer 和 Microsoft Office 2000 Developer 中的 Microsoft Visual Basic 來自動化 Microsoft PowerPoint。

其他相關資訊

您可以使用 PowerPoint 中的自動化,以程式設計方式列印、顯示投影片,並且執行大多數能夠以互動方式進行的操作。請依照下列步驟建置並執行 Visual Basic 自動化範例:
  1. 建立新的標準 EXE 專案。根據預設會建立 Form1。
  2. 將 CommandButton 加入預設表單。
  3. 按一下 [專案] 功能表中的 [參考],並加入 [Microsoft PowerPoint 8.0 物件程式庫][Microsoft Office 8.0 物件程式庫]。如果是 Office 2000,則為 9.0 版的型別程式庫。如果是 Office 2002,則為 10.0 版。如果是 Microsoft Office 2003,則為 11.0 版。
  4. 將下列程式碼加入您的表單程式碼視窗:
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    Private Sub Command1_Click()
        ' Start PowerPoint.
        Dim ppApp As PowerPoint.Application
        Set ppApp = CreateObject("Powerpoint.Application")
     
        ' Make it visible.
        ppApp.Visible = True
     
        ' Add a new presentation.
        Dim ppPres As PowerPoint.Presentation
        Set ppPres = ppApp.Presentations.Add(msoTrue)
     
        ' Add a new slide.
        Dim ppSlide1 As PowerPoint.Slide
        Set ppSlide1 = ppPres.Slides.Add(1, ppLayoutText)
     
        ' Add some text.
        ppSlide1.Shapes(1).TextFrame.TextRange.Text = "My first slide"
        ppSlide1.Shapes(2).TextFrame.TextRange.Text = "Automating Powerpoint is easy" & vbCr & "Using Visual Basic is fun!"
     
        ' Add another slide, with a chart.
        Dim ppSlide2 As PowerPoint.Slide
        Set ppSlide2 = ppPres.Slides.Add(2, ppLayoutTextAndChart)
        
        ' Add some text.
        ppSlide2.Shapes(1).TextFrame.TextRange.Text = "Slide 2's topic"
        ppSlide2.Shapes(2).TextFrame.TextRange.Text = "You can create and use charts in your Powerpoint slides!"
     
        ' Add a chart in the same location as the old one.
        Dim cTop As Double
        Dim cWidth As Double
        Dim cHeight As Double
        Dim cLeft As Double
        With ppSlide2.Shapes(3)
            cTop = .Top
            cWidth = .Width
            cHeight = .Height
            cLeft = .Left
            .Delete
        End With
        ppSlide2.Shapes.AddOLEObject cLeft, cTop, cWidth, cHeight, "MSGraph.Chart"
     
        ' Add another slide, with an organization chart.
        Dim ppSlide3 As PowerPoint.Slide
        Set ppSlide3 = ppPres.Slides.Add(3, ppLayoutOrgchart)
     
        ' Add some text.
    
        ppSlide3.Shapes(1).TextFrame.TextRange.Text = "The rest is only limited by your Imagination"
     
        ' Add an Org Chart in the same location as the old one.
        With ppSlide3.Shapes(2)
            cTop = .Top
            cWidth = .Width
            cHeight = .Height
            cLeft = .Left
            .Delete
        End With
        ppSlide3.Shapes.AddOLEObject cLeft, cTop, cWidth, cHeight, "OrgPlusWOPX.4" 'OrgPlusWOPX.4 
        ' is an object of the application Microsoft Organization Chart
    
     
        ' Setup slide show properties.
        With ppPres.Slides.Range.SlideShowTransition
            .EntryEffect = ppEffectRandom
            .AdvanceOnTime = msoTrue
            .AdvanceTime = 5 ' 5 seconds per slide
        End With
     
        ' Prepare and run the slide show.
        With ppPres.SlideShowSettings
            .ShowType = ppShowTypeKiosk
            .LoopUntilStopped = msoTrue
    
            .RangeType = ppShowAll
            .AdvanceMode = ppSlideShowUseSlideTimings
            .Run
        End With
     
        ' Sleep so user can watch the show.
        Sleep (15000)
     
        ' Clean up.
        ppApp.Quit
    
    End Sub
    					
  5. 執行專案。

?考

如需有關 Office 自動化的詳細資訊,請造訪 Microsoft Office Development 支援網站:
http://support.microsoft.com/ofd

屬性

文章編號: 222929 - 上次校閱: 2006年2月16日 - 版次: 5.1
這篇文章中的資訊適用於:
  • Microsoft Office Standard Edition 2003
  • Microsoft Office XP Developer Edition
  • Microsoft Office 2000 Developer Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition
  • Microsoft Office PowerPoint 2003
  • Microsoft PowerPoint 2002 Standard Edition
  • Microsoft PowerPoint 2000 Standard Edition
  • Microsoft PowerPoint 97 Standard Edition
關鍵字:?
kbhowto kbautomation KB222929
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。

提供意見

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com