locked
Powerpoint add-on project RRS feed

  • Question

  • This is my code for a powerpoint add-on writtin in C# visual studio to pull out hidden text (which will be text in the notes of the slide) in each slide as the slide is visited in a slideshow and convert it to audio. First is to pull out the text in the notes when the slide is visited and display as a message which isn't quite working in the code below.  At the moment when the code is ran it just opens powerpoint and nothing else seems to be working. There might be somewhere I'm going wrong.   I've added the for loop in the events to try to iterate through the shapes to find the one which is the notes placeholder.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
    using Office = Microsoft.Office.Core;
    using Microsoft.Office.Interop.PowerPoint;
    using System.Speech.Synthesis;
    namespace FirstPowerPointAddIn
    {
        public partial class ThisAddIn
        {
             private void ThisAddIn_Startup(object sender, System.EventArgs e) 
            {
                /*  //connect the Application_PresentationNewSlide event handler with the PresentationNewSlide event.
                  this.Application.PresentationNewSlide +=
                  new PowerPoint.EApplication_PresentationNewSlideEventHandler(
                  Application_PresentationNewSlide); */
                
                 SpeechSynthesizer synth = new SpeechSynthesizer();
                // Configure the audio output. 
                synth.SetOutputToDefaultAudioDevice();
             PowerPoint.Application oPowerPoint = null;
             try
              {
                    oPowerPoint.SlideShowBegin += oPowerPoint_SlideShowBegin;
                    oPowerPoint.SlideShowNextSlide += oPowerPoint_SlideShowNextSlide;
               }
                catch(Exception)
                {
                    Console.WriteLine("error");
                }
            }
            
            
            
            private void ThisAddIn_Shutdown(object Pender, System.EventArgs e)
            {
            }
         
            private void oPowerPoint_SlideShowBegin(SlideShowWindow Wn) 
            {
                for (int i = 0; i < Wn.View.Slide.NotesPage.Shapes.Count; i++)
                {
                    
                    // If the slide has notes, get the notes
                    if (Wn.View.Slide.HasNotesPage == Microsoft.Office.Core.MsoTriState.msoTrue)
                    {
                        if (Wn.View.Slide.NotesPage.Shapes[i].TextFrame.HasText == Microsoft.Office.Core.MsoTriState.msoTrue)
                            System.Windows.Forms.MessageBox.Show(Wn.View.Slide.NotesPage.Shapes[i].TextFrame.TextRange.Text);
                        
                    }
                }
            }
            
           
            void oPowerPoint_SlideShowNextSlide(PowerPoint.SlideShowWindow Wn)
            {
                for (int i = 0; i < Wn.View.Slide.NotesPage.Shapes.Count; i++)
                {
                    if (Wn.View.Slide.HasNotesPage == Microsoft.Office.Core.MsoTriState.msoTrue)
                    {
                        if (Wn.View.Slide.NotesPage.Shapes[i].TextFrame.HasText == Microsoft.Office.Core.MsoTriState.msoTrue)
                            System.Windows.Forms.MessageBox.Show(Wn.View.Slide.NotesPage.Shapes[i].TextFrame.TextRange.Text);
                    }
                }
            }
            #region VSTO generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
            
            #endregion
        }
    }
    Friday, January 24, 2014 4:10 PM

All replies

  • Hello Heisenberg2001,

    > which isn't quite working in the code below

    Did you try to debug the code and inspect each object and their property values?

    First of all, I'd recommend breaking the chain of calls into single lines of code, for example:

    Wn.View.Slide.NotesPage.Shapes.Count

    1. The View property returns an instance of the View class from the PowerPoint object model.

    2. Then the Slide property of the View class returns an instance of the Slide class.

    3. The NotesPage property of the Slide class returns an instance of the SlideRange object.

    4. The Shapes property of the SlideRange class returns an instance of the Shapes class.

    As you may see, four (4!) COM objects will be held in memory until you release them explicitly or the garbage collector is run. That is why I'd recommend release all RCWs that point to underlying COM objects. The Marshal.ReleaseComObject method can be used for getting the job done.  

    Saturday, January 25, 2014 12:57 PM