user773737
user773737

Reputation:

Automated Powerpoint process won't work unless I manually start up Powerpoint?

I have this C# console application trying to use Powerpoint 2013 to convert a file from ppt to pdf. From a cold boot, it crashes with an error:

    System.Runtime.InteropServices.ComException:Error HRESULT E_FAIL has been returned from a call to a COM component. 
    at Microsoft.Office.Interop.PowerPoint.Presentations.Open(String FileName, MsoTriState ReadOnly, MsoTriState Untitled, MsoTriState WithWindow)
    at ConsoleApplication4.Program.Main(String[] args)
    --- End of inner exception stack trace ---
    at ConsoleApplication4.Program.Main(String[] args)

If I just open Powerpoint to a new file, it won't work.

I have to manually open up a ppt with information in it, and then it'll work.

Is there anything I can do to bypass the manual opening of a ppt file before my console program will work? I'm trying to implement this as an automatic process on a server, and I can't be going in an manually opening up a file every time the server restarts.

The code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            String sourceFilename = args[0];
            String destinationFileName = args[1];

            if (!File.Exists(sourceFilename))
            {
                throw new FileNotFoundException(string.Format("The specified file {0} does not exist.", sourceFilename), sourceFilename);
            }

            try
            {
                Microsoft.Office.Interop.PowerPoint.Application app = new Microsoft.Office.Interop.PowerPoint.Application();

                app.Presentations.Open(sourceFilename,Microsoft.Office.Core.MsoTriState.msoFalse,Microsoft.Office.Core.MsoTriState.msoTrue,Microsoft.Office.Core.MsoTriState.msoFalse).SaveAs(destinationFileName, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPNG);
                app.Quit();
            }
            catch (Exception e)
            {
                throw new Exception(string.Format("Unable to convert {0} to {1}", sourceFilename, destinationFileName), e);
            }
        }
    }
}

I tried to add this line:

app.Presentations.Open(sourceFilename, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse).Close();

Before the SaveAs line, in an attempt to open and close the presentation, but upon a fresh boot, this doesn't fix the problem.

I also tried to add: app.Activate(), and app.Visible = MsoTriState.msoTrue and it opens a blank powerpoint window before crashing.

Upvotes: 1

Views: 1888

Answers (2)

user773737
user773737

Reputation:

I found that I didn't install VBA (Visual Basic For Application) when I installed powerpoint, so I went back and did that, and it cleared up the errors when running the program from the command line. I'm still having issues running it from C# though.

Also, you'll get a different error if you try to run the compiled file if you don't have .NET 4.5 installed

Upvotes: 0

Rik
Rik

Reputation: 2032

Did you double escape the \ in sourceFilename? You need to replace the \ with \\ or add a @ at the beginning of the sourceFilename.

From here:

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

Edit:

Could you try the following within your try and see where it crashes:

try
{
  Microsoft.Office.Core.MsoTriState ofalse = Microsoft.Office.Core.MsoTriState.msoFalse;
  Microsoft.Office.Core.MsoTriState otrue = Microsoft.Office.Core.MsoTriState.msoTrue;
  Microsoft.Office.Interop.PowerPoint.Application app = new Microsoft.Office.Interop.PowerPoint.Application();
  app.Visible = otrue
  app.Activate();
  Microsoft.Office.Interop.PowerPoint.Presentations ps = app.Presentations;
  Microsoft.Office.Interop.PowerPoint.Presentation p = ps.Open(sourceFilename,ofalse,otrue,otrue);
  p.SaveAs(destinationFileName, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPNG);
  app.Quit();
}

Does it still crash on the open-line? I played a little with the parameters in open. I think the WithWindow needs to be true but you can try for yourself.

You could also try adding that @ before the filename without escaping the escape characters and see if that helps.

Upvotes: 2

Related Questions