下面的代码工作正常,并向我返回两个搜索到的关键字之间的行/文本
Clear-Host
$SearchKeyword1 = 'keyword1'
$SearchKeyword2 = 'keyword1'
$F = Get-Content "D:\Contract.doc"| select-string -pattern $SearchKeyword1
Write-host $F.LineNumber
$G = Get-Content "D:\Contract.doc"| select-string -pattern $SearchKeyword2
Write-host $G.LineNumber
$Z = Get-Content "D:\Contract.doc"| select-string -pattern $SearchKeyword1 -Context 0, ($G.LineNumber - $F.LineNumber - 1)
Write-host $Z
如何使用-COMObject模型实现与上述相同的功能,仅适用于txt,但会导致docx单词文件出现问题
$word = New-Object -ComObject Word.Application
$word.Visible = $true
$doc = $word.Documents.Open($file, $ConfirmConversions, $ReadOnly)
$range = $doc.Content