Home > C/C++/C# > Create PowerPoint slide in C#

Create PowerPoint slide in C#

There are situations we need control MS Office components in our program, for example, manipulate data in Excel workbook, generate PowerPoint slides, intracte with Access database etc. In this article, I will provide detail information about how to control PowerPoint in C# program.

First of all, how can use use MS Office components in our standalone program in general? Since MS Office provide Office Primary Interop Assemblies, for example, Microsoft.Office.Interop.Excel and Microsoft.Office.Interop.PowerPoint. In you program that intends to control MS Office components, you have to add reference to these interop assemblies. To contol MS PowerPoint, you need add a reference pointing to Microsoft.Office.Interop.PowerPoint. You can use Add Reference function the GUI environment to achieve this.

The following code snippets provide some methods to control PowerPoint, mainly create new slides automatically. It is a method in a large program and is not a generic procedure. You can use ideas and methods presented in the code to generate yours.

Code snippet

private void CreatePresentation()
	string BasePath = Path.GetDirectoryName(myFilename) + "\\";
	string BaseFilename = "";

	String strTemplate = BasePath + "Calendar.ppt";
	//bool bAssistantOn;

	PowerPoint.Application objApp;
	PowerPoint.Presentations objPresSet;
	PowerPoint._Presentation objPres;
	PowerPoint.Slides objSlides;
	PowerPoint._Slide objSlide;
	PowerPoint.TextRange objTextRng;

	//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 = "Remote sensing calendar";
	objTextRng.Font.Name = "Comic Sans MS";
	objTextRng.Font.Size = 48;

	// insert a PowerPoint table
	// use data from a file
	PowerPoint.Table objTable = null;
	using (StreamReader sr= new StreamReader("summary.csv"))
		int nLine = 1;
		int nStartLine = 0;
		while (!sr.EndOfStream)
			string line = sr.ReadLine();
			if (line.Trim() == "")
				if (nStartLine == 0) 
					nStartLine = nLine;
					objTable= objSlide.Shapes.AddTable(nStartLine-1,9,36,138,648,294).Table;
			if ((nStartLine > 0) && (nLine>nStartLine))
				string [] cells = line.Split(',');
				for (int i=1; i<=cells.Length; i++)
					int k = i;
					if (i>1) k = i+2;
					objTable.Cell(nLine-nStartLine,k).Shape.TextFrame.TextRange.Text = cells[i-1];
					objTable.Cell(nLine-nStartLine,k).Shape.TextFrame.TextRange.Font.Size = 12;

	// build more slides
	// add pictures to the slides
	for (int i=0; i=1) System.Threading.Thread.Sleep(100);

//Reenable Office Assisant, if it was on:
	objApp.Assistant.On = true;
	objApp.Assistant.Visible = false;
  1. Dirnov
    August 24th, 2009 at 19:49 | #1

    Interesting, I`ll quote it on my site later.
    Have a nice day

  2. BlackNight
    August 30th, 2009 at 15:42 | #2

    Thank U for that, it’s very useful.
    BTW has anyone idea why do I get an exception “Attempted to read or write protected memory. This is often an indication that other memory is corrupt.”
    by the following line, in a place of second parameter:
    objPres = objPresSet.Open(strTemplate,
    MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);

  3. Chrizstone
    October 21st, 2009 at 09:10 | #3

    Hi, how can i specify that the cell weight is automatic? If i have a long string the cell should be bigger automatically!


  4. November 2nd, 2009 at 05:17 | #4

    I appriciate this article. I think that PowerPoint is the unmatchable office tool for business, shcool teaching and studing. We need to make full use of PowerPoint. I read this article with pleasure.

  1. October 10th, 2012 at 18:23 | #1
You must be logged in to post a comment.