RPC Server fault when printing PowerPoint from C# .Net
Posted on 2010-01-05
I want to print PowerPoint documents from my C# .NET application.
Using the Automation interop libraries, I created this code that opens a PowerPoint presentation, prints it, then gets rid of the PowerPoint instance. I need to avoid leaving the open PowerPoint program on the user's desktop, and also avoid interfering with any existing instances that may already be open outside of my application.
public void PrintFile(string myFilePath)
Application oPowerPoint = new Application();
Presentation oPresentation = null;
oPresentation = oPowerPoint.Presentations.Open(myFilePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoTrue);
oPresentation.PrintOptions.PrintInBackground = MsoTriState.msoFalse;
oPresentation.PrintOut(-1, -1, "", 1, MsoTriState.msoCTrue);
while (Marshal.ReleaseComObject(oPresentation) > 0) ;
oPresentation = null;
while (Marshal.ReleaseComObject(oPowerPoint) > 0) ;
oPowerPoint = null;
This code works the first time, but on subsequent calls after that it intermittently throws "RPC Server is unavailable" or "RPC_E_SERVERFAULT" errors. This exception is usually thrown on the "PrintOut" statement, but not always.
How can I cleanly print PowerPoint documents from my C# application, regardless of how many documents need to be printed, and all without leaving open an instance of PowerPoint on the desktop?