使用PowerShell,我们使用以下命令成功提取了所需的数据:
Get-Content $SourceFileName |
Select-String -Pattern "search keywords"
要求:打开Word文档,搜索提供给功能的模式(如果找到该页面的话)(应该通过找到该搜索词的页面打开最终用户的Word文档)
这将打开Word文档,但不会显示在右行(我要转到LINE 534)
Set-Variable -Name wdGoToLine -Value 3 -Option Constant
Set-Variable -Name wdGoToAbsolute -Value 1 -Option Constant
$t = $true
$f = $false
$a = New-Object -ComObject Word.Application
$a.Visible = $true
$b = $a.Documents.Open("D:\Scan.doc", $f, $t)
$c = $a.Selection
$d = $c.GoTo($wdGoToLine, $wdGoToAbsolute, 534)
这将我带到右行,但没有为我打开Word文档:
Get-Content D:\Scan.doc -TotalCount 534 | Select-Object -Last 3;
LINE NUMBER by上述命令有所不同。 Get-Content
给出534的一种模式,而Word.Application
给出1090的相同内容。为什么会发生这种情况?
答案 0 :(得分:1)
这应该让您入门
# set these to your liking
$file = "D:\Scan.docx"
$textToFind = "find me"
$matchWildcards = $false
$matchCase = $true
# some Word constants
$wdFindStop = 0
$wdActiveEndPageNumber = 3
$wdStory = 6
$wdGoToPage = 1
$wdGoToAbsolute = 1
# Documents.Open switches
$ReadOnly = $false # when ReadONly was set to $true, it gave me an error on 'Selection.GoTo()'
# 'This method or property is not available because this command is not available for reading.'
$ConfirmConversions = $false
$word = New-Object -ComObject Word.Application
$word.Visible = $true
$doc = $word.Documents.Open($file, $ConfirmConversions, $ReadOnly)
$range = $doc.Content
$range.Find.ClearFormatting();
$range.Find.Forward = $true
$range.Find.Text = $textToFind
$range.Find.Wrap = $wdFindStop
$range.Find.MatchWildcards = $matchWildcards
$range.Find.MatchCase = $matchCase
$range.Find.Execute()
if ($range.Find.Found) {
# get the pagenumber
$page = $range.Information($wdActiveEndPageNumber)
Write-Host "Found '$textToFind' on page $page" -ForegroundColor Green
[void] $word.Selection.GoTo($wdGoToPage, $wdGoToAbsolute, $page)
}
else {
Write-Host "'$textToFind' not found" -ForegroundColor Red
[void] $word.Selection.GoTo($wdGoToPage, $wdGoToAbsolute, 1)
}
# cleanup com objects
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($range) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($doc) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()