powershell一词到pdf

时间:2017-09-18 18:52:42

标签: powershell pdf ms-word

我修改了其他人在此论坛上的内容,基于成功使用powershell Excel到pdf但没有成功。关于我在以下代码中失败的任何想法?我的目标是能够在不打开本机应用程序的情况下将doc和docx文档的整个文件夹转换为pdf。提前致谢:

# Acquire a list of DOC files in a folder
$Word = New-Object -ComObject word.application 
$path = "c:\Users\Desktop\Test" 
$formats = "Microsoft.Office.Interop.Word.WdFixedFormatType" -as [type]
$Word.visible = $false
$fileTypes = "*.docx","*doc"
$Files = GET-CHILDITEM $path -include $fileTypes

Foreach ($File in $Files) {
    $filepath = Join-Path -path $path -childpath ($File.basename +".pdf")
    $Doc = $Word.open($File.fullname, 3)
    $Doc.Saved= $true
    "Converting $File to pdf ... $destPath"
    # Save this File as a PDF in Word 2010/2013
    $Doc.ExportAsFixedFormat($formats::wdTypePDF, $path)
    $Doc.FullName -replace '\.doc?$', '.pdf'
    $Doc.Close()
}
$Word.Quit()

1 个答案:

答案 0 :(得分:1)

$path = 'C:\tests'

$wd = New-Object -ComObject Word.Application
Get-ChildItem -Path $path -Include *.doc, *.docx -Recurse |
    ForEach-Object {
        $doc = $wd.Documents.Open($_.Fullname)
        $pdf = $_.FullName -replace $_.Extension, '.pdf'
        $doc.ExportAsFixedFormat($pdf,17,$false,0,3,1,1,0,$false, $false,0,$false, $true)
        $doc.Close()
    }
$wd.Quit()

这将通过该文件夹,然后查找.doc和.docx文件

然后打开每个文件创建一个新的保存名称并导出到PDF

您可以从中了解有关ExportAsFixedFormat的更多信息 https://msdn.microsoft.com/en-us/library/bb256835(v=office.12).aspx