如何透過 Visual Basic .NET 2002 或 Visual Basic .NET 2003,使用自動化功能建立和顯示 PowerPoint 2002 簡報或 Office PowerPoint 2003 簡報

文章翻譯 文章翻譯
文章編號: 303717 - 檢視此文章適用的產品。
如需本文的 Microsoft Visual C# .NET 版本,請參閱 303718
如需本文的 Microsoft Visual C++ .NET 版本,請參閱 308336
全部展開 | 全部摺疊

在此頁中

結論

本文將告訴您,如何透過 Microsoft Visual Basic .NET 2002 或 Visual Basic .NET 2003,使用自動化功能建立和顯示 Microsoft PowerPoint 2002 簡報或 Microsoft Office PowerPoint 2003 簡報。

其他相關資訊

為 Microsoft PowerPoint 2002 和 Microsoft Office PowerPoint 2003 建立自動化用戶端

  1. 啟動 Microsoft Visual Studio .NET 2002 或 Visual Studio .NET 2003。在 [檔案] 功能表上,按一下 [新增],然後按一下 [專案]。從 Visual Basic 專案類型中,選取 [Windows 應用程式]。根據預設會建立 Form1。
  2. 將參考新增至 [Microsoft PowerPoint Object Library][Microsoft Graph Object Library]。如果要執行這項操作,請依照下列步驟執行:
    1. 按一下 [專案] 功能表上的 [加入參考]
    2. [COM] 索引標籤上,找出 [Microsoft PowerPoint Object Library],然後按一下 [選取]。此外,找出 [Microsoft Graph Object Library],然後按一下 [選取]

      注意:Microsoft Office 2003 包括了「主要 Interop 組件」(PIA)。Microsoft Office XP 不包含 PIA,但是可以透過下載取得 PIA。 如需有關 Office XP PIA 的詳細資訊,請按一下下面的文件編號,檢視「Microsoft 知識庫」中的文件:
      328912 Microsoft Office XP primary interop assemblies (PIAs) are available for download
    3. 按一下 [加入參考] 對話方塊中的 [確定],以接受您的選擇。
  3. 選取 [檢視] 功能表上的 [工具箱],以顯示「工具箱」,並在 Form1 新增一個按鈕。
  4. 按兩下 [Button1]。表單的程式碼視窗隨即出現。
  5. 在程式碼視窗中,找出下列程式碼
        Private Sub Button1_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Button1.Click
    
        End Sub
    					
    以下列程式碼取代:
        Private Sub Button1_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Button1.Click
    
            Const sTemplate = _
               "C:\Program Files\Microsoft Office\Templates\Presentation Designs\Blends.pot"
            Const sPic = "C:\WINNT\Soap Bubbles.bmp"
    
            Dim oApp As PowerPoint.Application
            Dim oPres As PowerPoint.Presentation
            Dim oSlide As PowerPoint.Slide
            Dim bAssistantOn As Boolean
    
            'Start Powerpoint and make its window visible but minimized.
            oApp = New PowerPoint.Application()
            oApp.Visible = True
            oApp.WindowState = PowerPoint.PpWindowState.ppWindowMinimized
    
            'Create a new presentation based on the specified template.
            oPres = oApp.Presentations.Open(sTemplate, , , True)
    
            'Build Slide #1:
            'Add text to the slide, change the font and insert/position a 
            'picture on the first slide.
            oSlide = oPres.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitleOnly)
            With oSlide.Shapes.Item(1).TextFrame.TextRange
                .Text = "My Sample Presentation"
                .Font.Name = "Comic Sans MS"
                .Font.Size = 48
            End With
            oSlide.Shapes.AddPicture(sPic, False, True, 150, 150, 500, 350)
            oSlide = Nothing
    
            'Build Slide #2:
            'Add text to the slide title, format the text. Also add a chart to the
            'slide and change the chart type to a 3D pie chart.
            oSlide = oPres.Slides.Add(2, PowerPoint.PpSlideLayout.ppLayoutTitleOnly)
            With oSlide.Shapes.Item(1).TextFrame.TextRange
                .Text = "My Chart"
                .Font.Name = "Comic Sans MS"
                .Font.Size = 48
            End With
            Dim oChart As Graph.Chart
            oChart = oSlide.Shapes.AddOLEObject(150, 150, 480, 320, _
                        "MSGraph.Chart.8").OLEFormat.Object
            oChart.ChartType = Graph.XlChartType.xl3DPie
            oChart = Nothing
            oSlide = Nothing
    
            'Build Slide #3:
            'Add a text effect to the slide and apply shadows to the text effect.
            oSlide = oPres.Slides.Add(3, PowerPoint.PpSlideLayout.ppLayoutBlank)
            oSlide.FollowMasterBackground = False
            Dim oShape As PowerPoint.Shape
            oShape = oSlide.Shapes.AddTextEffect(Office.MsoPresetTextEffect.msoTextEffect27, _
                "The End", "Impact", 96, False, False, 230, 200)
            oShape.Shadow.ForeColor.SchemeColor = PowerPoint.PpColorSchemeIndex.ppForeground
            oShape.Shadow.Visible = True
            oShape.Shadow.OffsetX = 3
            oShape.Shadow.OffsetY = 3
            oShape = Nothing
            oSlide = Nothing
    
            'Modify the slide show transition settings for all 3 slides in
            'the presentation.
            Dim SlideIdx(3) As Integer
            SlideIdx(0) = 1
            SlideIdx(1) = 2
            SlideIdx(2) = 3
            With oPres.Slides.Range(SlideIdx).SlideShowTransition
                .AdvanceOnTime = True
                .AdvanceTime = 3
                .EntryEffect = PowerPoint.PpEntryEffect.ppEffectBoxOut
            End With
            Dim oSettings As PowerPoint.SlideShowSettings
            oSettings = oPres.SlideShowSettings
            oSettings.StartingSlide = 1
            oSettings.EndingSlide = 3
    
            'Prevent Office Assistant from displaying alert messages.
            bAssistantOn = oApp.Assistant.On
            oApp.Assistant.On = False
    
            'Run the slide show and wait for the slide show to end.
            oSettings.Run()
            Do While oApp.SlideShowWindows.Count >= 1
                System.Windows.Forms.Application.DoEvents()
            Loop
            oSettings = Nothing
    
            'Reenable Office Assisant, if it was on.
            If bAssistantOn Then
                oApp.Assistant.On = True
                oApp.Assistant.Visible = False
            End If
    
            'Close the presentation without saving changes and quit PowerPoint.
            oPres.Saved = True
            oPres.Close()
            oPres = Nothing
            oApp.Quit()
            oApp = Nothing
            GC.Collect()
        End Sub
    					
    注意 在此程式碼中,sTemplatesPic 常數分別代表 PowerPoint 範本和圖片的完整路徑和檔案名稱。請視需要修改這些路徑,以便使用系統上已安裝的範本或圖片。
  6. 將下列程式碼新增至 Form1.vb 的頂端:
    Imports Office = Microsoft.Office.Core
    Imports Graph = Microsoft.Office.Interop.Graph
    					
  7. 按下 F5 以建立並執行程式。
  8. 按一下表單上的 [Button1],以建立並顯示 PowerPoint 簡報。

