Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
powershell word至pdf_Powershell_Pdf_Ms Word - Fatal编程技术网

powershell word至pdf

powershell word至pdf,powershell,pdf,ms-word,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

我修改了其他人在这个论坛上的内容,基于成功使用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()
这将遍历文件夹,然后查找.doc和.docx文件

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

您可以从中了解有关ExportAsFixedFormat的更多信息

仅供参考,当您使用
Word.Application
comobject时,您正在打开本机应用程序。True,但$Word.visible=$false意味着应用程序仅在后台运行,我相信…您是否收到错误?
$Word.open
$Word.Documents.open
,首先。而且您的
Get ChildItem
不会返回任何内容,因此您永远不会输入
foreach
循环。此外,$Formats等于nothing
ExportAsFixedFormat
:13个参数。不少。现在我明白了为什么我的ISE在这条线上疯狂了。。。始终阅读文档!%)参数编号5是
WdExportRange
,用于确定导出的页面数量;我发现将其设置为
3
,因为在回答中,我只得到了第一页,设置为
0
将导出整个文档。
$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()