none
Need to open Powerpoint 2007 Presentation from C# Windows Forms Application (VisualStudio 2008) - Run-time error generated RRS feed

  • Question

  • I am getting a successful build but a run-time error is generated.  I want to be able to open a powerpoint presentation and then move through it with slide forward/slide back arrows.  At the end I will need to place text/images into specific locations (manufacturing/test procedure with data entry).  MS has been very helpful recently - hoping you can do it again.

    The error I am getting is:  -  err {System.Runtime.InteropServices.COMException (0x80048240): Presentations (unknown member) : Invalid request.  The PowerPoint Frame window does not exist.
       at Microsoft.Office.Interop.PowerPoint.Presentations.Open(String FileName, MsoTriState ReadOnly, MsoTriState Untitled, MsoTriState WithWindow)
       at ManufacturingProcedureAutomation.MPAStartForm.openProcedure_Click(Object sender, EventArgs e) in C:\ManufacturingProcedureAutomation\ManufacturingProcedureAutomation\MPAStartForm.cs:line 90} System.Exception {System.Runtime.InteropServices.COMException}

    Here is the relevant code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    using System.Configuration;
    using System.IO;
    using Excel = Microsoft.Office.Interop.Excel;
    using Access = Microsoft.Office.Interop.Access;
    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
    using System.Runtime.InteropServices;
    using Microsoft.Office.Core;
    
    
         private void openProcedure_Click(object sender, EventArgs e)
         {
            DialogResult result;
    
            PowerPoint._Application ppApp;
            PowerPoint.Presentation ppMP;
            ppApp = new PowerPoint.ApplicationClass();
    
            try
            {
              ppMP = ppApp.Presentations.Open("\\\\directory_path\\MPxxxxx-xxx-.pptx",
                 MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoTrue);
            }
            catch (Exception err)
            {
              result = MessageBox.Show("[Exception error: {0}]", err.ToString());
              return;
            }
     
         }
    
    • Moved by Cindy Meister MVPModerator Wednesday, February 2, 2011 2:20 PM not VSTO technology (From:Visual Studio Tools for Office)
    Tuesday, February 1, 2011 7:38 PM

