我试图使用PowerShell做文字DOCX的批量转换为PDF - 使用本网站上的脚本: http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/24/weekend-scripter-convert-word-documents-to-pdf-files-with-powershell.aspx基本PowerShell的 - 批量转换的Word DOCX到PDF
# Acquire a list of DOCX files in a folder
$Files=GET-CHILDITEM "C:\docx2pdf\*.DOCX"
$Word=NEW-OBJECT –COMOBJECT WORD.APPLICATION
Foreach ($File in $Files) {
# open a Word document, filename from the directory
$Doc=$Word.Documents.Open($File.fullname)
# Swap out DOCX with PDF in the Filename
$Name=($Doc.Fullname).replace("docx","pdf")
# Save this File as a PDF in Word 2010/2013
$Doc.saveas([ref] $Name, [ref] 17)
$Doc.close()
}
我不停的得到这个错误,不知道为什么:
PS C:\docx2pdf> .\docx2pdf.ps1
Exception calling "SaveAs" with "16" argument(s): "Command failed"
At C:\docx2pdf\docx2pdf.ps1:13 char:13
+ $Doc.saveas <<<< ([ref] $Name, [ref] 17)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
任何想法?
另外 - 我将如何改变它也转换文档(不DOCX)文件,以及使用本地文件(与脚本位置相同的位置的文件)?
对不起 - 从来没有做过PowerShell脚本...
这个工作 - 感谢 - 改变$ documents_path使用'$ documents_path =拆分路径-parent $ MyInvocation.MyCommand.Path' – takabanana 2013-05-14 22:16:27
是OLEDB使用基于脚本的位置是动态在这个转换中? – culter 2013-10-28 14:37:01
您还应该释放COM对象:'[System.Runtime.Interopservices.Marshal] :: ReleaseComObject($ word_app)'。 [The Scripting Guys](http://technet.microsoft.com/zh-cn/library/ff730962.aspx)还建议删除整个变量:'Remove-Variable word_app' – ComFreek 2014-11-02 18:37:50