Reputation: 123
i would like convert pps(x) or ppt(x) to PDF using C# and Microsoft.Office.Interop.PowerPoint. To do this, I use a method performing the following coding:
Microsoft.Office.Interop.PowerPoint.Presentation presentation = null;
Microsoft.Office.Interop.PowerPoint.Application application = null;
try
{
application = new Microsoft.Office.Interop.PowerPoint.Application();
presentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
presentation.SaveAs(targetPath, PpSaveAsFileType.ppSaveAsPDF, Microsoft.Office.Core.MsoTriState.msoTrue);
result = true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
result = false;
}
finally
{
if (presentation != null)
{
presentation.Close();
presentation = null;
}
if (application != null)
{
application.Quit();
application = null;
}
}
return result;
When the method is invoked for the first time, the ppsx is saved as pdf successfully. But when the method is invoked for another time, the following exception is raised on application = new Microsoft.Office.Interop.PowerPoint.Application();
The exception message is: Creating an instance of the COM component with CLSID {91493441-5A91-11CF-8700-00AA0060263B} from the IClassFactory failed due to the following error: 800706b5 The interface is unknown. (Exception from HRESULT: 0x800706B5).
Right before this exception is raised the console shows another exception "System.Runtime.InteropServices.COMException" in mscorlib.dll
.
When navigation into the interface Microsoft.Office.Interop.PowerPoint.Application
via F12, the GUID of this interface is 91493442-5A91-11CF-8700-00AA0060263B. So it is slightly different to the GUID inside the exception's message.
I would like to ask you, how to fix this issue?
PS: Microsoft Office 2010 is installed on this Notebook (running on Microsoft Win 7)
Thank you and best regards
Upvotes: 1
Views: 2951
Reputation: 31
You can try this code for converting any ppt file to pdf with interop and it works without any exception.Even there will not stay open COM object on the background processes.
Microsoft.Office.Interop.PowerPoint.Application app = new Microsoft.Office.Interop.PowerPoint.Application();
app.Visible = MsoTriState.msoTrue;
Presentations presentations = app.Presentations;
Presentation presentation = presentations.Open(fileLocation, ReadOnly: MsoTriState.msoCTrue);
presentation.ExportAsFixedFormat(outLocation, PpFixedFormatType.ppFixedFormatTypePDF);
Marshal.ReleaseComObject(presentations);
presentation.Close();
Marshal.ReleaseComObject(presentation);
app.Quit();
Marshal.ReleaseComObject(app);
Upvotes: 0
Reputation: 79
The following (arguably stupidsilly) workaround did the trick for me:
try
{
app = new Application();
}
catch (COMException)
{
app = new Application();
}
Edited: Thanks Jesse :)
Upvotes: 1
Reputation: 1
try closing the presentation and application before returning the value.
presentation.Close();
application.Quit();
otherwise the document and application remain opened and can't be accessed
Upvotes: 0