How to use an MS Office 2007 Interop COM object from an executable binary CGI file?


I am creating an online application that is conform to the CGI 1.1 standard and I am using C# to create the binary executable CGI. I am using Apache as my web server on a machine running MS Vista. The C# code is using the MS Office PowerPoint 2007 Interop capabilities in order to modify a PPT file dynamically from the webpage.

I tried the code to modify the PPT file separately and it works fine but when I integrate it with the CGI executable and run it from my web frontend, MS PowerPoint runs and pops up an error message saying: "Microsoft Office PowerPoint has not been installed for the current user. Please run setup to install the application."

However, when I run PowerPoint manually, I don't get this error message.

I tried to reinstall Office 2007 Home and Student but with no success.

I have Administrator permissions for my current user.

If I had to guess, I think my problem is that Apache starts the CGI executable with some other user that the one that I am currently logged in into. But I don't know how to check if I guessed right and I don't know how to fix the problem.

Thank you.
Who is Participating?
MerlinMM3Connect With a Mentor Author Commented:
OK, I finally found the answer to my problem!!!!

Just as last suspected, the problem was related to the environment variables. When the CGI program is executed by the web server, the environment variables are different from the ones in the command prompt. In most cases, we don't care because CGI programs do not need system environment variables. But, in this case, PowerPoint required one that was not set.

To resolve this issue, what I did was setting my system environment variables at the beginning of the CGI program code using the following C# method:

I added such a statement for each of my system environment variables. From there on, PowerPoint didn't give me that evil message anymore.

After further investigations, I found that setting only "SystemDrive" at the beginning of my CGI program seemed a sufficient solution.

Now that I know the solution, I remember that executing my CGI program had the side-effect of creating a folder %SystemDrive% in the local directory of my CGI program. I didn't pay much attention to it, but now I see that if any folder name of the form %folder_name% appears in the local directory, it is likely that a system environment variables is not set.

Also, instead of modifying the CGI program, an alternative solution could be to configure Apache to pass the system environment variable to the CGI programs, but I have very little experience in configuring web server. For those interested in doing so, look for "PassEnv" in your favorite search engine.
// Add this line at the beginning of the C# code
System.Environment.SetEnvironmentVariable("SystemDrive", "C:");

Open in new window

1) Could you first of all post the shebang line from your CGI script file?

2) I presume that in the http.conf file you have the CGI directory marked with ExecCGI as in :-

<Directory "C:/Program Files/Apache Software Foundation/Apache2.2/CGI-bin">
    AllowOverride None
    Options None    <-- change this line to 'Options ExecCGI'.
    Order allow,deny
    Allow from all

and have an AddHandler statement for cgi?

3) Lastly I presume you installed Apache as default, in which case it runs (and its CGI) under the local system account which has no priveliges. You might need to create a special account for Apache and run it there. This tutorial should help (see the last part of the paragraph "Running Apache as a Service"
MerlinMM3Author Commented:
There is no shebang line since my CGI file is an binary executable. Also, I set up Apache by adding the option ExecCGI just like you specified. I set up a default directory for CGIs. However, I didn't put an AddHandler to my config but chose to use a .htaccess file instead.

That being said, my problem is not that the CGI is not executed. The CGI does return a properly generated HTML page. The problem occurs when the CGI executes PowerPoint. PowerPoint is launched but pops up the error message I mentioned in my first post.

I searched the net for this error message and found that some people experienced this issue when first launching Office for the first time after installation. There is actually a KB article released by Microsoft about this. But my problem is somewhat different. When I run PowerPoint manually (I double click the icon on the desktop), it runs just fine and I can start using it.

But when my CGI runs PowerPoint using Microsoft.Office.Interop.PowerPoint (version 12.0), PowerPoint pops up the evil message saying that PowerPoint is not installed for the current user.

I thought that maybe my CGI was launching PowerPoint using a Guest user account instead of using the user on which Apache was currently running on but I tried displaying the name of the Windows user executing the CGI and it really was the user with Administrator rights.

Despite of that, I think it has to do with permissions but I just don't know how to fix this. Maybe it's an Interop specific problem and maybe it has to do with the Home and Student edition of my Office.
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

OK, I just wanted to be sure Apache was OK.

It seems like, since you mention it is a Student edition, that it might only allow itself to start from the desktop. Try putting the desktop command into a .bat file and execute that from a cmd box (DOS box).
MerlinMM3Author Commented:
That was a good suggestion but sadly, that didn't work.

The problem is I can't see what the Target is for my desktop icon as it is hidden. See the attached picture file to see exactly how.

So I tried executing PowerPoint
- from the Windows Explorer (and my user have Administrative rights).
- from the automatically generated icon on the desktop
- from the custom-made icon on the desktop
- from the command prompt
- from a batch file started from the desktop
- from a batch file started from the command prompt

I tried the "Run as Administrator" option in the Windows Explorer and in my custom-made icon on the desktop. The option is not available for the automatically generated icon.
So how do you actually create an instance of PowerPoint in your program?
MerlinMM3Author Commented:
I create my CGI binary in C#.
In my project, I add a reference to "Microsoft PowerPoint 12.0 Object Library" (TypeLib Version 2.9).
Then I use code that is similar to the attached snippet.
using Microsoft.Office.Interop;
PowerPoint.Application app = new PowerPoint.Application();
PowerPoint.Presentation presentation = app.Presentations.Open(filename, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoTrue);
PowerPoint.Slide slide = presentation.Slides[0];

Open in new window

MerlinMM3Author Commented:
Now I'm thinking that the problem might be caused by the environment variables. When executing the CGI binary file, the environment vars are not the same as when I start it in the command prompt. Maybe launching PowerPoint from a CGI messes it up. I'll investigate this today and come back to you for the results.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.