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;
				}
				else
				{
					break;
				}
			}
			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;
				}
			}
			nLine++;
		}
	}

	// 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:
if(bAssistantOn)
{
	objApp.Assistant.On = true;
	objApp.Assistant.Visible = false;
}
Share