?
Solved

RPC Server fault when printing PowerPoint from C# .Net

Posted on 2010-01-05
5
Medium Priority
?
1,244 Views
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;
      oPowerPoint.Activate();
      oPresentation = oPowerPoint.Presentations.Open(myFilePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoTrue);
      oPresentation.PrintOptions.PrintInBackground = MsoTriState.msoFalse;

      oPresentation.PrintOut(-1, -1, "", 1, MsoTriState.msoCTrue);

      oPresentation.Close();
      while (Marshal.ReleaseComObject(oPresentation) > 0) ;
      oPresentation = null;

      oPowerPoint.Quit();
      while (Marshal.ReleaseComObject(oPowerPoint) > 0) ;
      oPowerPoint = null;

      GC.Collect();
      GC.WaitForPendingFinalizers();
}


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?
0
Comment
Question by:AlphaGuys
  • 3
  • 2
5 Comments
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 26187159
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.
0
 

Author Comment

by:AlphaGuys
ID: 26190014
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.
0
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 1500 total points
ID: 26195162
Check:
http://msmvps.com/blogs/siva/archive/2009/03/22/server-side-automation-of-ms-office-applications.aspx
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)
0
 

Author Comment

by:AlphaGuys
ID: 26370186
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.
0
 

Author Closing Comment

by:AlphaGuys
ID: 31672896
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.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video teaches viewers how to add transitions to their Slideshows and how to set up timing for the transitions.
The viewer will learn how to edit the master slide. They will also learn how to combine multiple themes into one master slide to use them in their presentation.
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 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