RPC Server fault when printing PowerPoint from C# .Net

Posted on 2010-01-05
Last Modified: 2013-12-17
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?
Question by:AlphaGuys
    LVL 35

    Expert Comment

    by:Miguel Oz
    This is a COM related error
    Please provide more details:
    PC -OS/.net/vs/Office or PIA version.
    Have you restarted your PC?
    Is this code running on the main thread or in a background thread? (Office COM based code needs to run in STA mode - main thread)

    How many print operations you are expecting per minute/hour or day?
    Can it be grouped as a batch?( so you open powerpoint once for many print operations)

    Note:  Also you need to put try/finally constructs when you create and destroy oPowerPoint and oPresentation to make sure the COM references are properly taken care of.

    Author Comment

    The error occurs on more than one environment, but Im trying to debug it on a machine with:
    Windows XP Pro, using Visual Studio 2008, .Net 3.5 (SP1), and Office 2007

    This problem started when we upgraded from Office2003 to Office2007. Ive been running this code successfully for years with older versions of Office without any problem. Im not having problems with Word or Excel documents, even in this newer version of Office. Only PowerPoint 2007 is throwing these errors.

    I am, in fact, trying to print on a background thread (created by the BackgroundWorker object). Is there some documentation somewhere that states that Offcie COM needs to run on the main thread? That could be a giant problem for me. Although, as I said, Excel and Word do not exhibit the same problem even though they, too, are invoked from a background thread.
    LVL 35

    Accepted Solution

    Quoting from link:
    "Office applications are still COM based (COM servers) working in STA model. Microsoft hasn't made them to be server-side friendly and it may not happen, at least in near future. OK, the Office interoperability library provided by Microsoft is intended for desktop Windows applications only.
    The object model is not thread-safe and naturally not suited for multi-threaded web server environment.
    Office applications are designed to be desktop-based applications and assume the availability of user interactivity (e.g. popping up a dialog box and waiting for the user to respond to it). Since web applications run in non-interactive mode, an Office application popping up a dialog box, will just keep waiting (hanging) because the dialog box won't be visible and hence impossible to respond to it. The end-result is unpredictable.
    Deriving from 1 & 3, concurrent requests to Office object model will be serialized for unknown periods of time. This affects the web application's stability, performance and scalability adversely. Web servers will start issuing HTTP/500 status code to client requests once the request queue becomes full.
    Too many instances of Office applications consume more memory and have the potential for threading and shared-resource related issues.
    An alternative will be to use a queue to queue up all your printing requests and then this this queue can be read and print executed in a separate AppDomain. (Unfortnately I do not have the code for this)

    Author Comment

    I'm not convinced that having Office on a separate thread is the cause of my problem. In any event, it's not practical for me to move the printing function to the main thread or to a separate queue. I really need to have the main UI to be responsive while printing occurs (and, of course, to have the printing occur as fast as possible).

    I’ve looked at a lot of third-party tools (e.g., Viscomsoft, Aspose, …) for this and couldn’t find any that support printing of PowerPoint documents, so I suspect this is not such an easy thing to do.

    I found a workaround that I can use: PowerPoint allows saving of presentations to Tiffs (or PDFs or…). The Save As function doesn’t seem to cause the same RPC failure as the Print function, so I’m saving my files as Tiffs and then manipulating and printing those files as needed.

    Author Closing Comment

    Thanks for the information, but it didn't really help me to solve my problem except to prod me towards looking for a solution that did not involve printing from PowerPoint.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Setting the Scene Animations in PowerPoint are a great tool to convey messages when used carefuly with the content of your slides. There are plenty of animation effects and options, including a Repeat feature for individual animation effects. …
    Many programs have tried to outwit PowerPoint in terms of technology and skill. These programs, however, still lack several characteristics that PowerPoint has possessed from the start. Here's why PowerPoint replacements won't entirely work for desi…
    The viewer will learn how to edit animations within the presentation, incorporate sound, and set everything up with timing.
    The viewer will learn how to edit text. This includes Font, Spacing, Resizing, Color, and other special text options.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now