Problems with office automation in asp.net. I can use alternatives such as open-office, if I knew how
Asked Answered
P

6

5

I have a ASP.NET 2.0 web application that should upload a ppt file and then extract its slides to images. For that I have imported office.dll and Microsoft.Office.Interop.PowerPoint.dll assemblies and wrote the following code

public static int ExtractImages(string ppt, string targetPath, int width, int height)
    {
        var pptApplication = new ApplicationClass();

        var pptPresentation = pptApplication.Presentations.Open(ppt, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);

        var slides = new List<string>();

        for (var i = 1; i <= pptPresentation.Slides.Count; i++)
        {
            var target = string.Format(targetPath, i);
            pptPresentation.Slides[i].Export(target, "jpg", width, height);
            slides.Add(new FileInfo(target).Name);
        }

        pptPresentation.Close();

        return slides.Count;
}

If I run this code in my local machine, in asp.net or a executable, it runs perfectly. But If I try running it in the production server, I get the following error:

System.Runtime.InteropServices.COMException (0x80004005): PowerPoint could not open the file. at Microsoft.Office.Interop.PowerPoint.Presentations.Open(String FileName, MsoTriState ReadOnly, MsoTriState Untitled, MsoTriState WithWindow) at PPTImageExtractor.PptConversor.ExtractImages(String caminhoPpt, String caminhoDestino, Int32 largura, Int32 altura, String caminhoThumbs, Int32 larguraThumb, Int32 alturaThumb, Boolean geraXml) at Upload.ProcessRequest(HttpContext context)

The process is running with the user NT AUTHORITY\NETWORK SERVICE. IIS is configured to use anonymous authentication. The anonymous user is an administrator, I set it like this to allow the application to run without having to worry about permissions.

In my development machine I have office 2010 beta1. I have tested with the executable in a pc with office 2007 as well. And if I run the code from the executable in the server, with office 2003 installed, it runs perfectly.

To ensure that there wouldn't be any problems with permissions, everyone in the server has full access to the web site. The website is running in IIS7 and Classic Mode.

I also heard that Open-office has an API that should be able to do this, but I couldn't find anything about it. I don't mind using DLLImport to do what I have to do and I can install open-office on the web server. Don't worry about rewriting this method, as long as the parameters are the same, everything will work.

I appreciate your help.

Pregnant answered 16/4, 2010 at 14:37 Comment(1)
Don't ever use Office Interop from an ASP.NET application. See Considerations for server-side Automation of OfficeErythropoiesis
V
10

Don't ever use Office Interop from an ASP.NET application. See Considerations for server-side Automation of Office.

However, if you have no alternative, on Windows Server 2008 you need to create the following directories to get this to work:

Windows 2008 Server x64: C:\Windows\SysWOW64\config\systemprofile\Desktop Windows 2008 Server x86: C:\Windows\System32\config\systemprofile\Desktop

Vagrant answered 19/5, 2010 at 6:9 Comment(2)
You don't know how much frustration this issue has caused us. I would like to buy you a beer if you are ever in the Denver area. Thanks.Grigg
Hm. that did not work for Server 2008 SP2. I've executed the application on another server (R2) and it worked fine.Leija
C
1

When considering using Office Automation on the server, you might want to take a read through the info in the following KB article.

http://support.microsoft.com/kb/257757

A key phrase from the above link

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

And I can confirm that the above is very true, and not just MS trying to discourage you...

Cowhide answered 17/4, 2010 at 18:1 Comment(1)
yeah, if you automate them directly, you may consider a babysitting process to restart office crap when it fails.Leija
S
1

In my situation (converting presentation to video, and I have very similar problem with open stuff) this helps:

Creating the following directories:

C:\Windows\SysWOW64\config\systemprofile\Desktop

C:\Windows\System32\config\systemprofile\Desktop (I don't have this)

My operating system Windows Server 2012 R2 Standard

Sawfish answered 4/6, 2014 at 10:6 Comment(0)
C
0

The Interop libraries require that the application in question be installed on the machine from which they are running. I.e., in order for this to work on your production server, you will need to have Office installed on that server. This is generally an awful idea. There are many issues with type of setup as each time someone makes a request, a new instance of the application in question (in your case PowerPoint) is launched.

If you want to parse PowerPoint files on a central server, then you should use a library that reads PowerPoint files. I know that Aspose makes such product (Aspose.Slides) but there are others as well.

If the documents are in PowerPoint 2007, you might be able to parse the Open Office XML for the data you want. See Introducing the Office (2007) Open XML File Formats and Open XML SDK 2.0 for Microsoft Office for more.

Cottage answered 16/4, 2010 at 15:9 Comment(3)
good links to open office stuff... shouldn't need external libraries to deal with 2007+ office files, although they might make it easier.Veery
the server already has office 2003 installed and we only accept upload of ppt files, not pptx. Thats why I was considering using Open-Office.Pregnant
yeah, you wouldn't want each request to invoke the interop/com. You'd rather use a background service like a "watch" folder so there's no traffic accidents.Leija
V
0

I might be wrong, started looking into a similar thing recently. But you should not need office installed on a machine to create or edit 2007+ office files. They are basically zip files with a different extention. If you rename it to .zip you should see all the files and be able to edit them directly.

It can then be unzipped and all the files are there for you. You can create new files in a similar fashion

Veery answered 16/4, 2010 at 16:21 Comment(1)
Reading the xml files doesn't really help export the slides as images, unless you can construct them in the same waySerious
E
0

Don't ever use Office Interop from an ASP.NET application. See Considerations for server-side Automation of Office.

However, I just wanted to note here that I had to do the fix described by Murthy Pantham on a Windows 7 Professional 64 bit machine with Office 2010. It did not have Windows 7 Service Pack 1 or Office 2010 Service Pack 3 installed. So the lesson here is apparently that fix is not limited to Server 2008. Hope this helps someone.

Excise answered 2/12, 2011 at 15:56 Comment(1)
Doesn't the edit change the meaning of my response? Not sure why it was necessary to add "Don't ever use Office Interop from an ASP.NET application. See Considerations for server-side Automation of Office."Excise

© 2022 - 2024 — McMap. All rights reserved.