How to use Automation to create and to show a PowerPoint 2002 presentation by using Visual C# .NET 2002 (303718)



The information in this article applies to:

  • Microsoft Visual C# .NET (2002)
  • Microsoft PowerPoint 2002
  • Microsoft Visual Studio .NET (2002), Academic Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Professional Edition

This article was previously published under Q303718
For a Microsoft Visual Basic .NET version of this article, see 303717.
For a Microsoft Visual C++ .NET version of this article, see 308336.

SUMMARY

This article describes how to use Automation to create and to show a Microsoft PowerPoint 2002 presentation by using Microsoft Visual C# .NET 2002.

MORE INFORMATION

Create an Automation client for PowerPoint 2002

  1. Start Visual Studio .NET 2002. On the File menu, click New and then click Project. Select Windows Application from the Visual C# Projects types. Form1 is created by default.
  2. Add a reference to the Microsoft PowerPoint Object Library and the Microsoft Graph Object Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. On the COM tab, locate the Microsoft PowerPoint Object Library and click Select. Also locate theMicrosoft Graph Object Library and click Select.

      Note Microsoft Office XP does not include PIAs, but the PIAs may be downloaded. For more information about Office XP PIAs, click the following article number to view the article in the Microsoft Knowledge Base:

      328912 Microsoft Office XP primary interop assemblies (PIAs) are available for download

    3. Click OK in the Add References dialog box to accept your selections.
  3. On the View menu, select Toolbox to display the Toolbox and add a button to Form1.
  4. Double-click Button1. The code window for the Form appears.
  5. In the code window, replace the following code
    private void button1_Click(object sender, System.EventArgs e)
    {
    }
    					
    with:
    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();
    } 
    					
    NOTE: In this code, the sTemplate and sPic constants represent the full path and file name to a PowerPoint template and a picture, respectively. Modify these paths as needed to use a template or picture that is installed on your system.
  6. Scroll to the top of the code window. Add the following lines of code to the end of the list of using directives:
    using Microsoft.Office.Core;
    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
    using Graph = Microsoft.Office.Interop.Graph;
    using System.Runtime.InteropServices;
    					
  7. Press F5 to build and then run the program.
  8. Click Button1 on the form to create and then show a PowerPoint presentation.

REFERENCES

For more information, see the following Microsoft Web site: For more information about PowerPoint Automation, click the following article numbers to view the articles in the Microsoft Knowledge Base:

180616 How to use MFC to create and show a PowerPoint presentation

222929 How to automate PowerPoint by using Visual Basic in Office 2003, in Office XP Developer, and in Office 2000 Developer


Modification Type:MajorLast Reviewed:9/19/2005
Keywords:kbAutomation kbhowto KB303718 kbAudDeveloper