Ejemplo n.º 1
0
 public Presentation(PowerPoint.Presentation presentation)
 {
     this.presentation = presentation;
     this.tags = new PowerPointTags(this.presentation);
     this.properties = new DocumentProperty(this.presentation);
 }
Ejemplo n.º 2
0
 public Slide(PowerPoint.Presentation presentation, PowerPoint.Slide ppSlide, string slideIdTagName)
 {
     this.presentation   = presentation;
     this.slide          = ppSlide;
     this.slideIdTagName = slideIdTagName;
 }
        private void getPPtData()
        {
            try
            {
                if (pptApplication != null)
                {
                    // Get Presentation Object
                    presentation = pptApplication.ActivePresentation;
                    // Get Slide collection object
                    slides = presentation.Slides;
                    // Get Slide count
                    slidescount = slides.Count;

                    // Get current selected slide
                    try
                    {
                        // Get selected slide object in normal view
                        slide = slides[pptApplication.ActiveWindow.Selection.SlideRange.SlideNumber];
                    }
                    catch
                    {
                        // Get selected slide object in reading view
                        slide = pptApplication.SlideShowWindows[1].View.Slide;

                    }
                }
            }
            catch
            {

                // if pptApplication.SlideShowWindows[1].View.Slide is invalid,
                // e.g. the 'Press any key to exit slideshow' screen
                // ignore.

            }
        }
Ejemplo n.º 4
0
 private void powerApplication_AfterNewPresentationEvent(NetOffice.PowerPointApi.Presentation Pres)
 {
     textBoxEvents.BeginInvoke(_updateDelegate, new object[] { "Event AfterNewPresentation called." });
     Pres.Dispose();
 }
Ejemplo n.º 5
0
        private void buttonStartExample_Click(object sender, EventArgs e)
        {
            // start powerpoint
            _powerApplication = new PowerPoint.Application();

            Office.CommandBar       commandBar    = null;
            Office.CommandBarButton commandBarBtn = null;

            // 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);

            // add a commandbar popup
            Office.CommandBarPopup commandBarPopup = (Office.CommandBarPopup)_powerApplication.CommandBars["Menu Bar"].Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
            commandBarPopup.Caption = "commandBarPopup";

            #region few words, how to access the picture

            /*
             * you can see we use an own icon via .PasteFace()
             * is not possible from outside process boundaries to use the PictureProperty directly
             * the reason for is IPictureDisp: http://support.microsoft.com/kb/286460/de
             * its not important is early or late binding or managed or unmanaged, the behaviour is always the same
             * For example, a COMAddin running as InProcServer and can access the Picture Property
             */
            #endregion

            #region CommandBarButton

            // add a button to the popup
            commandBarBtn         = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
            commandBarBtn.Style   = MsoButtonStyle.msoButtonIconAndCaption;
            commandBarBtn.Caption = "commandBarButton";
            Clipboard.SetDataObject(_hostApplication.DisplayIcon.ToBitmap());
            commandBarBtn.PasteFace();
            commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

            #endregion

            #region Create a new toolbar

            // add a new toolbar
            commandBar         = _powerApplication.CommandBars.Add("MyCommandBar", MsoBarPosition.msoBarTop, false, true);
            commandBar.Visible = true;

            // add a button to the toolbar
            commandBarBtn             = (Office.CommandBarButton)commandBar.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
            commandBarBtn.Style       = MsoButtonStyle.msoButtonIconAndCaption;
            commandBarBtn.Caption     = "commandBarButton";
            commandBarBtn.FaceId      = 3;
            commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

            // add a dropdown box to the toolbar
            commandBarPopup         = (Office.CommandBarPopup)commandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
            commandBarPopup.Caption = "commandBarPopup";

            // add a button to the popup, we use an own icon for the button
            commandBarBtn         = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
            commandBarBtn.Style   = MsoButtonStyle.msoButtonIconAndCaption;
            commandBarBtn.Caption = "commandBarButton";
            Clipboard.SetDataObject(_hostApplication.DisplayIcon.ToBitmap());
            commandBarBtn.PasteFace();
            commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

            #endregion

            #region Create a new ContextMenu

            // add a commandbar popup
            commandBarPopup = (Office.CommandBarPopup)_powerApplication.CommandBars["Frames"].Controls.Add(
                MsoControlType.msoControlPopup, Missing.Value, Missing.Value, Missing.Value, true);
            commandBarPopup.Caption = "commandBarPopup";

            // add a button to the popup
            commandBarBtn             = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, Missing.Value, Missing.Value, Missing.Value, true);
            commandBarBtn.Style       = MsoButtonStyle.msoButtonIconAndCaption;
            commandBarBtn.Caption     = "commandBarButton";
            commandBarBtn.FaceId      = 9;
            commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

            #endregion

            // make visible & set buttons
            _powerApplication.Visible  = MsoTriState.msoTrue;
            buttonStartExample.Enabled = false;
            buttonQuitExample.Enabled  = true;
        }
Ejemplo n.º 6
0
 public DocumentProperty(PowerPoint.Presentation presentation)
 {
     this.element = presentation;
 }
Ejemplo n.º 7
0
 public Presentation(PowerPoint.Presentation presentation)
 {
     this.presentation = presentation;
     this.tags         = new PowerPointTags(this.presentation);
     this.properties   = new DocumentProperty(this.presentation);
 }
Ejemplo n.º 8
0
        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) { }
            }
        }
Ejemplo n.º 9
0
        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) { }
            }
        }
Ejemplo n.º 10
0
 private void SavePresentationAs(string exportFullFileName, PowerPoint.Presentation presentation)
 {
     presentation.SaveAs(exportFullFileName);
     this.taskManager.Sleep(WaitAfterSavingMilliseconds);
 }