PowerPoint 2007 SP2, ExportAsFixedFormat in PowerS

2019-06-04 20:13发布

问题:

Yesterday I was trying to batch convert a group of PPTs into PDFs for a friend, and I decided to have a look at PowerShell, since it's been sitting on my HD for a while.

Here's the code I've come 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's not the real problem I have
$p.visible = 1 

$f = $p.presentations.open('\some\file.ppt')

$f.ExportAsFixedFormat('\some\newfile.pdf', 2) 

2 is for PDF

Since the "brute force" method didn't 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 strange 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:

  • PpFixedFormatType Enumeration
  • ExportAsFixedFormat Method

Here's the full 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

回答1:

I've come across the same problem in Python. Try specifying PrintRange argument as said in solution by Stefan Schukat:

This is a bug in Powerpoint. It defines "[in, optional, defaultvalue(0)] PrintRange* PrintRange" which leads to the generation of "PrintRange=0" in the python wrapper. Therefore you'll get the error when calling the method. So no problem of makepy. Workaround call the method with PrintRange=None since None is a vali COM object. E.g. presentation.ExportAsFixedFormat(pptFile+'.pdf', win32com.client.constants.ppFixedFormatTypePDF, win32com.client.constants.ppFixedFormatIntentScreen, PrintRange=None) should work.

Source: Type mismatch when using export fuction of PowerPoint 2007


I don't know PowerShell at all but have worked out 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 isn't a full solution, but exporting is done. It somehow exports full presentation, not only slide no. 1, as I thought. P.S. Oh. Here's the same solution