【问题标题】:Returning Word search results using Powershell使用 Powershell 返回 Word 搜索结果
【发布时间】:2019-01-30 05:49:09
【问题描述】:

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

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

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

【问题讨论】:

  • 这是什么意思...(但是,我对斜体文本本身很好奇,类似于 -match 的 $matches 的内容。)?您在 Word DOM 中工作,而不是 PowerShell,您使用 PowerShell 与 Word 互操作这一事实并没有真正改变您使用 DOM 的事实。因此,您必须使用 Word DOM 比较,例如 $objWord.Selection,因为您正在尝试搜索整个文件。
  • @postanote 假设我有 this in 斜体。我想将“this in”作为返回字符串。
  • 看我的回答。

标签: powershell ms-word


【解决方案1】:

这是您尝试做的一个示例……

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

$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) 
}

... 此处记录:

Hey, Scripting Guy! How Can I Italicize Specific Words in a Microsoft Word Document?

【讨论】:

  • 对我来说并不完全清楚,我如何在我的示例中拥有一个变量,它的字符串值为“this in”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多