2009-05-21 74 views
1

昨天我試圖批量將一組PPT轉換爲一個朋友的PDF文件,我決定看看PowerShell,因爲它一直在我的HD上。PowerPoint 2007 SP2,PowerShell中的ExportAsFixedFormat?

這是我想出的代碼。

$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

自「蠻力」方法不起作用(「類型不匹配」),我試圖用

$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 
+1

什麼是確切的錯誤?我查了MSDN,關於這個方法的文檔似乎對這個參數是錯誤的。而且我甚至無法在MSDN上找到枚舉! – JasonMArcher 2009-05-21 22:16:23

回答

1

我在Python中遇到過同樣的問題。嘗試在Stefan Schukat的解決方案中指定PrintRange參數:

這是Powerpoint中的一個錯誤。它定義了「[可選, 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


我不知道在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,正如我想的那樣。附:哦。這裏是一樣的solution

相關問題