PowerPoint ExportAsFixedFormat in Powershell

2019-09-17 11:41发布

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)

2条回答
可以哭但决不认输i
2楼-- · 2019-09-17 11:58

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

查看更多
Fickle 薄情
3楼-- · 2019-09-17 12:05

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

查看更多
登录 后发表回答