通过Powershell在单词中插入标题

时间:2018-06-21 10:33:54

标签: powershell ms-word

我正在尝试使用此Microsoft Blog

中的Powershell脚本
$EmptyDoc = Get-ChildItem 'C:\\Users\\Admin\\Desktop\\test_folder\\empty.docx' $Path -Recurse -Include *.docx
$wdApplication = New-Object -ComObject "Word.Application"
$wdApplication.visible = $true
$wdDocumentPath = $EmptyDoc.FullName

write $wdDocumentOpenPath

$wdDocument = $wdApplication.Documents.Open($wdDocumentPath)

$section = $wdDocument.sections.item(1)
$header = $section.headers.item([microsoft.office.interop.word.WdHeaderFooterIndex]::wdHeaderFooterFirstPage)

$header.range.InsertAlignmentTab([microsoft.office.interop.word.WdAlignmentTabAlignment]::wdLeft)
$header.range.InsertAfter("Test Header")

$wdDocument.Save()
$wdDocument.Close()
$wdApplication.quit()
exit

我正在一个空的.docx文件[wdFormatXMLDocument]上运行它。但是,运行脚本后,.docx仍然是空文件。我尝试过Microsoft.Office.Interop.Word MSDN文档,而我的函数调用似乎是正确的。

0 个答案:

没有答案