PowerPoint 2007 SP2,PowerShell 中的 ExportAsFixedFormat?

发布于 2024-07-22 04:35:24 字数 1782 浏览 5 评论 0原文

昨天,我试图为朋友将一组 PPT 批量转换为 PDF,我决定看看 PowerShell,因为它在我的硬盘上已经有一段时间了。

这是我想出的代码。

$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 适用于 PDF

“暴力”方法不起作用(“类型不匹配”)我尝试导入枚举类型

$pptypepdf= [Microsoft.Office.Interop.PowerPoint.PpFixedFormatType]::PpFixedFormatTypePDF
$f.ExportAsFixedFormat('\some\newfile.pdf', $pptypepdf) 

这里奇怪的是它仍然抛出“类型不匹配”错误...

此外,SaveAs 可以很好地使用

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

What am I做错了吗?

更新

相关文档:

  • PpFixedFormatType Enumeration
  • < a href="http://msdn.microsoft.com/en-us/library/bb231096.aspx" rel="nofollow noreferrer">ExportAsFixedFormat 方法

这是完整的错误消息

$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

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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄断弥空 2024-07-29 04:35:24

我在Python中也遇到过同样的问题。 尝试指定 PrintRange 参数,如 Stefan Schukat 的解决方案中所述:

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

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


我不知道 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)

这不是完整的解决方案,但导出已完成。 它以某种方式导出完整的演示文稿,而不仅仅是幻灯片编号。 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文