概要
本文逐步介绍如何通过 Visual C# .NET 使 PowerPoint 自动运行以创建并显示 PowerPoint 演示文稿。更多信息
为 PowerPoint 创建自动化客户端
- 启动 Microsoft Visual Studio .NET。在文件菜单上,单击新建,然后单击项目。从 Visual C# 项目类型中选择 Windows 应用程序。默认情况下会创建 Form1。
- 添加对 Microsoft PowerPoint 对象库和 Microsoft Graph 对象库的引用。为此,请按照下列步骤操作:
- 在项目菜单上,单击添加引用。
- 在 COM 选项卡上,找到 Microsoft PowerPoint 对象库,然后单击选择。还应找到 Microsoft Graph 对象库,然后单击选择。
注意:Microsoft Office XP 不包含 PIA,但您可以下载 PIA。 有关 Office XP PIA 的其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:328912 INFO:Microsoft Office XP PIA 可供下载
- 在添加引用对话框中单击确定以接受您的选择。
- 在视图菜单上,选择工具箱以显示工具箱,然后向 Form1 添加一个按钮。
- 双击 Button1。将出现该窗体的代码窗口。
- 在代码窗口中,将以下代码
private void button1_Click(object sender, System.EventArgs e) { }
替换为:private void button1_Click(object sender, System.EventArgs e) { ShowPresentation(); GC.Collect(); } private void ShowPresentation() { String strTemplate, strPic; strTemplate = "C://Program Files//Microsoft Office//Templates//Presentation Designs//Blends.pot"; strPic = "C://Windows//Blue Lace 16.bmp"; bool bAssistantOn; PowerPoint.Application objApp; PowerPoint.Presentations objPresSet; PowerPoint._Presentation objPres; PowerPoint.Slides objSlides; PowerPoint._Slide objSlide; PowerPoint.TextRange objTextRng; PowerPoint.Shapes objShapes; PowerPoint.Shape objShape; PowerPoint.SlideShowWindows objSSWs; PowerPoint.SlideShowTransition objSST; PowerPoint.SlideShowSettings objSSS; PowerPoint.SlideRange objSldRng; Graph.Chart objChart; //Create a new presentation based on a template. objApp = new PowerPoint.Application(); objApp.Visible = MsoTriState.msoTrue; objPresSet = objApp.Presentations; objPres = objPresSet.Open(strTemplate, MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue); objSlides = objPres.Slides; //Build Slide #1: //Add text to the slide, change the font and insert/position a //picture on the first slide. objSlide = objSlides.Add(1,PowerPoint.PpSlideLayout.ppLayoutTitleOnly); objTextRng = objSlide.Shapes[1].TextFrame.TextRange; objTextRng.Text = "My Sample Presentation"; objTextRng.Font.Name = "Comic Sans MS"; objTextRng.Font.Size = 48; objSlide.Shapes.AddPicture(strPic, MsoTriState.msoFalse, MsoTriState.msoTrue, 150, 150, 500, 350); //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. objSlide = objSlides.Add(2, PowerPoint.PpSlideLayout.ppLayoutTitleOnly); objTextRng = objSlide.Shapes[1].TextFrame.TextRange; objTextRng.Text = "My Chart"; objTextRng.Font.Name = "Comic Sans MS"; objTextRng.Font.Size = 48; objChart = (Graph.Chart) objSlide.Shapes.AddOLEObject(150,150,480,320, "MSGraph.Chart.8", "", MsoTriState.msoFalse, "", 0, "", MsoTriState.msoFalse).OLEFormat.Object; objChart.ChartType = Graph.XlChartType.xl3DPie; objChart.Legend.Position=Graph.XlLegendPosition.xlLegendPositionBottom; objChart.HasTitle = true; objChart.ChartTitle.Text = "Here it is..."; //Build Slide #3: //Change the background color of this slide only. Add a text effect to the slide //and apply various color schemes and shadows to the text effect. objSlide = objSlides.Add(3, PowerPoint.PpSlideLayout.ppLayoutBlank); objSlide.FollowMasterBackground = MsoTriState.msoFalse; objShapes = objSlide.Shapes; objShape = objShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect27, "The End", "Impact", 96, MsoTriState.msoFalse, MsoTriState.msoFalse, 230, 200); //Modify the slide show transition settings for all 3 slides in //the presentation. int[] SlideIdx = new int[3]; for(int i=0;i<3;i++) SlideIdx[i]=i+1; objSldRng = objSlides.Range(SlideIdx); objSST = objSldRng.SlideShowTransition; objSST.AdvanceOnTime = MsoTriState.msoTrue; objSST.AdvanceTime = 3; objSST.EntryEffect = PowerPoint.PpEntryEffect.ppEffectBoxOut; //Prevent Office Assistant from displaying alert messages: bAssistantOn = objApp.Assistant.On; objApp.Assistant.On = false; //Run the Slide show from slides 1 thru 3. objSSS = objPres.SlideShowSettings; objSSS.StartingSlide = 1; objSSS.EndingSlide = 3; objSSS.Run(); //Wait for the slide show to end. objSSWs = objApp.SlideShowWindows; while(objSSWs.Count>=1) System.Threading.Thread.Sleep(100); //Reenable Office Assisant, if it was on: if(bAssistantOn) { objApp.Assistant.On = true; objApp.Assistant.Visible = false; } //Close the presentation without saving changes and quit PowerPoint. objPres.Close(); objApp.Quit(); }
注意:在上述代码中,sTemplate 和 sPic 常量分别表示 PowerPoint 模板和图片的完整路径及文件名。按照需要修改这些路径以使用安装在您系统中的模板或图片。 - 滚动到代码窗口的顶部。将下面的代码行添加到 using 指令列表的末尾:
using Microsoft.Office.Core; using PowerPoint = Microsoft.Office.Interop.PowerPoint; using Graph = Microsoft.Office.Interop.Graph; using System.Runtime.InteropServices;
- 按 F5 键生成并运行该程序。
- 在窗体中单击 Button1 创建并显示 PowerPoint 演示文稿。
参考
有关更多信息,请访问下面的 Microsoft Web 站点:Microsoft Office Development with Visual Studio(使用 Visual Studio 进行 Microsoft Office 开发)
http://msdn.microsoft.com/library/en-us/dnoffdev/html/vsofficedev.asp
180616 HOWTO:Use MFC to Create and Show a PowerPoint Presentation
222929 HOWTO:Automate PowerPoint Using Visual Basic
这篇文章中的信息适用于:
- Microsoft Visual C# .NET (2002)
- Microsoft PowerPoint 2002
最近更新: | 2004-2-13 (6.0) |
关键字: | kbAutomation kbhowto KB303718 kbAudDeveloper |