PowerPoint 2007 SP2,PowerShell中的ExportAsFixedFormat?

时间:2022-01-01 13:06:00

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.

昨天我试图将一组PPT批量转换成朋友的PDF格式,我决定看一下PowerShell,因为它已经在我的HD上待了一段时间。

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

2是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

此外,SaveAs可以正常使用

$f.SaveAs('\some\newfile.pdf', 32) # 32 is for PDF

What am I doing wrong?

我究竟做错了什么?

Update

Relevant documentation:

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 个解决方案

#1


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

我在Python中遇到了同样的问题。尝试使用Stefan Schukat在解决方案中指定的PrintRange参数:

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.

这是Powerpoint中的一个错误。它定义了“[in,optional,defaultvalue(0)] PrintRange * PrintRange”,它导致在python包装器中生成“PrintRange = 0”。因此,调用方法时会出现错误。所以没有makepy的问题。解决方法使用PrintRange = None调用该方法,因为None是一个vali COM对象。例如。 presentation.ExportAsFixedFormat(pptFile +'。pdf',win32com.client.constants.ppFixedFormatTypePDF,win32com.client.constants.ppFixedFormatIntentScreen,PrintRange = None)应该有效。

Source: Type mismatch when using export fuction of PowerPoint 2007

来源:使用PowerPoint 2007的导出功能时类型不匹配


I don't know PowerShell at all but have worked out a working example:

我根本不了解PowerShell,但已经找到了一个有效的例子:

$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

这不是一个完整的解决方案,但导出已完成。它以某种方式导出完整的演示文稿,不仅仅是幻灯片。 1,正如我所想。附:哦。这是同样的解决方案

#1


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

我在Python中遇到了同样的问题。尝试使用Stefan Schukat在解决方案中指定的PrintRange参数:

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.

这是Powerpoint中的一个错误。它定义了“[in,optional,defaultvalue(0)] PrintRange * PrintRange”,它导致在python包装器中生成“PrintRange = 0”。因此,调用方法时会出现错误。所以没有makepy的问题。解决方法使用PrintRange = None调用该方法,因为None是一个vali COM对象。例如。 presentation.ExportAsFixedFormat(pptFile +'。pdf',win32com.client.constants.ppFixedFormatTypePDF,win32com.client.constants.ppFixedFormatIntentScreen,PrintRange = None)应该有效。

Source: Type mismatch when using export fuction of PowerPoint 2007

来源:使用PowerPoint 2007的导出功能时类型不匹配


I don't know PowerShell at all but have worked out a working example:

我根本不了解PowerShell,但已经找到了一个有效的例子:

$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

这不是一个完整的解决方案,但导出已完成。它以某种方式导出完整的演示文稿,不仅仅是幻灯片。 1,正如我所想。附:哦。这是同样的解决方案