private void buttonStartExample_Click(object sender, EventArgs e) { // start powerpoint and turn off msg boxes PowerPoint.Application powerApplication = new PowerPoint.Application(); powerApplication.Visible = MsoTriState.msoTrue; // PowerPoint 2000 doesnt support DisplayAlerts, we check at runtime its available and set if (powerApplication.EntityIsAvailable("DisplayAlerts")) { powerApplication.DisplayAlerts = PpAlertLevel.ppAlertsNone; } // we register some events. note: the event trigger was called from power point, means an other Thread powerApplication.PresentationCloseEvent += new NetOffice.PowerPointApi.Application_PresentationCloseEventHandler(powerApplication_PresentationCloseEvent); powerApplication.AfterNewPresentationEvent += new NetOffice.PowerPointApi.Application_AfterNewPresentationEventHandler(powerApplication_AfterNewPresentationEvent); // add a new presentation with one new slide PowerPoint.Presentation presentation = powerApplication.Presentations.Add(MsoTriState.msoTrue); PowerPoint.Slide slide = presentation.Slides.Add(1, PpSlideLayout.ppLayoutBlank); // close the document presentation.Close(); // close power point and dispose reference powerApplication.Quit(); powerApplication.Dispose(); }
public TestResult DoTest() { PowerPoint.Application application = null; DateTime startTime = DateTime.Now; try { application = new PowerPoint.Application(); application.Visible = MsoTriState.msoTrue; // PowerPoint 2000 doesnt support DisplayAlerts, we check at runtime its available and set if (application.EntityIsAvailable("DisplayAlerts")) { application.DisplayAlerts = PpAlertLevel.ppAlertsNone; } application.PresentationCloseEvent += new NetOffice.PowerPointApi.Application_PresentationCloseEventHandler(powerApplication_PresentationCloseEvent); application.AfterNewPresentationEvent += new NetOffice.PowerPointApi.Application_AfterNewPresentationEventHandler(powerApplication_AfterNewPresentationEvent); // add a new presentation with one new slide PowerPoint.Presentation presentation = application.Presentations.Add(MsoTriState.msoTrue); PowerPoint.Slide slide = presentation.Slides.Add(1, PpSlideLayout.ppLayoutBlank); System.Threading.Thread.Sleep(2000); // close the document presentation.Close(); if (_afterNewPresentation && _presentationClose) { return(new TestResult(true, DateTime.Now.Subtract(startTime), "", null, "")); } else { return(new TestResult(false, DateTime.Now.Subtract(startTime), String.Format("AfterNewPresentation:{0} , PresentationClose:{1}", _afterNewPresentation, _presentationClose), null, "")); } } catch (Exception exception) { return(new TestResult(false, DateTime.Now.Subtract(startTime), exception.Message, exception, "")); } finally { if (null != application) { application.Quit(); application.Dispose(); } } }
private void doWork2() { PowerPoint.Presentation presentation = null; PowerPoint.Application applicationPowerPoint = null; Excel.Workbook book = null; Excel.Application applicationExcel = null; try { Dictionary <String, Excel.Shape> excelShapesDictionary = new Dictionary <string, Excel.Shape>(); Log.Information("Opening Excel Applicaiton"); applicationExcel = new Excel.Application(); applicationExcel.DisplayAlerts = false; String xlsx = "D:\\Input\\Input.xlsx"; String output_folder = "D:\\Output"; String pptxSingle = "D:\\Input\\input.pptx"; Log.Information("Opening Excel File {0}", xlsx); book = applicationExcel.Workbooks.Open(xlsx, true, true); Excel.Worksheet worksheet = (Excel.Worksheet)book.Worksheets[1]; int index = 3; List <PresentationItem> presentationItems = new List <PresentationItem>(); do { String s = worksheet.Range("A" + index).Value2.ToString(); Log.Information(worksheet.Name + " " + s); PresentationItem presentationItem = new PresentationItem(); presentationItem.OutputFileName = s; int index2 = 1; do { String attr_type = worksheet.Range(((char)((byte)'A' + index2)).ToString() + 1).Value2.ToString(); String attr_name = worksheet.Range(((char)((byte)'A' + index2)).ToString() + 2).Value2.ToString(); String attr_value = worksheet.Range(((char)((byte)'A' + index2)).ToString() + index).Value2.ToString(); Log.Information("{0} {1} {2} {3}", ((char)((byte)'A' + index2)).ToString() + 1, attr_type, attr_name, attr_value); presentationItem.Attributes.Add(new Attribute(attr_type, attr_name, attr_value)); index2++; if (index2 == 20) { break; } } while (worksheet.Range(((char)((byte)'A' + index2)).ToString() + 1).Value2 != null && worksheet.Range(((char)((byte)'A' + index2)).ToString() + 1).Value2.ToString() != ""); index++; presentationItems.Add(presentationItem); if (index == 100) { break; } } while (worksheet.Range("A" + index).Value2 != null && worksheet.Range("A" + index).Value2.ToString() != ""); Log.Information("Closing Excel File"); book.Close(); Log.Information("Opening PowerPoint Applicaiton"); applicationPowerPoint = new PowerPoint.Application(); applicationPowerPoint.DisplayAlerts = PowerPoint.Enums.PpAlertLevel.ppAlertsNone; foreach (var presentationItem in presentationItems) { Dictionary <int, List <PowerPoint.Shape> > powerpointShapesSheetDictionary = new Dictionary <int, List <PowerPoint.Shape> >(); Log.Information("Opening Presentation {0}", pptxSingle); presentation = applicationPowerPoint.Presentations.Open(pptxSingle, true, true, true); foreach (PowerPoint.Shape shape in presentation.SlideMaster.Shapes) { Log.Information("\tSlide Master: Shape Name : {0} Size(w x h) : {1} x {2} Position(left x top) : {3} , {4}", shape.Name, shape.Width, shape.Height, shape.Left, shape.Top); if (shape.HasTextFrame == Office.Enums.MsoTriState.msoTrue && shape.TextFrame.HasText == Office.Enums.MsoTriState.msoTrue) { foreach (var attribute in presentationItem.Attributes) { if (attribute.type == "Text") { string before = shape.TextFrame.TextRange.Text; shape.TextFrame.TextRange.Replace("|*" + attribute.name + "*|", attribute.value); string after = shape.TextFrame.TextRange.Text; if (before != after) { Log.Information("\t\t{0} >> {1}", before, after); } } } } } Log.Information("Iterating all shapes in all slides and filtering shapes with name staring with \"#\""); foreach (PowerPoint.Slide slide in presentation.Slides) { foreach (PowerPoint.Shape shape in slide.Shapes) { Log.Information("\tSlide No : {0} Shape Name : {1} Size(w x h) : {2} x {3} Position(left x top) : {4} , {5}", slide.SlideNumber, shape.Name, shape.Width, shape.Height, shape.Left, shape.Top); if (shape.HasTextFrame == Office.Enums.MsoTriState.msoTrue && shape.TextFrame.HasText == Office.Enums.MsoTriState.msoTrue) { foreach (var attribute in presentationItem.Attributes) { if (attribute.type == "Text") { string before = shape.TextFrame.TextRange.Text; shape.TextFrame.TextRange.Replace("|*" + attribute.name + "*|", attribute.value); string after = shape.TextFrame.TextRange.Text; if (before != after) { Log.Information("\t\t{0} >> {1}", before, after); } } } } foreach (var attribute in presentationItem.Attributes) { if (attribute.type == "Chart" && shape.Name == "|*" + attribute.name + "*|") { string before = shape.Name; shape.Name = attribute.value; string after = shape.Name; if (before != after) { Log.Information("\t\t{0} >> {1}", before, after); } } } } Log.Information(""); } String outputfile = Path.Combine(output_folder, new FileInfo(pptxSingle).Name); Log.Information("Saving a copy of updated Presentation to {0}", outputfile); presentation.SaveCopyAs(outputfile); Log.Information("Closing Presentation"); } presentation.Close(); Log.Information("Closing PowerPoint Applicaiton"); applicationPowerPoint.Quit(); applicationPowerPoint.Dispose(); Log.Information("Closing Excel Applicaiton"); applicationExcel.Quit(); applicationExcel.Dispose(); Log.Information("Done"); } catch (Exception e) { Log.Error("Fatal Error - " + e.ToString()); thisForm.BeginInvoke((MethodInvoker)(() => { MessageBox.Show(thisForm, e.ToString(), "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error); })); try { presentation.Close(); } catch (Exception ee) { } try { applicationPowerPoint.Quit(); applicationPowerPoint.Dispose(); } catch (Exception ee) { } try { book.Close(); } catch (Exception ee) { } try { applicationExcel.Quit(); applicationExcel.Dispose(); } catch (Exception ee) { } } }
private void button_close_Click(object sender, EventArgs e) { timer.Stop(); pret.Close(); ppt.Quit(); }
private void doWork() { PowerPoint.Presentation presentation = null; PowerPoint.Application applicationPowerPoint = null; Excel.Workbook book = null; Excel.Application applicationExcel = null; try { Dictionary <String, Excel.Shape> excelShapesDictionary = new Dictionary <string, Excel.Shape>(); Log.Information("Opening Excel Applicaiton"); applicationExcel = new Excel.Application(); applicationExcel.DisplayAlerts = false; Log.Information("Opening Excel File {0}", xlsx); book = applicationExcel.Workbooks.Open(xlsx, true, true); Boolean flagDouble = false; Log.Information("Iterating all shapes in all sheets and filtering shapes with name staring with \"#\""); foreach (Excel.Worksheet sheet in book.Worksheets) { foreach (Excel.Shape shape in sheet.Shapes) { if (shape.Name.Length > 1 && shape.Name.Substring(0, 1) == "#") { if (excelShapesDictionary.ContainsKey(shape.Name.ToLower().Trim())) { Log.Error("\t\tSheet: {0} Shape Name : {1} Error!!! Shape with the same name exists", sheet.Name, shape.Name, shape.Width, shape.Height, shape.Left, shape.Top); flagDouble = true; } else { Log.Information("\tSheet: {0} Shape Name : {1} ", sheet.Name, shape.Name, shape.Width, shape.Height, shape.Left, shape.Top); excelShapesDictionary.Add(shape.Name.ToLower().Trim(), shape); } } } } if (flagDouble) { Log.Error("ERROR -Found shapes with the same name : Duplicated shapes need to be manually renamed in the Excel file before proceeding. Terminating run."); thisForm.BeginInvoke((MethodInvoker)(() => { MessageBox.Show(thisForm, "Duplicated shapes need to be manually renamed in the Excel file before proceeding.\nSee logs for more information", "ERROR - Found shapes with the same name", MessageBoxButtons.OK, MessageBoxIcon.Error); })); goto closeWorkbook; } Log.Information("Opening PowerPoint Applicaiton"); applicationPowerPoint = new PowerPoint.Application(); applicationPowerPoint.DisplayAlerts = PowerPoint.Enums.PpAlertLevel.ppAlertsNone; foreach (string pptxSingle in pptx) { Dictionary <int, List <PowerPoint.Shape> > powerpointShapesSheetDictionary = new Dictionary <int, List <PowerPoint.Shape> >(); Log.Information("Opening Presentation {0}", pptxSingle); presentation = applicationPowerPoint.Presentations.Open(pptxSingle, true, true, false); Log.Information("Iterating all shapes in all slides and filtering shapes with name staring with \"#\""); Boolean datamissing = false; foreach (PowerPoint.Slide slide in presentation.Slides) { foreach (PowerPoint.Shape shape in slide.Shapes) { if (shape.Name.Length > 1 && shape.Name.Substring(0, 1) == "#") { if (!excelShapesDictionary.ContainsKey(shape.Name.ToLower().Trim())) { Log.Information("\t\tData Missing for Slide No : {0} Shape Name : {1} ", slide.SlideNumber, shape.Name, shape.Width, shape.Height, shape.Left, shape.Top); datamissing = true; } else { Log.Information("\tFound data for Slide No : {0} Shape Name : {1} ", slide.SlideNumber, shape.Name, shape.Width, shape.Height, shape.Left, shape.Top); } if (!powerpointShapesSheetDictionary.ContainsKey(slide.SlideNumber)) { powerpointShapesSheetDictionary.Add(slide.SlideNumber, new List <PowerPoint.Shape>()); } powerpointShapesSheetDictionary[slide.SlideNumber].Add(shape); } else { /* * Log.Information("\tFound data for Slide No : {0} Shape Name : {1} Size(w x h) : {2} x {3} Position(left x top) : {4} , {5}", slide.SlideNumber, shape.Name, shape.Width, shape.Height, shape.Left, shape.Top); * if (shape.HasTextFrame == Office.Enums.MsoTriState.msoTrue && shape.TextFrame.HasText == Office.Enums.MsoTriState.msoTrue) * shape.TextFrame.TextRange.Replace("|*test*|", "123"); */ } } } if (datamissing) { Log.Error("ERROR - Match not found for shape/s in presentation : All shapes starting with \"#\"in the presentation should have matching shape in the excel."); thisForm.BeginInvoke((MethodInvoker)(() => { MessageBox.Show(thisForm, "All shapes starting with \"#\"in the presentation should have matching shape in the excel.\nSee logs for more information", "ERROR - Match not found for shape/s in presentation", MessageBoxButtons.OK, MessageBoxIcon.Error); })); goto closePresentation; } Log.Information("Start of Update"); foreach (int slideNo in powerpointShapesSheetDictionary.Keys) { foreach (var shape in powerpointShapesSheetDictionary[slideNo]) { excelShapesDictionary[shape.Name.ToLower()].Copy(); Thread.Sleep(100); PowerPoint.ShapeRange shapes = presentation.Slides[slideNo].Shapes.PasteSpecial(PowerPointEnums.PpPasteDataType.ppPasteJPG); string shape_name = shape.Name; float shape_top = shape.Top; float shape_left = shape.Left; float shape_width = shape.Width; float shape_height = shape.Height; shapes[1].Name = shape_name; shapes[1].Top = shape_top; shapes[1].Left = shape_left; shapes[1].ScaleWidth(shape.Width / shapes[1].Width, Office.Enums.MsoTriState.msoFalse); //shapes[1].Height = shape_height; shape.Delete(); Log.Information(" Updated {0} on slide {1}", shape_name, slideNo); } } Log.Information("End of Update"); String outputfile = Path.Combine(output_folder, new FileInfo(pptxSingle).Name); Log.Information("Saving a copy of updated Presentation to {0}", outputfile); presentation.SaveCopyAs(outputfile); closePresentation: Log.Information("Closing Presentation"); presentation.Close(); } Log.Information("Closing PowerPoint Applicaiton"); applicationPowerPoint.Quit(); applicationPowerPoint.Dispose(); closeWorkbook: Log.Information("Closing Excel File"); book.Close(); Log.Information("Closing Excel Applicaiton"); applicationExcel.Quit(); applicationExcel.Dispose(); Log.Information("Done"); thisForm.BeginInvoke((MethodInvoker)(() => { button1.Enabled = true; button2.Enabled = true; buttonFolder.Enabled = true; button3.Enabled = true; listView1.Enabled = true; listView2.Enabled = true; listView3.Enabled = true; MessageBox.Show(thisForm, "Task Completed. Check logs for more info", "Completed", MessageBoxButtons.OK, MessageBoxIcon.None); })); } catch (Exception e) { Log.Error("Fatal Error - " + e.ToString()); thisForm.BeginInvoke((MethodInvoker)(() => { MessageBox.Show(thisForm, e.ToString(), "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error); button1.Enabled = true; button2.Enabled = true; buttonFolder.Enabled = true; button3.Enabled = true; listView1.Enabled = true; listView2.Enabled = true; listView3.Enabled = true; })); try { presentation.Close(); } catch (Exception ee) { } try { applicationPowerPoint.Quit(); applicationPowerPoint.Dispose(); } catch (Exception ee) { } try { book.Close(); } catch (Exception ee) { } try { applicationExcel.Quit(); applicationExcel.Dispose(); } catch (Exception ee) { } } }