• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1438
  • Last Modified:

Print PowerPoint document from a C# application

I want to print PowerPoint documents from my C# .NET application.

First, I tried to simply reference the Automation interop libraries like this:

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.PrintOut(-1, -1, "", 1, MsoTriState.msoCTrue);

      oPresentation.Close();
      oPresentation = null;
      oPowerPoint.Quit();
      oPowerPoint = null;
}

But that leaves an instance of PowerPoint open on the desktop  which I dont want. I added ReleaseComObject calls before nulling out my references to the Presentation and PowerPoint objects, but that didnt have any affect.

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

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

Next, I added explicit garbage collection at the end:

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

This closes the PowerPoint application, but it does it too soon! The application is closed before the print job is sent to the printer, so the print job is not completed.

How can I use PowerPoint to print presentations from my C# .Net code without leaving PowerPoint visible on the desktop?
0
AlphaGuys
Asked:
AlphaGuys
  • 2
1 Solution
 
Roshan DavisCommented:
Can you try any of the documented events listed in the following link?
http://support.microsoft.com/kb/302817
0
 
AlphaGuysAuthor Commented:
I don't think PowerPoint has any useful events like that. The link describes Word events only. PowerPoint does have a PresentationPrint event, but that's fired before the file is printed.
0
 
Miguel OzSoftware EngineerCommented:
Check that Check the oPowerPoint.Options.PrintBackground is set to false. This will cause your code to wait until printout sucessufully finish to print your document.
The default is true which is causing you this issue.
0
 
AlphaGuysAuthor Commented:
That does answer my question. Setting PrintInBackground to false and using my code to close PowerPoint after printing does successfully print without leaving PowerPoint open on the desktop.

But I've still got a problem dealing with PowerPoint files. I've opened up a separate question that describes another issue I have. I'd love any input you can give my on that one, too.

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_25020429.html
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now