views:

424

answers:

2

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,
  Microsoft.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?

A: 

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).

Andy Jacobs
Do you mean by opening Power Point this code Application app = new Application(); app.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;or do I have to open the powerpoint.exe somewhere in the programm files folder?
sander
You should try both (if needed), the first obviously being easier to try.
Andy Jacobs
A: 

Try this:

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