以编程方式删除Word文档中的所有隐藏文本

时间:2016-11-17 13:30:34

标签: powershell ms-word com

使用PowerShell,我需要编写一个脚本,删除Word文档的所有隐藏文本。

这是我到目前为止所做的:

$WordDocument = Get-Item "C:\MyWordDocument.docx"

$word_app = New-Object -ComObject Word.Application
$word_app.Visible = $false

$document = $word_app.Documents.Open($WordDocument.FullName)

$objSelection = $word_app.Selection 
$objSelection.Font.Hidden = $True

$FindText = "" # search on formatting only (according to MS doc)
$wdFindContinue = 1
$ReplaceAll = 2
$MatchCase = $False 
$MatchWholeWord = $False 
$MatchWildcards = $False 
$MatchSoundsLike = $False 
$MatchAllWordForms = $False 
$Forward = $True 
$Wrap = $wdFindContinue 
$Format = $True # ?
$ReplaceWith = "" 

$a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, ` 
    $MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,` 
    $Wrap,$Format,$ReplaceWith,$ReplaceAll) 
$document.Save()
$document.Close()

$word_app.Quit()

它不起作用,我无法弄清楚原因。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

错误在于您将搜索过滤器设置为查找隐藏文本。而不是$objSelection.Font.Hidden = $True(这实际上隐藏了当前选定的文本),您需要在$objSelection.Find对象上设置属性:

$objSelection.Find.Font.Hidden = $True