我正在尝试使用此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文档,而我的函数调用似乎是正确的。