Introduction
This is a simple way of creating a Microsoft PowerPoint document through the C# language.
Background
You need a minimum understanding of C# language buzz.
Using the Code
Create a new project, for simplicity, create a Windows application. Go ahead and right click on References in the Solution Explorer, and select Add Reference… When the Add Reference window comes up, select the COM tab. This will list all Component names which are available on your machine. Since we are going to use Microsoft PowerPoint , you will scroll down until you find: Microsoft Power point 11.0 Object Library. You can even do the same thing with PIAs. In the code, I have used PIA.
I have created a very simple wrapper class for the PowerPoint object model. The code will be given below. This project also has the error provider functionality to make sure that the user will enter values for all the fields.
Create button click event. Here, we check whether all fields are filled and then we call the CreateFile()
function in the PowerPoint wrapper class.
private void btnCreate_Click(object sender, System.EventArgs e)
{
bool filled = true;
if ( ( txtName.Text).Trim().Length.Equals(0) )
{
errorProvider.SetError(txtName,"cannot be empty" );
filled = false;
}
if( ( txtAge.Text ).Trim().Length.Equals(0) )
{
errorProvider.SetError( txtAge,"cannot be empty" );
filled = false;
}
if( ( txtDesignation.Text ).Trim().Length.Equals(0) )
{
errorProvider.SetError( txtDesignation,"cannot be empty" );
filled = false;
}
if( ( txtPlace.Text ).Trim().Length.Equals(0) )
{
errorProvider.SetError( txtPlace,"cannot be empty" );
filled = false;
}
if( ( txtEmail.Text ).Trim().Length.Equals(0) )
{
errorProvider.SetError( txtEmail,"cannot be empty" );
filled = false;
}
if( ( txtCompany.Text ).Trim().Length.Equals(0) )
{
errorProvider.SetError( txtCompany,"cannot be empty" );
filled = false;
}
if ( filled == true )
{
PPTAuto ppt = new PPTAuto();
StringCollection array = new StringCollection ();
array.Insert(0,txtName.Text);
array.Insert(1,txtAge.Text);
array.Insert(2,txtDesignation.Text);
array.Insert(3,txtCompany.Text);
array.Insert(4,txtPlace.Text);
array.Insert(5,txtEmail.Text);
ppt.CreateFile(array);
}
}
PowerPoint File Creation Code
public void CreateFile(StringCollection array)
{
String strTemplate;
strTemplate = "C:\\Program Files\\Microsoft Office\\Templates\\
Presentation Designs\\Glass Layers.pot";
objApp = new PowerPoint.Application();
objApp.Visible = MsoTriState.msoTrue;
objPresSet = objApp.Presentations;
objPres = objPresSet.Open(strTemplate,
MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);
objSlides = objPres.Slides;
objSlide = objSlides.Add(1,PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
objSlide.Shapes.AddTextbox
(MsoTextOrientation.msoTextOrientationHorizontal,100,100,500,80);
objSlide.Shapes.AddTextbox
(MsoTextOrientation.msoTextOrientationHorizontal,100,150,500,80);
objSlide.Shapes.AddTextbox
(MsoTextOrientation.msoTextOrientationHorizontal,100,200,500,80);
objSlide.Shapes.AddTextbox
(MsoTextOrientation.msoTextOrientationHorizontal,100,250,500,80);
objSlide.Shapes.AddTextbox
(MsoTextOrientation.msoTextOrientationHorizontal,100,300,500,80);
objSlide.Shapes.AddTextbox
(MsoTextOrientation.msoTextOrientationHorizontal,100,350,500,80);
objSlide.Shapes.AddTextbox
(MsoTextOrientation.msoTextOrientationHorizontal,100,400,500,80);
objTextRng = objSlide.Shapes[1].TextFrame.TextRange;
objTextRng.Text = "These are my details";
objTextRng.Font.Name = "Arial";
objTextRng.Font.Size = 20;
objTextRng = objSlide.Shapes[2].TextFrame.TextRange;
objTextRng.Text =array[0] ;
objTextRng.Font.Name = "Arial";
objTextRng.Font.Size = 20;
objTextRng = objSlide.Shapes[3].TextFrame.TextRange;
objTextRng.Text =array[1] ;
objTextRng.Font.Name = "Arial";
objTextRng.Font.Size = 20;
objTextRng = objSlide.Shapes[4].TextFrame.TextRange;
objTextRng.Text =array[2] ;
objTextRng.Font.Name = "Arial";
objTextRng.Font.Size = 20;
objTextRng = objSlide.Shapes[5].TextFrame.TextRange;
objTextRng.Text =array[3] ;
objTextRng.Font.Name = "Arial";
objTextRng.Font.Size = 20;
objTextRng = objSlide.Shapes[6].TextFrame.TextRange;
objTextRng.Text =array[4] ;
objTextRng.Font.Name = "Arial";
objTextRng.Font.Size = 20;
objTextRng = objSlide.Shapes[7].TextFrame.TextRange;
objTextRng.Text =array[5] ;
objTextRng.Font.Name = "Arial";
objTextRng.Font.Size = 20;
}
Conclusion
The interop assemblies provide a lot of options for working with PowerPoint data, both reading and writing. I hope this article gives you a head start in controlling PowerPoint from .NET and C#. Some experiment with the object model will help you to get that hands on.
History