Answers

  • Hi fred

    PowerPoint's object model has various "Open" methods, one of them takes four parameters, another five. Open2007 is the latter and the additional parameter is to support new functionality (an OpenAndRepair parameter). The reason for this is because the Office object models don't support over-loading, so they're starting to accumulate multiple methods...

    It's important to realize that these are COM type libraries that are "translated" for .NET by Interop Assemblies. Your project should be picking up the PRIMARY IAs installed in the GAC by Office. You do have Office 2007 installed on this development machine? and you're also referencing the "12" library for PowerPoint?

    As to the "two different Core files": As a matter of fact, some versions of Visual Studio install their own sets of type libraires. Those are the ones in the .NET tab, while the ones in the COM tab are those that come with Office. I usually target the ones in the COM tab as I can be sure those will point to the PIAs in the GAC. I've had odd behavior going via the .NET tab, even though the end result is supposed to be the same.

    The following code works for me (button on a Windows Form):

        private void btnPPT_Click(object sender, EventArgs e)
        {
          Microsoft.Office.Interop.PowerPoint.Application pptApp = new Microsoft.Office.Interop.PowerPoint.Application();
          Microsoft.Office.Core.MsoTriState ofalse = Microsoft.Office.Core.MsoTriState.msoFalse;
    Microsoft.Office.Core.MsoTriState otrue = Microsoft.Office.Core.MsoTriState.msoTrue;
          pptApp.Visible = otrue;
          pptApp.Activate();
          Microsoft.Office.Interop.PowerPoint.Presentations ps = pptApp.Presentations;
          Microsoft.Office.Interop.PowerPoint.Presentation p = ps.Open(@"D:\AQHA\Wegbeschreibung\Reithallenplan.pptx", ofalse, ofalse, otrue);
          System.Diagnostics.Debug.Print(p.Windows.Count.ToString());
          MessageBox.Show(pptApp.ActiveWindow.Caption);
        }
    

    Cindy Meister, VSTO/Word MVP
    • Marked as answer by fredConklin Wednesday, February 9, 2011 4:17 PM
    Monday, February 7, 2011 7:07 PM
    Moderator
  • Hi Fred

    Sorry, I've hit a busy patch in the "real world"...

    To a certain extent, this is philosophical, but: Not all versions and editions of Visual Studio install a link that will appear in the .NET tab. You should always get a link in the COM tab. Since I answer a lot of questions and write books on Office programming (German language market), I make it a habit to use "the lowest common denominator".

    In any case, both of these should cause your project to link to the shared PIAs installed in the Windows GAC. So, in principle it shouldn't matter. On occasion, I've found things to be more stable linking to through the COM tab, but I can't recite you any exact cases at the moment.

    and by "stable" I mean if I open the project again in Visual Studio the reference might "go missing". Actually, thinking about it some more, it may have been project coming up from VS 2005 or VS 2008 to the next version (as I know I saw it when updating a book's sample code). Anything linked via the COM tab was still "found", but sometimes not things linked through the .NET tab. And this could make some sense as the version numbers might change with .NET components, but certainly won't with the "native" COM components.

    <<It was only at runtime that I got the PowerPoint frame error message.  What was wrong that i got that at runtime?>>

    I honestly don't know. My "gut feeling" is telling me that PowerPoint couldn't be found at that time. That's not an error I've seen often (if at all) before, so I've only "gut feeling" to go on...


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by fredConklin Friday, February 11, 2011 3:03 PM
    Thursday, February 10, 2011 6:21 PM
    Moderator

All replies

  • Hi fredConklin,

    Thanks for posting in the MSDN Forum.

    We are not able to show a presentation, but we are able to show the slides in the presentation.  I show the snippet of how to show slides of a power point file.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
    using Core = Microsoft.Office.Core;
    
    namespace OpenPPT
    {
      public partial class Form1 : Form
      {
        private string FileName = "";
        public Form1()
        {
          InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          openFileDialog1.ShowDialog();
          FileName = openFileDialog1.FileName;
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
          
          PowerPoint.Application application = new PowerPoint.Application();
          PowerPoint.Presentation presesntation = application.Presentations.Open2007(FileName, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue);
          PowerPoint.SlideShowSettings sst = presesntation.SlideShowSettings;
          sst.ShowType = Microsoft.Office.Interop.PowerPoint.PpSlideShowType.ppShowTypeSpeaker;
          sst.Run();
        }
      }
    }
    

    This is the solution sample which developed by Visual Studio 2008 in Windows XP sp3.

    http://cid-bfdf05e934413519.office.live.com/self.aspx/Sample%20Code%20for%20MSDN%20Forum/OpenPPT.rar 

    I hope it helps.

    Tom Xu


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 4, 2011 6:43 AM
    Moderator
  • Where does the "application.Presentations.Open2007" come from?  Specifically the OPEN2007.  This generates a compile error.  I am using VS2008.  My methods list only shows OPEN as an option.

    Error 1 'Microsoft.Office.Interop.PowerPoint.Presentations' does not contain a definition for 'Open2007' and no extension method 'Open2007' accepting a first argument of type 'Microsoft.Office.Interop.PowerPoint.Presentations' could be found (are you missing a using directive or an assembly reference?) C:\ManufacturingProcedureAutomation\ManufacturingProcedureAutomation\MPAStartForm.cs 390 48 ManufacturingProcedureAutomation

    Also the statement "using Core = Microsoft.Office.Core;"  causes the MsoTriState parameters to become unrecognized but using the "using Microsoft.Office.Core;" works.

    thanks for the attempt.  Hope you will keep on this.

    Fred

    Friday, February 4, 2011 3:24 PM
  • hi fedConklin,

    Could you run the execute file in the Tom's solution? I can run it! and when I complied this solution in Visual 2008 I found error at first, but I resolved it at last! At first I implement an error dll to reference the "Microsoft.Office.Core" this dll you can found in "COM" tab. I found the correct dll is in the Visual Studio folder. I think Tom must stand up to clarify these case. And fedConklin, I hope my description can help you for further research.

    Have a great day,

    Auguest

    Saturday, February 5, 2011 1:06 PM
  • Am I being told that there are 2 different  "Microsoft.Office.Core" files in the Visual Studio system? Doesn't this go against NameSpace rules?  How do I know what conditions require the one and not the other?

    I added the reference Object Library 12 from the COM tab (as done originally).  My choices are 10 or 12.  It will not allow me to do both.  I go back to my third question.

    Monday, February 7, 2011 5:09 PM
  • Hi fred

    PowerPoint's object model has various "Open" methods, one of them takes four parameters, another five. Open2007 is the latter and the additional parameter is to support new functionality (an OpenAndRepair parameter). The reason for this is because the Office object models don't support over-loading, so they're starting to accumulate multiple methods...

    It's important to realize that these are COM type libraries that are "translated" for .NET by Interop Assemblies. Your project should be picking up the PRIMARY IAs installed in the GAC by Office. You do have Office 2007 installed on this development machine? and you're also referencing the "12" library for PowerPoint?

    As to the "two different Core files": As a matter of fact, some versions of Visual Studio install their own sets of type libraires. Those are the ones in the .NET tab, while the ones in the COM tab are those that come with Office. I usually target the ones in the COM tab as I can be sure those will point to the PIAs in the GAC. I've had odd behavior going via the .NET tab, even though the end result is supposed to be the same.

    The following code works for me (button on a Windows Form):

        private void btnPPT_Click(object sender, EventArgs e)
        {
          Microsoft.Office.Interop.PowerPoint.Application pptApp = new Microsoft.Office.Interop.PowerPoint.Application();
          Microsoft.Office.Core.MsoTriState ofalse = Microsoft.Office.Core.MsoTriState.msoFalse;
    Microsoft.Office.Core.MsoTriState otrue = Microsoft.Office.Core.MsoTriState.msoTrue;
          pptApp.Visible = otrue;
          pptApp.Activate();
          Microsoft.Office.Interop.PowerPoint.Presentations ps = pptApp.Presentations;
          Microsoft.Office.Interop.PowerPoint.Presentation p = ps.Open(@"D:\AQHA\Wegbeschreibung\Reithallenplan.pptx", ofalse, ofalse, otrue);
          System.Diagnostics.Debug.Print(p.Windows.Count.ToString());
          MessageBox.Show(pptApp.ActiveWindow.Caption);
        }
    

    Cindy Meister, VSTO/Word MVP
    • Marked as answer by fredConklin Wednesday, February 9, 2011 4:17 PM
    Monday, February 7, 2011 7:07 PM
    Moderator
  • Yes it is office 2007.  And I did pick from the COM tab the '12' version.  I see you have changed to different code - I will need to update and get back.

    Thanks

    Fred

    Monday, February 7, 2011 9:53 PM
  • Hi fredConklin,

    If you select "Microsoft Office 12 Object Library" in the COM tab, you will find a "Microsoft.Office.Core" name space in it. However, this is not the namespace you need. You'd better select "Office" 12 version in the .NET tab to implement "Microsoft.Office.Core"

    I hope it helps,

    Auguest

    Tuesday, February 8, 2011 2:23 AM
  • Please see Cindy's comment above.  I have moved to the NET tab and will get to trying it at some point shortly.  THIS TAKES ME BACK to one of the original questions - HOW does one know which version of MULTIPLE namespaces to use?  This is not good.
    Wednesday, February 9, 2011 12:00 AM
  • Hi fredConklin,

    If you find the namespace “Interop.Microsoft.Office.Core” in the class view, it means you use the wrong assembly for the solution. There has a namespace “Microsoft.Office.Core” under the “Interop.Microsoft.Office”

    Friendly Regards,

    Auguest

    Wednesday, February 9, 2011 5:22 AM
  • Sorry - the question is more general.

    When developing any project that requires the use of "...Office.Core" HOW do you know which one of the myriad ".Cores" to use?

    Wednesday, February 9, 2011 4:08 PM
  • And I am still not seeing the OPEN2007 anywhere.
    Wednesday, February 9, 2011 4:10 PM
  • Cindy says use the COM tab, you say use the NET tab - neither seem to work
    Wednesday, February 9, 2011 4:12 PM
  • Cindy

    Your code worked regardless of the office.core used (com or net)  BUT I am still confused regarding GENERAL development guidelines as to which should be used and under what conditions.  It seems that you and Auguest have differing opinions on this.  If it's a don't care, tell me... If there are conditions that make one more optimal than another please list them.

    Wednesday, February 9, 2011 4:21 PM
  • Again thanks.

    But another question occurs to me.  The original code I wrote compiled properly without warning or error.  It was only at runtime that I got the PowerPoint frame error message.  What was wrong that i got that at runtime?

    Wednesday, February 9, 2011 5:19 PM
  • Hi Fred

    Sorry, I've hit a busy patch in the "real world"...

    To a certain extent, this is philosophical, but: Not all versions and editions of Visual Studio install a link that will appear in the .NET tab. You should always get a link in the COM tab. Since I answer a lot of questions and write books on Office programming (German language market), I make it a habit to use "the lowest common denominator".

    In any case, both of these should cause your project to link to the shared PIAs installed in the Windows GAC. So, in principle it shouldn't matter. On occasion, I've found things to be more stable linking to through the COM tab, but I can't recite you any exact cases at the moment.

    and by "stable" I mean if I open the project again in Visual Studio the reference might "go missing". Actually, thinking about it some more, it may have been project coming up from VS 2005 or VS 2008 to the next version (as I know I saw it when updating a book's sample code). Anything linked via the COM tab was still "found", but sometimes not things linked through the .NET tab. And this could make some sense as the version numbers might change with .NET components, but certainly won't with the "native" COM components.

    <<It was only at runtime that I got the PowerPoint frame error message.  What was wrong that i got that at runtime?>>

    I honestly don't know. My "gut feeling" is telling me that PowerPoint couldn't be found at that time. That's not an error I've seen often (if at all) before, so I've only "gut feeling" to go on...


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by fredConklin Friday, February 11, 2011 3:03 PM
    Thursday, February 10, 2011 6:21 PM
    Moderator
  • O.K.   Sort of what I expected.  Use COM because it is a more direct but both should (and did).  I am still wondering why my original code didn't work - but that is too much to ask.  One last thing that I have not been able to find documentation for - the @ symbol in the code starting

    ps.Open(@"D:\AQHA

    what does it do? 

    Take your time on this one - it's more a matter of when do I use its functionality as I do more C# (I'm begining to get the hang of it).

     

    Friday, February 11, 2011 3:12 PM
  • Hi Fred

    <<ps.Open(@"D:\AQHA>>

    The backslash character in C# is a so-called "escape character", meaning when it precedes certain other characters it has a special meaning. For example, \r is the equivalent of "carriage return" (ANSI 13), \t is a tab (ANSI 9). So in order to write a backslash, traditionally you had to double them: \\

    This can be a pain to remember, so a couple of versions back the @ was introduced to tell C# to not interpret the backslash as an escape character. Put it before the opening quote and you can write paths as you would in Windows, VB, etc.

    <<But another question occurs to me.  The original code I wrote compiled properly without warning or error.  It was only at runtime that I got the PowerPoint frame error message.  What was wrong that i got that at runtime?>>

    As I indicated in my previous reply, I don't know exactly what causes that error message to appear. my suspicion is/was that the PowerPoint application couldn't be accessed. It's possible that it somehow "got lost" mid-way through your code. IOW the code itself may not have been basically wrong, but something was keeping it from connecting with PowerPoint at the time. Again, I have no idea what that might have been. So please don't ask me this yet again :-)?


    Cindy Meister, VSTO/Word MVP
    Friday, February 11, 2011 5:15 PM
    Moderator
  • Hi Cindy,

    Not sure if this is still an active thread but I've got something similar I'm working on and it's giving me a lot of trouble.

    First off, I've got a Windows service application I've pieced together that opens a powerpoint in the background and converts the slides in to jpeg files. This works without any issues - here is the code for reference:

    protected void ConvertToJpegs()
        {
            Microsoft.Office.Interop.PowerPoint.Application app = new Microsoft.Office.Interop.PowerPoint.Application();
            Microsoft.Office.Core.MsoTriState ofalse = Microsoft.Office.Core.MsoTriState.msoFalse;
            Microsoft.Office.Core.MsoTriState otrue = Microsoft.Office.Core.MsoTriState.msoTrue;
            Presentation pptPresentation = null;
            app.Visible = otrue;
            app.Activate();
            try
            {
                pptPresentation = app.Presentations.Open(tempSavePath + fileName, ofalse, ofalse, ofalse);
                pptPresentation.SaveAs(tempSavePath + ".", PpSaveAsFileType.ppSaveAsJPG, ofalse);
                Thread.Sleep(500);
            }
            catch (Exception ex1)
            {
                throw ex1;
            }
            finally
            {
                pptPresentation.Close();
                app.Quit();
            }
        }

    Now however a new requirement is to have this done is a website. I'm using a .net framework 4 and V.Studio 2010. The application fails at the "Open" with "PowerPoint could not open the file." So I've built a test app with Windows forms and added the app.Visible, and app.Activate() statement to my code and it works. However, the Powerpoint application was opened, rather than ran in the background. I'm suspecting it was due to setting the fourth param in the Open method to msoTrue, as well as setting visibility to true and calling the Activate method.

    Is there a way for web application to open a powerpoint file in the background as in my service application? I'm beginning to think that I've reach the limitations of the web world, but I have not come across a valid confirmation. Every user has a PC with a standard build with Office 2010, I've also installed Office 2010 on my dev server (I know - bad) but I really wanted to make this work.

    Another issue that I need to figure out is, can I programmatically convert a pptx file to a ppsx file using the interop classes? Again, I can't find whole lot on this topic.

    I appreciate your time in advance!

    EricJM


    EJM

    Tuesday, November 27, 2012 9:49 PM