HOWTO:在 Visual C# .NET 中使用自动化创建并显示 PowerPoint 演示文稿
Posted on 2010-03-15 21:54 剑灵 阅读(457) 评论(0) 编辑 收藏 举报为 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 (http://support.microsoft.com/kb/328912/EN-US/ ) INFO:Microsoft Office XP PIA 可供下载 - 在添加引用对话框中单击确定以接受您的选择。
- 在视图菜单上,选择工具箱以显示工具箱,然后向 Form1 添加一个按钮。
- 双击 Button1。将出现该窗体的代码窗口。
- 在代码窗口中,将以下代码1 private void button1_Click(object sender, System.EventArgs e)
2 {
3
4 }
替换为:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 private void button1_Click(object sender, System.EventArgs e)
2 {
3 ShowPresentation();
4 GC.Collect();
5 }
6
7 private void ShowPresentation()
8 {
9 String strTemplate, strPic;
10 strTemplate =
11 "C:\\Program Files\\Microsoft Office\\Templates\\Presentation Designs\\Blends.pot";
12 strPic = "C:\\Windows\\Blue Lace 16.bmp";
13 bool bAssistantOn;
14
15 PowerPoint.Application objApp;
16 PowerPoint.Presentations objPresSet;
17 PowerPoint._Presentation objPres;
18 PowerPoint.Slides objSlides;
19 PowerPoint._Slide objSlide;
20 PowerPoint.TextRange objTextRng;
21 PowerPoint.Shapes objShapes;
22 PowerPoint.Shape objShape;
23 PowerPoint.SlideShowWindows objSSWs;
24 PowerPoint.SlideShowTransition objSST;
25 PowerPoint.SlideShowSettings objSSS;
26 PowerPoint.SlideRange objSldRng;
27 Graph.Chart objChart;
28
29 //Create a new presentation based on a template.
30 objApp = new PowerPoint.Application();
31 objApp.Visible = MsoTriState.msoTrue;
32 objPresSet = objApp.Presentations;
33 objPres = objPresSet.Open(strTemplate,
34 MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);
35 objSlides = objPres.Slides;
36
37 //Build Slide #1:
38 //Add text to the slide, change the font and insert/position a
39 //picture on the first slide.
40 objSlide = objSlides.Add(1,PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
41 objTextRng = objSlide.Shapes[1].TextFrame.TextRange;
42 objTextRng.Text = "My Sample Presentation";
43 objTextRng.Font.Name = "Comic Sans MS";
44 objTextRng.Font.Size = 48;
45 objSlide.Shapes.AddPicture(strPic, MsoTriState.msoFalse, MsoTriState.msoTrue,
46 150, 150, 500, 350);
47
48 //Build Slide #2:
49 //Add text to the slide title, format the text. Also add a chart to the
50 //slide and change the chart type to a 3D pie chart.
51 objSlide = objSlides.Add(2, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
52 objTextRng = objSlide.Shapes[1].TextFrame.TextRange;
53 objTextRng.Text = "My Chart";
54 objTextRng.Font.Name = "Comic Sans MS";
55 objTextRng.Font.Size = 48;
56 objChart = (Graph.Chart) objSlide.Shapes.AddOLEObject(150,150,480,320,
57 "MSGraph.Chart.8", "", MsoTriState.msoFalse, "", 0, "",
58 MsoTriState.msoFalse).OLEFormat.Object;
59 objChart.ChartType = Graph.XlChartType.xl3DPie;
60 objChart.Legend.Position=Graph.XlLegendPosition.xlLegendPositionBottom;
61 objChart.HasTitle = true;
62 objChart.ChartTitle.Text = "Here it is...";
63
64 //Build Slide #3:
65 //Change the background color of this slide only. Add a text effect to the slide
66 //and apply various color schemes and shadows to the text effect.
67 objSlide = objSlides.Add(3, PowerPoint.PpSlideLayout.ppLayoutBlank);
68 objSlide.FollowMasterBackground = MsoTriState.msoFalse;
69 objShapes = objSlide.Shapes;
70 objShape = objShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect27,
71 "The End", "Impact", 96, MsoTriState.msoFalse, MsoTriState.msoFalse, 230, 200);
72
73 //Modify the slide show transition settings for all 3 slides in
74 //the presentation.
75 int[] SlideIdx = new int[3];
76 for(int i=0;i<3;i++) SlideIdx[i]=i+1;
77 objSldRng = objSlides.Range(SlideIdx);
78 objSST = objSldRng.SlideShowTransition;
79 objSST.AdvanceOnTime = MsoTriState.msoTrue;
80 objSST.AdvanceTime = 3;
81 objSST.EntryEffect = PowerPoint.PpEntryEffect.ppEffectBoxOut;
82
83 //Prevent Office Assistant from displaying alert messages:
84 bAssistantOn = objApp.Assistant.On;
85 objApp.Assistant.On = false;
86
87 //Run the Slide show from slides 1 thru 3.
88 objSSS = objPres.SlideShowSettings;
89 objSSS.StartingSlide = 1;
90 objSSS.EndingSlide = 3;
91 objSSS.Run();
92
93 //Wait for the slide show to end.
94 objSSWs = objApp.SlideShowWindows;
95 while(objSSWs.Count>=1) System.Threading.Thread.Sleep(100);
96
97 //Reenable Office Assisant, if it was on:
98 if(bAssistantOn)
99 {
100 objApp.Assistant.On = true;
101 objApp.Assistant.Visible = false;
102 }
103
104 //Close the presentation without saving changes and quit PowerPoint.
105 objPres.Close();
106 objApp.Quit();
107 }
注意:在上述代码中,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://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx (http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx)
有关 PowerPoint 自动化的其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:
http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx (http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx)
180616 (http://support.microsoft.com/kb/180616/EN-US/ ) HOWTO:Use MFC to Create and Show a PowerPoint Presentation
222929 (http://support.microsoft.com/kb/222929/EN-US/ ) HOWTO:Automate PowerPoint Using Visual Basic