Powershell 中的 PowerPoint 导出为固定格式

发布于 2024-09-30 10:08:28 字数 1242 浏览 1 评论 0原文

我尝试通过 PowerShell 2.0 脚本在 PowerPoint 2007 中使用 ExportAsFixedFormat。仅需要前两个参数,但这不起作用。

我总是得到:

使用“2”调用“ExportAsFixedFormat”时出现异常 参数:“类型不匹配。( HRESULT 异常:0x80020005 (DISP_E_TYPEMISMATCH))"

我读到必须指定所有参数才能使其正常工作,但这也不起作用。顺便说一句,同样的方法适用于我在 Word 2007 和 Excel 2007 中。

所以这是什么问题:

Add-type -AssemblyName Office
Add-type -AssemblyName Microsoft.Office.Interop.PowerPoint

$p = new-object -comobject powerpoint.application 
$p.visible = 1  
$document = $p.presentations.open('somefile.ppt')


$document.ExportAsFixedFormat($Path, 
[Microsoft.Office.Interop.PowerPoint.PpFixedFormatType]::ppFixedFormatTypePDF, 
[Microsoft.Office.Interop.PowerPoint.PpFixedFormatIntent]::ppFixedFormatIntentScreen, 
[Microsoft.Office.Core.MsoTriState]::msoFalse, 
[Microsoft.Office.Interop.PowerPoint.PpPrintHandoutOrder]::ppPrintHandoutVerticalFirst, 
[Microsoft.Office.Interop.PowerPoint.PpPrintOutputType]::ppPrintOutputSlides, 
[Microsoft.Office.Core.MsoTriState]::msoFalse, 
$null, 
[Microsoft.Office.Interop.PowerPoint.PpPrintRangeType]::ppPrintAll, 
[System.Reflection.Missing]::Value, 
$true, 
$true, 
$true, 
$true, 
$false, 
[System.Reflection.Missing]::Value)

I try to use the ExportAsFixedFormat in PowerPoint 2007 from a PowerShell 2.0 script. Only the first two arguments are required, but that won't work.

I always get:

Exception calling "ExportAsFixedFormat" with "2"
argument(s): "Type mismatch. (
Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"

I've read that all arguments have to be specified for it to function, but that doesn't work either. BTW, the same method works for me in Word 2007 and Excel 2007.

So what is wrong with this:

Add-type -AssemblyName Office
Add-type -AssemblyName Microsoft.Office.Interop.PowerPoint

$p = new-object -comobject powerpoint.application 
$p.visible = 1  
$document = $p.presentations.open('somefile.ppt')


$document.ExportAsFixedFormat($Path, 
[Microsoft.Office.Interop.PowerPoint.PpFixedFormatType]::ppFixedFormatTypePDF, 
[Microsoft.Office.Interop.PowerPoint.PpFixedFormatIntent]::ppFixedFormatIntentScreen, 
[Microsoft.Office.Core.MsoTriState]::msoFalse, 
[Microsoft.Office.Interop.PowerPoint.PpPrintHandoutOrder]::ppPrintHandoutVerticalFirst, 
[Microsoft.Office.Interop.PowerPoint.PpPrintOutputType]::ppPrintOutputSlides, 
[Microsoft.Office.Core.MsoTriState]::msoFalse, 
$null, 
[Microsoft.Office.Interop.PowerPoint.PpPrintRangeType]::ppPrintAll, 
[System.Reflection.Missing]::Value, 
$true, 
$true, 
$true, 
$true, 
$false, 
[System.Reflection.Missing]::Value)

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

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

发布评论

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

