Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving a powerpoint file using C#

Tags:

c#

powerpoint

I just need to create a .pptx file with 1 dummy slide using C# and save it to the current directory. Can anyone tell me how to do this?

So far, I have this code to create a Powerpoint presentation:

Microsoft.Office.Interop.PowerPoint.Application obj = new Application();
obj.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
like image 597
user1221572 Avatar asked Nov 04 '22 22:11

user1221572


2 Answers

This code snippet creates a new presentation:

    private void DpStartPowerPoint()
{
    // Create the reference variables
    PowerPoint.Application ppApplication = null;
    PowerPoint.Presentations ppPresentations = null;
    PowerPoint.Presentation ppPresentation = null;

    // Instantiate the PowerPoint application
    ppApplication = new PowerPoint.Application();

    // Create a presentation collection holder
    ppPresentations = ppApplication.Presentations;

    // Create an actual (blank) presentation
    ppPresentation = ppPresentations.Add(Office.MsoTriState.msoTrue);

    // Activate the PowerPoint application
    ppApplication.Activate();
}

And this code snippet saves it:

    // Assign a filename under which to save the presentation
string myFileName = "myPresentation";

// Save the presentation unconditionally
ppPresentation.Save();

// Save the presentation as a PPTX
ppPresentation.SaveAs(myFileName,
                      PowerPoint.PpSaveAsFileType.ppSaveAsDefault, 
                      Office.MsoTriState.msoTrue);

// Save the presentation as a PDF
ppPresentation.SaveAs(myFileName,
                      PowerPoint.PpSaveAsFileType.ppSaveAsPDF, 
                      Office.MsoTriState.msoTrue);

// Save a copy of the presentation
ppPresentation.SaveCopyAs(“Copy of “ + myFileName,
                          PowerPoint.PpSaveAsFileType.ppSaveAsDefault, 
                          Office.MsoTriState.msoTrue);

See this page for references on other powerpoint automation capabilities.

like image 91
David Avatar answered Nov 09 '22 23:11

David


The following resources include how to save file and also many other code samples on how to manipulate Ms - Power Point presentation files using C#:

http://code.msdn.microsoft.com/office/CSAutomatePowerPoint-b312d416

http://www.eggheadcafe.com/community/csharp/2/10068596/create-ppt-slides-through-cnet.aspx

Hope this helps

Edit:

The following includes details about adding references:

http://support.microsoft.com/kb/303718

like image 21
Dulini Atapattu Avatar answered Nov 09 '22 23:11

Dulini Atapattu