Out of Memory failure automating VSTO Powerpoint A

2019-07-21 11:35发布

问题:

I am using the VSTO Powerpoint API to automate the creation of Powerpoint files. What I have found is that using the most basic call to create a Powerpoint file from a web application on a Windows 7 system causes an out of memory error (other VSTO calls that manipulate an open file also fail):

PowerPoint.Presentation presentation = _ppPresentations.Add(Office.MsoTriState.msoTrue);
        string outputDirectory= getOutputDirectory();
        presentation.SaveAs(outputDirectory+ "\\CreateSlideTestOutput.pptx",
                            PowerPoint.PpSaveAsFileType.ppSaveAsDefault, Office.MsoTriState.msoTrue);

The error that I receive is "Insufficient Memory" at whatever VSTO API call is being made to manipulate a file (for instance, saving or adding a slide to a powerpoint).

However, I found that if I run the same code in a unit test, or as a user executable (.exe), I have no problems adding content, manipulating and saving Powerpoint files.

But then, when I tried to create a Windows service to automate the same code used in my user executable, I am receiving an Insufficient Memory error again. I can set a breakpoint in my service and see that it is failing on the exact same VSTO API calls that failed when called from my web application.

Does anyone have an explanation why the user executable thread can execute VSTO API calls without any issue what so ever (and no observable jump in memory) whereas my App pool thread, or Windows service thread always runs into Insufficient Memory errors?

回答1:

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

Automating Office documents (with the Interop .dlls) without a user context isn't "stability" supported by MS. They advise to use another solution like the OpenXML SDK:

http://www.microsoft.com/en-us/download/details.aspx?id=5124