简体   繁体   中英

How can I stop a ppt presentation from opening automatically while creating it programmatically using Microsoft.interop?

I am using office.interop.powerpoint to create a powerpoint presentation. The issue that I am facing right now is that this powerpoint presentation that I am creting first opens in foreground and then the slides are added later on as the function execution proceeds and finally the file is saved.

Is there any way that I can stop this Powerpoint presentation from opening before all slide gets added into it. Below is the sample code that I am using:

Application pptApplication = new Application();

Microsoft.Office.Interop.PowerPoint.Slides slides;
Microsoft.Office.Interop.PowerPoint._Slide slide;
Microsoft.Office.Interop.PowerPoint.TextRange objText;

// Create the Presentation File
Presentation pptPresentation = pptApplication.Presentations.Add(MsoTriState.msoTrue);

Microsoft.Office.Interop.PowerPoint.CustomLayout customLayout = pptPresentation.SlideMaster.CustomLayouts[Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutText];

for(int i=0;i<4;i++){
        // Create new Slide
        slides = pptPresentation.Slides;
        slide = slides.AddSlide(1, customLayout);
        
        objText = slide.Shapes[2].TextFrame.TextRange;
        objText.Text = "Content goes here\nYou can add text\nItem 3";
        
        Microsoft.Office.Interop.PowerPoint.Shape shape = slide.Shapes[2];
        slide.Shapes.AddPicture(pictureFileName,Microsoft.Office.Core.MsoTriState.msoFalse,Microsoft.Office.Core.MsoTriState.msoTrue,shape.Left, shape.Top, shape.Width, shape.Height);
    

}

pptPresentation.SaveAs(@"c:\temp\fppt.pptx", Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoTrue);
//pptPresentation.Close();
//pptApplication.Quit();

As the code reaches to line with code Presentation pptPresentation = pptApplication.Presentations.Add(MsoTriState.msoTrue); , powerpoint application popsup opening this newly creted PPT and then all slides are added one by one to it.

Did you try setting the Presentations.Add(WithWindow) parameter to msoFalse ?

Presentation pptPresentation = pptApplication.Presentations.Add(MsoTriState.msoFalse);

Setting it to msoTrue creates the presentation in a visible window. Refer here .

You can also change the application visibility to false.

pptApplication.Visible = MsoTriState.msoFalse;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM