2015-12-29 175 views
3

有人可以请指导我该怎么解决代码?我需要将PowerPoint文件转换为PDF文件。将ppt转换为PDF时出错

代码:

#Convert Powerpoint formats to pdf 
Param(
    [string]$inputPath, 
    [string]$outputPath 
) 
Add-Type -AssemblyName Office 
Add-Type -AssemblyName Microsoft.Office.Interop.PowerPoint 
$ppFormatPDF = 32 

$ppQualityStandard = 0 
$pp = New-Object -ComObject PowerPoint.Application 
# TODO: Why this property does not work 
#$pp.visible = [Microsoft.Office.Core.MsoTriState]::msoFalse 

$ppt = $pp.Presentations.Open($inputPath) 
$ppt.SaveAs($outputPath, $ppFormatPDF) # 32 is for PDF 
$ppt.Close() 
$pp.Quit() 
$pp = $null 
[gc]::Collect() 
[gc]::WaitForPendingFinalizers() 

错误:

Exception calling "SaveAs" with "2" argument(s): "Presentation.SaveAs : 
PowerPoint can't save ^0 to ^1." 
At D:\AllAquent\Rambo\Digo\war\WEB-INF\classes\resources\pptToPdf.ps1:17 char:12 
+ $ppt.SaveAs <<<< ($outputPath, $opt) # 32 is for PDF 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : ComMethodTargetInvocation 
+0

您的错误消息与您的代码不符。 '$ opt'的值是否真的是32?您的PowerPoint版本是否实际支持以PDF格式保存? –

回答

0

用于this script几个月前,在这里发帖之前除去我自己的修改。希望这可以帮助你!

请注意,此脚本旨在将指定目录中的所有PowerPoint演示文稿转换为PDF。

Function Convert-PptxToPDF { 

[CmdletBinding()] 
Param( 
$File, 
$OutputFile 
) 

# add key assemblies 
Add-type -AssemblyName office -ErrorAction SilentlyContinue 
Add-Type -AssemblyName microsoft.office.interop.powerpoint -ErrorAction SilentlyContinue 

# Open PowerPoint 
$ppt = new-object -com powerpoint.application 
$ppt.visible = [Microsoft.Office.Core.MsoTriState]::msoFalse 


# Open the $File presentation 
$pres = $ppt.Presentations.Open($file) 

# Now save it away as PDF 
$opt= [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType]::ppSaveAsPDF 
$pres.SaveAs($OutputFile,$opt) 

# and Tidy-up 
$pres.Close() 
$ppt.Quit() 
$ppt=$null 

} 

#Where your PDF will be saved 
$OutputFile = "C:\Temp\" 

# File-extension could be changed to .pptx if needed 
Foreach ($File in $(ls $OutputFile -Filter "*.ppt")) { 
    # Build name of output file 
    $pathname = split-path $File 
    $filename = split-path $File -leaf 
    $rmfileext  = $filename.split(".")[0] 
    $OutputFile = $pathname + $rmfileext + ".pdf" 

    # Convert _this_ file to PDF 
    Convert-PptxToPDF -file $File -OutputFile $OutputFile 
} 

我对这个剧本没有功劳。

+0

好的..谢谢..我会尽力让你知道。 – User3091