评论(3

﹉夏雨初晴づ 2024-10-07 10:08:28

我意识到这是一个迟到的答案,但我想我已经找到了解决方案。 (我试图使用 NetOffice 在 C# 中调用此方法,并得到相同的错误)

Microsoft Powerpoint 中似乎存在错误(至少在 v 2007 和 2010 中)。
必须指定PrintRange参数,因为默认值(0)无效!

因此,工作脚本可能如下所示:

Add-type -AssemblyName Office
Add-type -AssemblyName Microsoft.Office.Interop.PowerPoint

$p = new-object -comobject powerpoint.application 
$p.visible = 1  
$document = $p.presentations.open('somefile.ppt')
$ranges = $document.PrintOptions.Ranges
$range = $ranges.Add(1,1)


$document.ExportAsFixedFormat($Path, 
[Microsoft.Office.Interop.PowerPoint.PpFixedFormatType]::ppFixedFormatTypePDF, 
[Microsoft.Office.Interop.PowerPoint.PpFixedFormatIntent]::ppFixedFormatIntentScreen, 
[Microsoft.Office.Core.MsoTriState]::msoFalse, 
[Microsoft.Office.Interop.PowerPoint.PpPrintHandoutOrder]::ppPrintHandoutVerticalFirst, 
[Microsoft.Office.Interop.PowerPoint.PpPrintOutputType]::ppPrintOutputSlides, 
[Microsoft.Office.Core.MsoTriState]::msoFalse, 
$range, 
[Microsoft.Office.Interop.PowerPoint.PpPrintRangeType]::ppPrintAll, 
[System.Reflection.Missing]::Value, 
$true, 
$true, 
$true, 
$true, 
$false, 
[System.Reflection.Missing]::Value)

注意现在传入了 $range 参数。

注意 - 这个答案改编自此处的解决方案: https://netoffice.codeplex.com/discussions/449288

I realise this is a late answer, but I think I've got the solution. (I was trying to call this method in c# using NetOffice, and getting the same error)

It appears that there is a bug in Microsoft Powerpoint (At least in v 2007 & 2010).
The PrintRange parameter must be specified, because the default (0) is invalid!

So a working script might look like:

Add-type -AssemblyName Office
Add-type -AssemblyName Microsoft.Office.Interop.PowerPoint

$p = new-object -comobject powerpoint.application 
$p.visible = 1  
$document = $p.presentations.open('somefile.ppt')
$ranges = $document.PrintOptions.Ranges
$range = $ranges.Add(1,1)


$document.ExportAsFixedFormat($Path, 
[Microsoft.Office.Interop.PowerPoint.PpFixedFormatType]::ppFixedFormatTypePDF, 
[Microsoft.Office.Interop.PowerPoint.PpFixedFormatIntent]::ppFixedFormatIntentScreen, 
[Microsoft.Office.Core.MsoTriState]::msoFalse, 
[Microsoft.Office.Interop.PowerPoint.PpPrintHandoutOrder]::ppPrintHandoutVerticalFirst, 
[Microsoft.Office.Interop.PowerPoint.PpPrintOutputType]::ppPrintOutputSlides, 
[Microsoft.Office.Core.MsoTriState]::msoFalse, 
$range, 
[Microsoft.Office.Interop.PowerPoint.PpPrintRangeType]::ppPrintAll, 
[System.Reflection.Missing]::Value, 
$true, 
$true, 
$true, 
$true, 
$false, 
[System.Reflection.Missing]::Value)

notice the $range parameter is now passed in.

NB - this answer is adapted from the solution here: https://netoffice.codeplex.com/discussions/449288

墨洒年华 2024-10-07 10:08:28

参数存在两个问题:

  1. ExternalExporter使用正确的 PrintRange
  2. [System.Reflection.Missing]::Value 无效。跳过它。
Add-type -AssemblyName Office
Add-type -AssemblyName Microsoft.Office.Interop.PowerPoint

$ppt = New-Object -ComObject PowerPoint.Application
$presentation = $ppt.Presentations.Open($inputPath)
$printRange = $presentation.PrintOptions.Ranges.Add(1, $presentation.Slides.Count)

$presentation.ExportAsFixedFormat2(
    $outputPath, 
    [Microsoft.Office.Interop.PowerPoint.PpFixedFormatType]::ppFixedFormatTypePDF, # FixedFormatType
    [Microsoft.Office.Interop.PowerPoint.PpFixedFormatIntent]::ppFixedFormatIntentScreen, # Intent
    [Microsoft.Office.Core.MsoTriState]::msoTrue, # FrameSlides
    [Microsoft.Office.Interop.PowerPoint.PpPrintHandoutOrder]::ppPrintHandoutHorizontalFirstl, # HandoutOrder (ppPrintHandoutVerticalFirst)
    [Microsoft.Office.Interop.PowerPoint.PpPrintOutputType]::ppPrintOutputSlides, # OutputType (ppPrintOutputBuildSlides)
    [Microsoft.Office.Core.MsoTriState]::msoFalse, # PrintHiddenSlides
    $printRange, # PrintRange <-- use a proper printRange
    [Microsoft.Office.Interop.PowerPoint.PpPrintRangeType]::ppPrintAll, # RangeType
    [System.Reflection.Missing]::Value, # SlideShowName
    $false, # IncludeDocProperties
    $false, # KeepIRMSettings
    $false, # DocStructureTags
    $false, # BitmapMissingFonts
    $false, # PDF/A UseISO19005_1
    $true # includeMarkup 
    # ExternalExporter <-- skip this param completely
    )

$presentation.Close()
$ppt.Quit()

[System.GC]::Collect();
[System.GC]::WaitForPendingFinalizers();
[System.GC]::Collect();
[System.GC]::WaitForPendingFinalizers();

代码来自非常完整的示例 https://gist.github.com/allenyllee/5d7c4a16ae0e33375e4a6d25acaeeda2

There were two issues with the parameters:

  1. use a proper PrintRange
  2. for ExternalExporter, [System.Reflection.Missing]::Value is invalid. Just skip it.
Add-type -AssemblyName Office
Add-type -AssemblyName Microsoft.Office.Interop.PowerPoint

$ppt = New-Object -ComObject PowerPoint.Application
$presentation = $ppt.Presentations.Open($inputPath)
$printRange = $presentation.PrintOptions.Ranges.Add(1, $presentation.Slides.Count)

$presentation.ExportAsFixedFormat2(
    $outputPath, 
    [Microsoft.Office.Interop.PowerPoint.PpFixedFormatType]::ppFixedFormatTypePDF, # FixedFormatType
    [Microsoft.Office.Interop.PowerPoint.PpFixedFormatIntent]::ppFixedFormatIntentScreen, # Intent
    [Microsoft.Office.Core.MsoTriState]::msoTrue, # FrameSlides
    [Microsoft.Office.Interop.PowerPoint.PpPrintHandoutOrder]::ppPrintHandoutHorizontalFirstl, # HandoutOrder (ppPrintHandoutVerticalFirst)
    [Microsoft.Office.Interop.PowerPoint.PpPrintOutputType]::ppPrintOutputSlides, # OutputType (ppPrintOutputBuildSlides)
    [Microsoft.Office.Core.MsoTriState]::msoFalse, # PrintHiddenSlides
    $printRange, # PrintRange <-- use a proper printRange
    [Microsoft.Office.Interop.PowerPoint.PpPrintRangeType]::ppPrintAll, # RangeType
    [System.Reflection.Missing]::Value, # SlideShowName
    $false, # IncludeDocProperties
    $false, # KeepIRMSettings
    $false, # DocStructureTags
    $false, # BitmapMissingFonts
    $false, # PDF/A UseISO19005_1
    $true # includeMarkup 
    # ExternalExporter <-- skip this param completely
    )

$presentation.Close()
$ppt.Quit()

[System.GC]::Collect();
[System.GC]::WaitForPendingFinalizers();
[System.GC]::Collect();
[System.GC]::WaitForPendingFinalizers();

Code is from very complete example at https://gist.github.com/allenyllee/5d7c4a16ae0e33375e4a6d25acaeeda2

泡沫很甜 2024-10-07 10:08:28

$null 更改为 [System.Reflection.Missing]::Value

Change $null to [System.Reflection.Missing]::Value.

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