AlphaGuys
asked on
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(myFil ePath, 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( oPresentat ion) > 0) ;
oPresentation = null;
<snip>
while (Marshal.ReleaseComObject( oPowerPoin t) > 0) ;
oPowerPoint = null;
Next, I added explicit garbage collection at the end:
GC.Collect();
GC.WaitForPendingFinalizer s();
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?
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.
oPresentation.PrintOut(-1,
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 = null;
<snip>
while (Marshal.ReleaseComObject(
oPowerPoint = null;
Next, I added explicit garbage collection at the end:
GC.Collect();
GC.WaitForPendingFinalizer
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?
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
https://www.experts-exchange.com/questions/25020429/RPC-Server-fault-when-printing-PowerPoint-from-C-Net.html
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.
https://www.experts-exchange.com/questions/25020429/RPC-Server-fault-when-printing-PowerPoint-from-C-Net.html
http://support.microsoft.com/kb/302817