?考

如需詳細資訊,請參閱下列 Microsoft Developer Network (MSDN) 網站:
http://msdn.microsoft.com/library/en-us/odc_2003_ta/html/odc_landvsto03_tr.asp
如需有關 PowerPoint 自動化的詳細資訊,請按一下下面的文件編號,檢視「Microsoft 知識庫」中的文件:
180616 How to use MFC to create and show a PowerPoint presentation
222929 如何使用 Office 2003、Office XP Developer 和 Office 2000 Developer 中的 Visual Basic 來自動化 PowerPoint

屬性

文章編號: 303717 - 上次校閱: 2007年1月17日 - 版次: 8.2
這篇文章中的資訊適用於:
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Office PowerPoint 2003
  • Microsoft PowerPoint 2002 Standard Edition
  • Microsoft Visual Studio .NET 2002 Academic Edition
  • Microsoft Visual Studio .NET 2002 Enterprise Architect
  • Microsoft Visual Studio .NET 2002 Enterprise Developer
  • Microsoft Visual Studio .NET 2003 Academic Edition
  • Microsoft Visual Studio .NET 2003 Enterprise Architect
  • Microsoft Visual Studio .NET 2003 Enterprise Developer
  • Microsoft Visual Studio .NET 2003 專業版
關鍵字:?
kbautomation kbhowto KB303717
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