PowerPoint 2007 SP2, ExportAsFixedFormat in PowerShell?
Yesterday I was trying to convert a group of PPTs to PDFs for a friend and I decided to take a look at PowerShell as it has been sitting on my HD for a while.
Here is the code I came up with.
$p = new-object -comobject powerpoint.application
# I actually don't know why I have to set the window to visible,
# but it doesn't work otherwise, anyway, it not the real problem I have
$p.visible = 1
$f = $p.presentations.open('\some\file.ppt')
$f.ExportAsFixedFormat('\some\newfile.pdf', 2)
Since the "brute force" method did not work ("type mismatch"), I tried to import the enum type with
$pptypepdf= [Microsoft.Office.Interop.PowerPoint.PpFixedFormatType]::PpFixedFormatTypePDF
$f.ExportAsFixedFormat('\some\newfile.pdf', $pptypepdf)
The weird thing here is that it still throws a "type mismatch" error ...
Also, SaveAs works fine with
$f.SaveAs('\some\newfile.pdf', 32) # 32 is for PDF
What am I doing wrong?
Update
Relevant documentation:
Here's the complete error message
$pptypepdf= [Microsoft.Office.Interop.PowerPoint.PpFixedFormatType]::PpFixedFormatTypePDF
$f.ExportAsFixedFormat($filepath, $pptypepdf)
Exception calling "ExportAsFixedFormat" with "2" argument(s): "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"
At line:1 char:23
+ $f.ExportAsFixedFormat <<<< ($filepath, $pptypepdf)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
a source to share
I faced the same problem in Python. Try to provide an argument PrintRange
as stated in Stefan Shukat's solution:
This is a bug in Powerpoint. It defines "[in, optional, defaultvalue (0)] PrintRange * PrintRange" which will generate "PrintRange = 0" in the python shell. Therefore, you will get when you call the method. So there is no problem with makepy. A temporary solution is to call the method using the PrintRange = None parameter, since None is a COM object vali. For instance. presentation.ExportAsFixedFormat (pptFile + 'PDF', win32com.client.constants.ppFixedFormatTypePDF, win32com.client.constants.ppFixedatIntentScreen, PrintRange = None) should work.
Source: Type Mismatch When Using PowerPoint 2007 Export
I don't know PowerShell at all, but developed a working example:
$p.ActivePresentation.PrintOptions.Ranges.Add(1,1)
$r = $p.ActivePresentation.PrintOptions.Ranges.Item(1)
$document.ExportAsFixedFormat('D:\\ps.pdf', 2, 1, 0, 1, 1, 0, $r)
This is not a complete solution, but the export is done. This somehow exports the full presentation, not just the slide. 1 as I thought. Postscript Oh. Here is the solution
a source to share