Merge Powerpoint presentations

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gill1978
    New Member
    • May 2007
    • 19

    Merge Powerpoint presentations

    Hi,

    What I'm trying to do is,

    once the users select the files they want to merge, they select a button which will:

    1. Open an instance of powerpoint
    2. Open the first of the files selected by the user
    3. Copy/ export that slides from that presentation
    4. Paste into the instance of powerpoint (in point 1 above)
    5. Close the first file
    ... repeat these steps for all the rest of the files selected by the user

    I know what needs to be done but I have no idea how to go about the syntax in VB/ C#

    //I know I need to have the following directive:
    Code:
    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
    //Maybe the following:
    Code:
    PowerPoint.Application oPPT;
    PowerPoint.Presentations objPresSet;
    //the location of your powerpoint presentation
    Code:
    string strPres;
    strPres = @"mpPres.ppt";
    But have no idea where to go from here ...

    Any help would be very much appreciated ..

    Thanks

    J
  • SammyB
    Recognized Expert Contributor
    • Mar 2007
    • 807

    #2
    Do you really need to do this in .Net? Seems like a PowerPoint macro would be a lot easier. In any case, I would do it as a macro first, because you can record a macro to see how to do it. Then switch back to .Net. That is what I would have to do in order to help you. Try that and post back if you have trouble with anything specific. --Sam

    Comment

    • gill1978
      New Member
      • May 2007
      • 19

      #3
      Originally posted by SammyB
      Do you really need to do this in .Net? Seems like a PowerPoint macro would be a lot easier. In any case, I would do it as a macro first, because you can record a macro to see how to do it. Then switch back to .Net. That is what I would have to do in order to help you. Try that and post back if you have trouble with anything specific. --Sam
      Hi,

      This is the macro that I've created that works:

      Code:
      Sub Macro6()
      
          Dim PPApp As PowerPoint.Application
          Dim PPPres As PowerPoint.Presentation
          Dim PPSlide As PowerPoint.Slide
      
          ' Create instance of PowerPoint
          Set PPApp = CreateObject("Powerpoint.Application")
      
          ' Create a presentation
          Set PPPres = PPApp.Presentations.Add
      
      
      Dim nSlides As Integer
      Dim fnames As New Collection
      fnames.Add "Blah1.ppt"
      fnames.Add "Blah2.ppt"
      'fnames.Add "p3.ppt"
      'fnames.Add "p4.ppt"
      'fnames.Add "p5.ppt"
      For Each fnam In fnames
        nSlides = ActivePresentation.Slides.InsertFromFile(fnam, ActivePresentation.Slides.Count)
      Next
      End Sub

      Comment

      • SammyB
        Recognized Expert Contributor
        • Mar 2007
        • 807

        #4
        We are geniuses: I've just about finished the same solution in C#. I'll post it when it works. Hope it's not 4:30 AM where you are like it is here! :)

        Comment

        • gill1978
          New Member
          • May 2007
          • 19

          #5
          4:30 ... nope its 9:40 ... !! Take it you're an insomniac!!!

          Comment

          • SammyB
            Recognized Expert Contributor
            • Mar 2007
            • 807

            #6
            Originally posted by gill1978
            4:30 ... nope its 9:40 ... !! Take it you're an insomniac!!!
            Well, I'm going back to bed! Here's what I have. I think it would work except for the Type.Missing: can't figure out how to pass a missing Com int parameter:
            Code:
            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.Data;
            using System.Drawing;
            using System.Text;
            using System.Windows.Forms;
            using PowerPoint = Microsoft.Office.Interop.PowerPoint; // Add a reference to Microsoft.Office.Interop.PowerPoint.dll
            using Office = Microsoft.Office.Core; // Add a reference to office.dll
            namespace PowerPointCombine
            {
            	public partial class Form1 : Form
            	{
            		public Form1()
            		{
            			InitializeComponent();
            		}
            		private void button1_Click(object sender, EventArgs e)
            		{
            			openFileDialog1.Filter = "Presentations (*.ppt)|*.ppt|All files (*.*)|*.*";
            			openFileDialog1.FilterIndex = 1;
            			openFileDialog1.Multiselect = true;
            			openFileDialog1.Title = "Select Presentations To Merge";
            			openFileDialog1.ShowDialog();
            			// Create an instance of PowerPoint, make it visible, and open a new pres.
            			PowerPoint.ApplicationClass ppApp = new PowerPoint.ApplicationClass();
            			ppApp.Visible = Office.MsoTriState.msoTrue;
            			PowerPoint.Presentations ppSet = ppApp.Presentations;
            			PowerPoint.Presentation ppPres = ppSet.Add(Office.MsoTriState.msoTrue);
            			PowerPoint.Slides ppSlides = ppPres.Slides;
            			foreach (string f in openFileDialog1.FileNames)
            			{
            				ppSlides.InsertFromFile(f, 1, ppSlides.Count, Type.Missing);
            			}
            			ppPres.SaveAs(@"C:\Documents and Settings\Sam\My Documents\Merged.ppt", PowerPoint.PpSaveAsFileType.ppSaveAsPresentation, Office.MsoTriState.msoFalse);
            			ppPres.Close();
            			System.Runtime.InteropServices.Marshal.ReleaseComObject(ppPres);
            			ppPres = null;
            			System.Runtime.InteropServices.Marshal.ReleaseComObject(ppSlides);
            			ppSlides = null;
            			System.Runtime.InteropServices.Marshal.ReleaseComObject(ppSet);
            			ppSet = null;
            			ppApp.Quit();
            			System.Runtime.InteropServices.Marshal.ReleaseComObject(ppApp);
            			ppApp = null;
            		}
            	}
            }
            BTW, as you can see, you have to be very precise in C#. All of the Office Com stuff is wrapped in what VBA calls a variant which does not fly in C# so you cannot sling stuff together like Application.Pre sentations.Add.

            Comment

            • SammyB
              Recognized Expert Contributor
              • Mar 2007
              • 807

              #7
              Well, so much for our "genius" solution. You cannot pass missing value parameters in C# (http://msdn2.microsoft.com/en-us/lib...ffice.11).aspx), so we will have to open each presentation manually.

              Comment

              • gill1978
                New Member
                • May 2007
                • 19

                #8
                Manually as in the user will have to open the files???
                Not so good then ... wonder if there's another approach???

                Comment

                • SammyB
                  Recognized Expert Contributor
                  • Mar 2007
                  • 807

                  #9
                  Originally posted by gill1978
                  Manually as in the user will have to open the files???
                  Not so good then ... wonder if there's another approach???
                  No, no, not that! We will have to open each presentation in code, copy the slides, switch back to the new presentation, and paste the slides there. Only 4 or 5 more lines. After simplifying a recorded macro, it looks like:
                  Code:
                  ActivePresentation.Slides.Range.Copy
                  Windows.Item(Index:=2).Activate
                  ActiveWindow.View.Paste
                  but there I already had two presentations open. I may have time tonight after you are sound asleep to do it in C#. This is my first venture with PowerPoint InterOp. Makes regular PoPo programming look easy. Stay tuned and/or try it yourself. If you try it yourself, you'll need to save a DocumentWindow object from the merged Presentation. I suspect that it is ppPres.Windows( 1). Then you won't need the Activate, just an Open, Copy & Paste for each file.

                  Comment

                  • SammyB
                    Recognized Expert Contributor
                    • Mar 2007
                    • 807

                    #10
                    Well, it sort of works! It was very annoying. I have a new rule: NEVER do InterOp in C#: it should be called InterNoOp. Maybe you can make it better! If so, it would be great to post your code. HTH --Sam
                    Code:
                    using System;
                    using System.Collections.Generic;
                    using System.ComponentModel;
                    using System.Data;
                    using System.Drawing;
                    using System.Text;
                    using System.Windows.Forms;
                    using PowerPoint = Microsoft.Office.Interop.PowerPoint; // Add a reference to Microsoft.Office.Interop.PowerPoint.dll
                    using Office = Microsoft.Office.Core; // Add a reference to office.dll
                    namespace PowerPointCombine
                    {
                    	public partial class Form1 : Form
                    	{
                    		public Form1()
                    		{
                    			InitializeComponent();
                    		}
                    		private void button1_Click(object sender, EventArgs e)
                    		{
                    			openFileDialog1.Filter = "Presentations (*.ppt)|*.ppt|All files (*.*)|*.*";
                    			openFileDialog1.FilterIndex = 1;
                    			openFileDialog1.Multiselect = true;
                    			openFileDialog1.Title = "Select Presentations To Merge";
                    			openFileDialog1.ShowDialog();
                    			// Create an instance of PowerPoint, make it visible, and open a new pres.
                    			PowerPoint.ApplicationClass ppApp = new PowerPoint.ApplicationClass();
                    			ppApp.Visible = Office.MsoTriState.msoTrue;
                    			PowerPoint.Presentations ppSet = ppApp.Presentations;
                    			PowerPoint.Presentation ppMergedPres = ppSet.Add(Office.MsoTriState.msoTrue);
                    			PowerPoint.Slides ppMergedSlides = ppMergedPres.Slides;
                    			PowerPoint.DocumentWindows ppWindows = ppMergedPres.Windows;
                    			PowerPoint.DocumentWindow ppWindow = ppWindows[1];
                    			PowerPoint.View ppMergedView = ppWindow.View;
                    			PowerPoint.Presentation ppPres = null;
                    			PowerPoint.Slides ppSlides = null;
                    			foreach (string f in openFileDialog1.FileNames)
                    			{
                    				//ppSlides.InsertFromFile(f, ppSlides.Count, 1, 999);
                    				ppPres = ppSet.Open(f, Office.MsoTriState.msoTrue, Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue);
                    				ppSlides = ppPres.Slides;
                    				for (int i=ppSlides.Count; i>=1; i--)
                    				{
                    					ppSlides[i].Copy();
                    					ppMergedView.Paste();
                    				}
                    				ppPres.Close();
                    			}
                    			ppMergedPres.SaveAs(@"C:\Documents and Settings\Sam\My Documents\Merged.ppt", PowerPoint.PpSaveAsFileType.ppSaveAsPresentation, Office.MsoTriState.msoFalse);
                    			ppMergedPres.Close();
                    			System.Runtime.InteropServices.Marshal.ReleaseComObject(ppMergedPres);
                    			ppMergedPres = null;
                    			System.Runtime.InteropServices.Marshal.ReleaseComObject(ppSlides);
                    			ppSlides = null;
                    			System.Runtime.InteropServices.Marshal.ReleaseComObject(ppSet);
                    			ppSet = null;
                    			ppApp.Quit();
                    			System.Runtime.InteropServices.Marshal.ReleaseComObject(ppApp);
                    			ppApp = null;
                    		}
                    	}
                    }

                    Comment

                    • gill1978
                      New Member
                      • May 2007
                      • 19

                      #11
                      Hi again ...
                      Got dragged into something else ... anyhow ... trying to find the references .. however they seem to be missing. There's no interop or core ...
                      Oh and I'm doing this on a web form?!?!

                      Comment

                      • SammyB
                        Recognized Expert Contributor
                        • Mar 2007
                        • 807

                        #12
                        Originally posted by gill1978
                        Hi again ...
                        Got dragged into something else ... anyhow ... trying to find the references .. however they seem to be missing. There's no interop or core ...
                        Oh and I'm doing this on a web form?!?!
                        Why, Why, Why, etc

                        You have to download Interop. Have you done that?
                        If you have Visual Studio, then you follow the download instructions.
                        If you have Express, you just add the ref by browsing to the Interop directory.

                        When googling for info about this, I remember a lot of whining about Interop & web. Do you really need a Web form? If so, I'd do some google searches.

                        Do you have to do this in C#? VB would be easier. It allows missing value parameters, so the easy InsertFromFile would work. If you must do it in C#, then I think that it would be easier to just open each presentation, get the slide count, and close it. Then, again, you could use InsertFromFile.

                        Have you thought about how to order the presentations? Since XP, the OpenFileDialog does not have any order except insanity?

                        Aren't you glad you got dragged into something else? BTW, I don't do web, so I'll be no help. :) Sam

                        Comment

                        • gill1978
                          New Member
                          • May 2007
                          • 19

                          #13
                          Thanks Sam ...

                          Got those reference's ... !

                          Currently I have the first bit (getting the files and ordering them) done in C# ... I just assumed since part of it was working that way the rest would be done in C# ....

                          The files are ordered in a listbox ... these I guess i'll need to pass as a collection to the function you've written above...

                          The function for getting and ordering the files are:

                          Code:
                          public partial class FullQuestionnaire : System.Web.UI.Page
                          {
                              protected void Page_Load(object sender, EventArgs e)
                              {
                          
                              }
                          
                              protected void Blue(object sender, EventArgs e)
                              {
                                  foreach(ListItem item in CheckBoxList1.Items)
                                  {
                                      if (item.Selected && !FileSelectedListBox.Items.Contains(item))
                                      {
                                          FileSelectedListBox.Items.Add(new ListItem(item.Text, item.Value));
                                      }
                                      else if (!item.Selected && FileSelectedListBox.Items.Contains(item))
                                      {
                                          FileSelectedListBox.Items.Remove(item);
                                      }
                                  }
                              }
                              protected void Button1_Click(object sender, EventArgs e)
                              {
                                
                              }
                          }
                          My first .NET app ... muddling through as you can see .. the something else I was pulled into wasn't that great!!!

                          Comment

                          • gill1978
                            New Member
                            • May 2007
                            • 19

                            #14
                            Hiya,

                            From looking about i've got the following bit of code to get the ListBox Items into and arra to pass to your function ....

                            However, since i'm very new to C# this isn't making much sense to me.
                            For example, what does the <string> bit mean ... do I pass the name of the ListBox ... or is this a new variable?

                            Code:
                            protected void SomeEvent(...)
                            {
                               //get selected values
                               List<string> selected = new List<string>();
                               foreach(ListItem item in myControl.Items)
                               {
                                    if(item.selected)
                                    {
                                        selected.Add(item.Value);
                                    }
                               }
                               //call function
                               MyClass c = new MyClass(...);
                               c.MyFunction(selected.ToArray());
                            }
                            Thanks,
                            J

                            p.s. You wouldn't know of a good place to go brush up on my C#???!!!

                            Comment

                            • gill1978
                              New Member
                              • May 2007
                              • 19

                              #15
                              Hi again ...

                              Figured out the array thing but am having a couple of issues with the way i'm passing in the file names:
                              I get the error:
                              The best overloaded method match for 'Microsoft.Offi ce.Interop.Powe rPoint.Presenta tions.Open(stri ng, Microsoft.Offic e.Core.MsoTriSt ate, Microsoft.Offic e.Core.MsoTriSt ate, Microsoft.Offic e.Core.MsoTriSt ate)' has some invalid arguments


                              This is the code that I've got now:

                              Code:
                                  protected void Button1_Click(object sender, EventArgs e)
                                 // Create an instance of PowerPoint, make it visible, and open a new pres.
                                      Microsoft.Office.Interop.PowerPoint.ApplicationClass ppApp = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
                                      ppApp.Visible = Office.MsoTriState.msoTrue;
                                      Powerpoint.Presentations ppSet = ppApp.Presentations;
                                      Powerpoint.Presentation ppMergedPres = ppSet.Add(Office.MsoTriState.msoTrue);
                                      Powerpoint.Slides ppMergedSlides = ppMergedPres.Slides;
                                      Powerpoint.DocumentWindows ppWindows = ppMergedPres.Windows;
                                      Powerpoint.DocumentWindow ppWindow = ppWindows[1];
                                      Powerpoint.View ppMergedView = ppWindow.View;
                                      Powerpoint.Presentation ppPres = null;
                                      Powerpoint.Slides ppSlides = null;
                                      System.Collections.Generic.List<String> selected = new System.Collections.Generic.List<String>();
                                      foreach (ListItem item in FileSelectedListBox.Items)
                                      {
                                          if (item.Selected)
                                          {
                                              selected.Add(item.Value);
                                              //ppSlides.InsertFromFile(f, ppSlides.Count, 1, 999);
                                              ppPres = ppSet.Open(selected, Office.MsoTriState.msoTrue, Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue);
                                              ppSlides = ppPres.Slides;
                                              for (int i = ppSlides.Count; i >= 1; i--)
                                              {
                                                  ppSlides[i].Copy();
                                                  ppMergedView.Paste();
                                              }
                                              ppPres.Close();
                                          }
                                      }
                              
                                  }
                              Thanks

                              J

                              Comment

                              Working...