开发者

Powerpoint could not open file

开发者 https://www.devze.com 2022-12-17 03:30 出处:网络
I am developping a C# programm which creates a PowerPoint presentation. However, I am running into a problem with the following instruction:

I am developping a C# programm which creates a PowerPoint presentation. However, I am running into a problem with the following instruction:

Presentation pres = pres_set.Open(path,
  Microsoft.Office.Core.MsoTriState.msoFalse,
  Mi开发者_Python百科crosoft.Office.Core.MsoTriState.msoTrue,
  Microsoft.Office.Core.MsoTriState.msoTrue);

This instruction works only sometimes. If it doesn't, it throws a exception with the message "PowerPoint could not open file". When I then manually open the template file, close it and execute the function again, most of the time it will execute correctly.

I have used the Microsoft Powerpoint 14.0 and the Microsoft Powerpoint 12.0 libraries, but both have the same problem.

Is there any way to avoid this strange problem?


The answer is much simpler: The Powerpoint-application expects an existing file.

I just had the same exception because I used a relative path, and PowerPoint tried to open the file relative to PowerPoint.exe instead of your Program.exe.

This problem can quickly be fixed by adding something like this before calling the open-method:

// Start Powerpoint
var powerpointApp = new Microsoft.Office.Interop.PowerPoint.Application();
// Get a fileInfo object to generate the full path
FileInfo fileInfo = new FileInfo(@"RelativeDir\YourPresentation.pptx");
// Use the full path
powerpointApp.Presentations.Open(fileInfo.FullName, MsoTriState.msoTrue, WithWindow: MsoTriState.msoFalse);


Have you tried not setting the TriState, like this?

Object oMiss = System.Reflection.Missing.Value; 
Presentation pres = pres_set.Open(ref path, ref oMiss, ref oMiss, ref oMiss); 


I had the same problem while trying to open an existing PowerPoint presentation until I found out that Visual Basic for Applications (VBA) was not installed on my Office installation options as described here:

http://www.debugging.com/bug/22261

Apparently, this problem only happens when working with PowerPoint as I have no problems manipulating Excel and Word files.

The problem´s gone away as soon as I repaired my Office, including the VBA to the installation.

Hope it helps!


I had a similar problem with PowerPoint. I found my Presentations.Open method was failing unless I had PowerPoint open.

One potential solution is to set PowerPointApplication.Visible = MsoTriState.msoTrue However, this will cause PowerPoint to physically open which is likely to be undesirable.

I resolved my issue by setting the final argument of the Open method to msoFalse, which specifies that the PowerPoint window should not open on the server which is more desirable.

Presentations.Open(inputFileName, 
                   MsoTriState.msoFalse, 
                   MsoTriState.msoTrue,
                   MsoTriState.msoFalse);

Take a look at this MSDN KB article for more information on the different parameters of the Open method.


        ProcessStartInfo ten_ct = new ProcessStartInfo();
        ten_ct.FileName = "POWERPNT.EXE";
        ten_ct.Arguments = @"D:\project\GiaoAn\GiaoAn\MyPpt.pptx";
        Process.Start(ten_ct);


I've seen problems like this with Excel, even when I try to launch it as a user with a command-line file to open. So it may not be anything wrong with your program. When I do this as a user, usually it works the second time.

So my advice would be either

1) For your program to try opening Power Point first without the file, wait (start with 5 seconds), and then have it try to load the file.

or

2) Your program can catch the exception, and simply try opening the file again if it fails (and if you find this works, you should add a maximum number of tries, so the program doesn't loop trying to do this all day). You could optionally also check to see if the file exists (if that's a possibility in your scenario - but it doesn't sound like this is currently the problem you are facing).


Try this:

Presentation pres = pres_set.Open(path, 
  Microsoft.Office.Core.MsoTriState.msoFalse, 
  Microsoft.Office.Core.MsoTriState.msoTrue, 
  Microsoft.Office.Core.MsoTriState.msoFalse);


I solve this problem using:

        var app = new PP.Application();
        PP.Presentation pres = null;
        try
        {
            Process.Start(inputFile);
            var presCol = app.Presentations;
            // Waiting for loading
            Thread.Sleep(2000);
            pres = presCol[inputFile];
            // Your code there
            // ...............
        }
        catch (System.Exception ex)
        {
            Log.Error(ex.Message);
            throw;
        }
        finally
        {
            if (pres != null)
            {
                pres.Close();
            }
            if (app != null)
            {
                app.Quit();
            }
            pres = null;
            app = null;
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消