About Me!

Web Developer , Web Designer , Freelancer , Land Lord Visit my online portfolio Visit my Portfolio

Friday, July 23, 2010

Power Point Automation Using C#

1. Start Visual Studio .NET 2008. 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:

a. On the Project menu, click Add Reference.

b. On the COM tab, locate the Microsoft PowerPoint Object Library and click Select. Also locate theMicrosoft Graph Object Libraryand 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 (http/support.microsoft.com/kb/328912/ ) Microsoft Office XP primary interop assemblies (PIAs) are available for download

c. Click OK in the Add References dialog box to accept your selections.


3. Create a Form or Class File, In my part i create a class file.

with the following code.


public class PowerPointUtility

{


}


4. I Create a Method.

with following code.


public void ShowMyPresentation()

{


}


5. Inside my method I added a arguments and some code.

the data will coming from the database.

with following code.


public void ShowMyPresentation(System.Data.DataTable PowerPointTable)

{


PowerPoint.Application PPApp = new Microsoft.Office.Interop.PowerPoint.Application();


//this code will set the mircosoft office power point visible.

PPApp.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;


PowerPoint.Slides objSlides;

PowerPoint._Slide objSlide;

PPApp.Presentations.Add(MsoTriState.msoTrue);

objSlides = PPApp.Presentations[1].Slides;

objSlide = objSlides.Add(1, PowerPoint.PpSlideLayout.ppLayoutBlank);

objSlide.BackgroundStyle = MsoBackgroundStyleIndex.msoBackgroundStylePreset12;

//add a table to the slide.

PowerPoint.Shape tableFirme = objSlide.Shapes.AddTable(15, 4, 24, 20, 670, 500); ;

for (int j = 0; j <= PowerPointTable.Rows.Count/15; j++)

{

if (PowerPointTable.Rows.Count / 15 >= 1)

{

objSlide = objSlides.Add(j + 2, PowerPoint.PpSlideLayout.ppLayoutBlank);

objSlide.BackgroundStyle = MsoBackgroundStyleIndex.msoBackgroundStylePreset12;

}

tableFirme.Table.Cell(1, 1).Shape.TextFrame.TextRange.Text = "Rank";

tableFirme.Table.Cell(1, 1).Shape.TextFrame.TextRange.Font.Bold = MsoTriState.msoTrue;

tableFirme.Table.Cell(1, 1).Shape.TextFrame.TextRange.Font.Size = 25;

tableFirme.Table.Cell(1, 1).Shape.TextFrame.TextRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignCenter;

tableFirme.Table.Cell(1, 2).Shape.TextFrame.TextRange.Text = "Full Name";

tableFirme.Table.Cell(1, 2).Shape.TextFrame.TextRange.Font.Bold = MsoTriState.msoTrue;

tableFirme.Table.Cell(1, 2).Shape.TextFrame.TextRange.Font.Size = 25;

tableFirme.Table.Cell(1, 2).Shape.TextFrame.TextRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignCenter;

tableFirme.Table.Cell(1, 3).Shape.TextFrame.TextRange.Text = "Alias";

tableFirme.Table.Cell(1, 3).Shape.TextFrame.TextRange.Font.Bold = MsoTriState.msoTrue;

tableFirme.Table.Cell(1, 3).Shape.TextFrame.TextRange.Font.Size = 25;

tableFirme.Table.Cell(1, 3).Shape.TextFrame.TextRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignCenter;

tableFirme.Table.Cell(1, 4).Shape.TextFrame.TextRange.Text = "Points";

tableFirme.Table.Cell(1, 4).Shape.TextFrame.TextRange.Font.Bold = MsoTriState.msoTrue;

tableFirme.Table.Cell(1, 4).Shape.TextFrame.TextRange.Font.Size = 25;

tableFirme.Table.Cell(1, 4).Shape.TextFrame.TextRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignCenter;

}

for (int i = 0; i <= PowerPointTable.Rows.Count-1; i++)

{

//------------Data-----------------

int intRank = i + 1;

tableFirme.Table.Cell(i + 2, 1).Shape.TextFrame.TextRange.Text = intRank.ToString();

tableFirme.Table.Cell(i + 2, 1).Shape.TextFrame.TextRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignCenter;

tableFirme.Table.Cell(i + 2, 2).Shape.TextFrame.TextRange.Text = PowerPointTable.Rows[i]["fullname"].ToString();

tableFirme.Table.Cell(i + 2 , 2).Shape.TextFrame.TextRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignLeft;

tableFirme.Table.Cell(i + 2, 3).Shape.TextFrame.TextRange.Text = PowerPointTable.Rows[i]["alias"].ToString();

tableFirme.Table.Cell(i + 2, 3).Shape.TextFrame.TextRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignLeft;

tableFirme.Table.Cell(i + 2, 4).Shape.TextFrame.TextRange.Text = PowerPointTable.Rows[i]["Points"].ToString();

tableFirme.Table.Cell(i + 2, 4).Shape.TextFrame.TextRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignLeft;

}


}


OUTPUT WILL THIS:





oDesk Certified CSS 2.0 Designer

3 comments:

  1. You can do slides automation using c# with Aspose.Slides for .NET Library and you can use many of their sample codes to add in your application.

    ReplyDelete
  2. Alternativelt,Spire.Presentation for .NET is a
    professional PowerPoint compatible component that enables developers to create, read, write, and modify PowerPoint documents from any .NET(C#, VB.NET, ASP.NET) platform. As an independent PowerPoint .NET component, Spire.Presentation for .NET doesn't need Microsoft PowerPoint installed on the machine.

    ReplyDelete
  3. this example don't use any third party software. thank you

    ReplyDelete