private void CreateNewPresentation() { PowerPoint_App = new Ppt.Application(); multi_presentations = PowerPoint_App.Presentations; presentation = multi_presentations.Add(); layout = Ppt.PpSlideLayout.ppLayoutText; presentation.Slides.AddSlide(1, presentation.SlideMaster.CustomLayouts[layout]); }
private void InitializePowerpointObjects() { m_pptApplication = new PowerPoint.Application(); m_pptPresCollection = m_pptApplication.Presentations; // Argument to choose whether the ppt is visible or not m_pptPres = m_pptPresCollection.Add(Office.MsoTriState.msoFalse); m_pptSlides = m_pptPres.Slides; }
// //********************************************************************************************** // // Class TPPT // Part of : TDRFree // Function : Interacts with Microsoft Powerpoint to make a presentation // Author : Jan G. Wesseling // Date : April 9th, 2013 // //********************************************************************************************** // public TPPT() { MyApp = new PowerPoint.Application(); MyApp.Visible = MsoTriState.msoTrue; MyApp.WindowState = PowerPoint.PpWindowState.ppWindowMinimized; MyPresentations = MyApp.Presentations; MyPresentation = MyPresentations.Add(MsoTriState.msoFalse); MySlides = MyPresentation.Slides; }
private void cmdNewFile_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (FocusedRow.Data is IGroup group) { var baseName = "New file"; var name = baseName + ".pptx"; int cnt = 2; while (group.ContainsFile(name)) //TODO: AST: Limit number of iterations { name = Path.Combine(baseName + $" ({cnt++})") + ".pptx"; } var fullPath = Path.Combine(group.FullPath, name); PPT.Application app = null; PPT.Presentations pres = null; PPT.Presentation ppt = null; try { app = new PPT.Application(); pres = app.Presentations; ppt = pres.Add(MsoTriState.msoFalse); ppt.SaveAs(fullPath); } finally { ppt?.Close(); ppt.ReleaseCOM(); ppt = null; pres.ReleaseCOM(); pres = null; app = null; //TODO: AST: app is not released } var file = group.AddFile(name); var row = FocusedRow; var newRow = dtTree.AddTreeRow(row.ID, 4, Path.GetFileNameWithoutExtension(file.Name), file, false); //TODO: AST: Move it to builder var node = uxTree.FindNodeByKeyID(newRow.ID); if (node != null) { node.Selected = true; uxTree.ShowEditor(); } } }
private bool CreateNewPresentation() { SaveFileDialog sfd = new SaveFileDialog { Filter = powerpointformat }; try { if (sfd.ShowDialog() == DialogResult.OK) { file_path = sfd.FileName; } else { return(false); } } catch (SystemException exc) { MessageBox.Show(save_err_msg + sfd.FileName + ":\n\n" + exc); return(false); } finally { sfd.Dispose(); } presentation = presentation_list.Add(); layout = Ppt.PpSlideLayout.ppLayoutText; presentation.Slides.AddSlide(1, presentation.SlideMaster.CustomLayouts[layout]); ChangeButtons(); return(true); }
public void Open(string path) { application = new PowerPoint.Application(); application.WindowState = PowerPoint.PpWindowState.ppWindowNormal; application.DisplayAlerts = PowerPoint.PpAlertLevel.ppAlertsAll; application.Visible = MsoTriState.msoTrue; if (string.IsNullOrEmpty(path)) { presentationList = application.Presentations; pres = presentationList.Add(MsoTriState.msoTrue); oSlides = pres.Slides; pres.Application.Activate(); } else { if (string.IsNullOrEmpty(Path.GetDirectoryName(path))) { path = application.ActivePresentation.Path + "\\" + path; } pres = application.Presentations.Open(path); pres.Application.Activate(); } this.path = path; }
public static new int Convert(String inputFile, String outputFile, Hashtable options, ref List <PDFBookmark> bookmarks) { // Check for password protection if (Converter.IsPasswordProtected(inputFile)) { Console.WriteLine("Unable to open password protected file"); return((int)ExitCode.PasswordFailure); } Boolean running = (Boolean)options["noquit"]; try { Microsoft.Office.Interop.PowerPoint.Application app = null; Microsoft.Office.Interop.PowerPoint.Presentation activePresentation = null; Microsoft.Office.Interop.PowerPoint.Presentations presentations = null; try { try { app = (Microsoft.Office.Interop.PowerPoint.Application)Marshal.GetActiveObject("PowerPoint.Application"); } catch (System.Exception) { int tries = 10; app = new Microsoft.Office.Interop.PowerPoint.Application(); running = false; while (tries > 0) { try { // Try to set a property on the object app.DisplayAlerts = PpAlertLevel.ppAlertsNone; } catch (COMException) { // Decrement the number of tries and have a bit of a snooze tries--; Thread.Sleep(500); continue; } // Looks ok, so bail out of the loop break; } if (tries == 0) { Converter.releaseCOMObject(app); return((int)ExitCode.ApplicationError); } } MSCore.MsoTriState nowrite = (Boolean)options["readonly"] ? MSCore.MsoTriState.msoTrue : MSCore.MsoTriState.msoFalse; bool pdfa = (Boolean)options["pdfa"] ? true : false; if ((Boolean)options["hidden"]) { // Can't really hide the window, so at least minimise it app.WindowState = PpWindowState.ppWindowMinimized; } PpFixedFormatIntent quality = PpFixedFormatIntent.ppFixedFormatIntentScreen; if ((Boolean)options["print"]) { quality = PpFixedFormatIntent.ppFixedFormatIntentPrint; } if ((Boolean)options["screen"]) { quality = PpFixedFormatIntent.ppFixedFormatIntentScreen; } Boolean includeProps = !(Boolean)options["excludeprops"]; Boolean includeTags = !(Boolean)options["excludetags"]; app.FeatureInstall = MSCore.MsoFeatureInstall.msoFeatureInstallNone; app.DisplayDocumentInformationPanel = false; app.DisplayAlerts = PpAlertLevel.ppAlertsNone; app.Visible = MSCore.MsoTriState.msoTrue; app.AutomationSecurity = MSCore.MsoAutomationSecurity.msoAutomationSecurityLow; presentations = app.Presentations; activePresentation = presentations.Open2007(inputFile, nowrite, MSCore.MsoTriState.msoTrue, MSCore.MsoTriState.msoTrue, MSCore.MsoTriState.msoTrue); activePresentation.Final = false; // Sometimes, presentations can have restrictions on them that block // access to the object model (e.g. fonts containing restrictions). // If we attempt to access the object model and fail, then try a more // sneaky method of getting the presentation - create an empty presentation // and insert the slides from the original file. var fonts = activePresentation.Fonts; try { var fontCount = fonts.Count; } catch (System.Runtime.InteropServices.COMException) { Converter.releaseCOMObject(fonts); // This presentation looked read-only activePresentation.Close(); Converter.releaseCOMObject(activePresentation); // Create a new blank presentation and insert slides from the original activePresentation = presentations.Add(MSCore.MsoTriState.msoFalse); // This is only a band-aid - backgrounds won't come through activePresentation.Slides.InsertFromFile(inputFile, 0); } Converter.releaseCOMObject(fonts); activePresentation.ExportAsFixedFormat(outputFile, PpFixedFormatType.ppFixedFormatTypePDF, quality, MSCore.MsoTriState.msoFalse, PpPrintHandoutOrder.ppPrintHandoutVerticalFirst, PpPrintOutputType.ppPrintOutputSlides, MSCore.MsoTriState.msoFalse, null, PpPrintRangeType.ppPrintAll, "", includeProps, true, includeTags, true, pdfa, Type.Missing); // Determine if we need to make bookmarks if ((bool)options["bookmarks"]) { loadBookmarks(activePresentation, ref bookmarks); } activePresentation.Saved = MSCore.MsoTriState.msoTrue; activePresentation.Close(); return((int)ExitCode.Success); } catch (Exception e) { Console.WriteLine(e.Message); return((int)ExitCode.UnknownError); } finally { Converter.releaseCOMObject(activePresentation); Converter.releaseCOMObject(presentations); if (app != null && !running) { app.Quit(); } Converter.releaseCOMObject(app); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } } catch (System.Runtime.InteropServices.COMException e) { Console.WriteLine(e.Message); return((int)ExitCode.UnknownError); } }
public static void AutomatePowerPoint() { PowerPoint.Application oPowerPoint = null; PowerPoint.Presentations oPres = null; PowerPoint.Presentation oPre = null; PowerPoint.Slides oSlides = null; PowerPoint.Slide oSlide = null; PowerPoint.Shapes oShapes = null; PowerPoint.Shape oShape = null; PowerPoint.TextFrame oTxtFrame = null; PowerPoint.TextRange oTxtRange = null; try { // Create an instance of Microsoft PowerPoint and make it // invisible. oPowerPoint = new PowerPoint.Application(); // By default PowerPoint is invisible, till you make it visible. // oPowerPoint.Visible = Office.MsoTriState.msoFalse; // Create a new Presentation. oPres = oPowerPoint.Presentations; oPre = oPres.Add(Office.MsoTriState.msoTrue); Console.WriteLine("A new presentation is created"); // Insert a new Slide and add some text to it. Console.WriteLine("Insert a slide"); oSlides = oPre.Slides; oSlide = oSlides.Add(1, PowerPoint.PpSlideLayout.ppLayoutText); Console.WriteLine("Add some texts"); oShapes = oSlide.Shapes; oShape = oShapes[1]; oTxtFrame = oShape.TextFrame; oTxtRange = oTxtFrame.TextRange; oTxtRange.Text = "All-In-One Code Framework"; // Save the presentation as a pptx file and close it. Console.WriteLine("Save and close the presentation"); string fileName = Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location) + "\\Sample1.pptx"; oPre.SaveAs(fileName, PowerPoint.PpSaveAsFileType.ppSaveAsOpenXMLPresentation, Office.MsoTriState.msoTriStateMixed); oPre.Close(); // Quit the PowerPoint application. Console.WriteLine("Quit the PowerPoint application"); oPowerPoint.Quit(); } catch (Exception ex) { Console.WriteLine("Solution1.AutomatePowerPoint throws the error: {0}", ex.Message); } finally { // Clean up the unmanaged PowerPoint COM resources by explicitly // calling Marshal.FinalReleaseComObject on all accessor objects. // See http://support.microsoft.com/kb/317109. if (oTxtRange != null) { Marshal.FinalReleaseComObject(oTxtRange); oTxtRange = null; } if (oTxtFrame != null) { Marshal.FinalReleaseComObject(oTxtFrame); oTxtFrame = null; } if (oShape != null) { Marshal.FinalReleaseComObject(oShape); oShape = null; } if (oShapes != null) { Marshal.FinalReleaseComObject(oShapes); oShapes = null; } if (oSlide != null) { Marshal.FinalReleaseComObject(oSlide); oSlide = null; } if (oSlides != null) { Marshal.FinalReleaseComObject(oSlides); oSlides = null; } if (oPre != null) { Marshal.FinalReleaseComObject(oPre); oPre = null; } if (oPres != null) { Marshal.FinalReleaseComObject(oPres); oPres = null; } if (oPowerPoint != null) { Marshal.FinalReleaseComObject(oPowerPoint); oPowerPoint = null; } } }
//------------------------------------------------------------------------------- protected void InitializePPT() { application = (PowerPoint.ApplicationClass)Server.CreateObject("PowerPoint.Application"); presentations = application.Presentations; presentation = presentations.Add(Microsoft.Office.Core.MsoTriState.msoTrue); slides = presentation.Slides; }
private bool BuildPowerPointFile(List <string> names) { // open the base presentation // create the animations // save as ... to the destination folder PowerPoint.Application ppApplication = null; PowerPoint.Presentations ppPresentations = null; PowerPoint.Presentation ppPresentation = null; try { ppApplication = new PowerPoint.Application(); ppPresentations = ppApplication.Presentations; // to create a new presentation ppPresentation = ppPresentations.Add(MsoTriState.msoTrue); ppApplication.Activate(); SlideWidthCentre = (int)ppPresentation.PageSetup.SlideWidth / 2; SlideHeightCentre = (int)ppPresentation.PageSetup.SlideHeight / 2; ppPresentation.ApplyTemplate(Path.Combine(BaseSettingsFolder, TemplateFile)); AddTitleSlide(ppPresentation, "My trust", "2015"); AddAnimationNames(ppPresentation, names); //CentrePictures(ppPresentation); ppPresentation.SaveAs(Path.Combine(DestinationFolder, "AnimatedNames"), PowerPoint.PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoTrue); } catch (Exception e) { ProcessTextBox.AppendText(Environment.NewLine); ProcessTextBox.AppendText("Error: " + e.Message); return(false); } finally { try { if (ppPresentation != null) { ppPresentation.Close(); ppApplication.Quit(); ppApplication = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } catch (Exception e) { } } return(true); }
public static void AutomatePowerPoint() { PowerPoint.Application oPowerPoint = null; PowerPoint.Presentations oPres = null; PowerPoint.Presentation oPre = null; PowerPoint.Slides oSlides = null; PowerPoint.Slide oSlide = null; PowerPoint.Shapes oShapes = null; PowerPoint.Shape oShape = null; PowerPoint.TextFrame oTxtFrame = null; PowerPoint.TextRange oTxtRange = null; try { // 创建一个Microsoft PowerPoint实例并使其不可见。 oPowerPoint = new PowerPoint.Application(); // 默认情况下PowerPoint不可见,直道你使它可见。 //oPowerPoint.Visible = Office.MsoTriState.msoFalse; // 创建一个新的演示文稿。 oPres = oPowerPoint.Presentations; oPre = oPres.Add(Office.MsoTriState.msoTrue); Console.WriteLine("一个新的演示文稿被建立"); // 插入一个幻灯片,并为幻灯片加入一些文本。 Console.WriteLine("插入一个幻灯片"); oSlides = oPre.Slides; oSlide = oSlides.Add(1, PowerPoint.PpSlideLayout.ppLayoutText); Console.WriteLine("添加一些文本"); oShapes = oSlide.Shapes; oShape = oShapes[1]; oTxtFrame = oShape.TextFrame; oTxtRange = oTxtFrame.TextRange; oTxtRange.Text = "一站式代码框架"; // 保存此演示文稿为pptx文件并将其关闭。 Console.WriteLine("保存并退出演示文稿"); string fileName = Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location) + "\\Sample1.pptx"; oPre.SaveAs(fileName, PowerPoint.PpSaveAsFileType.ppSaveAsOpenXMLPresentation, Office.MsoTriState.msoTriStateMixed); oPre.Close(); // 退出PowerPoint应用程序 Console.WriteLine("退出PowerPoint应用程序"); oPowerPoint.Quit(); } catch (Exception ex) { Console.WriteLine("解决方案1.AutomatePowerPoint抛出的错误: {0}", ex.Message); } finally { // 显式地调用Marshal.FinalReleaseComObject访问所有的存取器对象清除 // 非托管的COM资源。 // 参见http://support.microsoft.com/kb/317109 if (oTxtRange != null) { Marshal.FinalReleaseComObject(oTxtRange); oTxtRange = null; } if (oTxtFrame != null) { Marshal.FinalReleaseComObject(oTxtFrame); oTxtFrame = null; } if (oShape != null) { Marshal.FinalReleaseComObject(oShape); oShape = null; } if (oShapes != null) { Marshal.FinalReleaseComObject(oShapes); oShapes = null; } if (oSlide != null) { Marshal.FinalReleaseComObject(oSlide); oSlide = null; } if (oSlides != null) { Marshal.FinalReleaseComObject(oSlides); oSlides = null; } if (oPre != null) { Marshal.FinalReleaseComObject(oPre); oPre = null; } if (oPres != null) { Marshal.FinalReleaseComObject(oPres); oPres = null; } if (oPowerPoint != null) { Marshal.FinalReleaseComObject(oPowerPoint); oPowerPoint = null; } } }