【发布时间】:2010-10-27 23:29:10
【问题描述】:
昨天我试图将一组 PPT 批量转换为一个朋友的 PDF,我决定看看 PowerShell,因为它已经在我的 HD 上放置了一段时间。
这是我想出的代码。
$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)
由于“蛮力”方法不起作用(“类型不匹配”),我尝试使用
导入枚举类型$pptypepdf= [Microsoft.Office.Interop.PowerPoint.PpFixedFormatType]::PpFixedFormatTypePDF
$f.ExportAsFixedFormat('\some\newfile.pdf', $pptypepdf)
这里奇怪的是,它仍然抛出“类型不匹配”的错误......
另外,SaveAs 也适用于
$f.SaveAs('\some\newfile.pdf', 32) # 32 is for PDF
我做错了什么?
更新
相关文档:
这是完整的错误信息
$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
【问题讨论】:
-
确切的错误是什么?我查了 MSDN,关于这个方法的文档对于这个论点似乎是错误的。而且我什至在 MSDN 上都找不到那个枚举!
-
我已经用相关的细节编辑了问题,谢谢
-
您是否尝试过提供所有可选参数的调用?
-
当缺少“可选”参数时,PowerPoint 的 ExportAsFixedFormat 将返回“类型不匹配”。只有提供了所有参数,Invoke 返回的 puArgErr 值才有意义。
-
请查看我在这里提供的解决方案:link 看看是否可行!
标签: pdf com powershell powerpoint office-2007