使用 Powershell 返回 Word 搜索结果



我想使用PowerShell从Word文档中提取格式信息。使用 Word,您可以搜索格式化的文本片段。这样,Word 会突出显示满足条件的部分(例如,带绿色下划线的文本)。有了这个,我也可以在PowerShell中找到斜体文本:

$objWord = New-Object -Com Word.Application
$myWordFile = 'C:MyWordFile.docx'
$objDocument = $objWord.Documents.Open($myWordFile)
$objDocument.Paragraphs[0].Range.Find.Font.Italic = $true
$objDocument.Paragraphs[0].Range.Find.Execute()

但是,我对斜体文本本身很好奇,这与-match $matches的内容类似。

这是您要执行的操作的示例...

这是查找和替换,因此,如果这不是您的最终目标,请忽略替换部分 - 它也是查找单词然后应用斜体,但相同的方法可用于查找所有斜体单词。

$application = New-Object -comobject word.application 
$application.visible = $true 
$document = $application.documents.open("C:fsoTest.docx") 
$selection = $application.Selection 
$words = "exchange","sql" 
$matchCase = $false 
$matchWholeWord = $true 
$matchWildCards = $false 
$matchSoundsLike = $false 
$matchAllWordForms = $false 
$forward = $true 
$wrap = 1 
$format = $true 
$replace = 2 
Foreach ($word in $words) 
{ 
    $findText = $word 
    $replaceWith = $word 
    $selection.find.replacement.font.italic = $true 
    $exeRTN = $selection.find.execute($findText,$matchCase, 
    $matchWholeWord,$matchWIldCards,$matchSoundsLike, 
    $matchAllWordForms,$forward,$wrap,$format,$replaceWith, 
    $replace) 
}

。如下所述:

嘿,脚本专家!如何在Microsoft Word 文档中将特定单词设置为斜体?