Function 源档中查找关键字()
Dim StartRange As Long, EndRange As Long, InsertRange As Range
MsgBox source_Path & source_doc & Keyword
Set doc = Documents.Open(FileName:=source_Path & source_doc) ', Visible:=False) '以隐藏方式打开指定文档
Set FindRange = Documents(source_Path & source_doc).Content '搜索范围(整个源档)
TarLines = doc.BuiltInDocumentProperties("Number of lines").Value 'DOC文档的总行数
With FindRange.Find
.ClearFormatting
.Text = Keyword
.Execute
MsgBox Keyword & " " & KeyLineSum & " " & TarLines
If .Found = False Then
MsgBox Keyword & "没找到" '将关键字标志设为1(未完成)
myxl.Sheets(DocName).Range("Dm").Value = 0
MsgBox "KeySign: " & KeySign
Else
MsgBox Keyword & "已经找到"
FindPage = FindRange.Information(wdActiveEndPageNumber)
Findlines = FindRange.Information(wdFirstCharacterLineNumber) '关键行的行号
MsgBox Findlines
StartRange = doc.GoTo(wdGoToLine, , Findlines).Start '关键字位置起始点
MsgBox KeyLineSum
'如果输入行号与DOC的总行数一致,则终点位置为文档末位置,
'反之则为下一行的起点
EndRange = VBA.IIf(i = TarLines, doc.Content.End, doc.GoTo(wdGoToLine, , Findlines + KeyLineSum).Start)
Set InsertRange = doc.Range(StartRange, EndRange) '定义一个RANGE对象 '
MsgBox InsertRange.Text
k.Offset(, 3).Value = "成功" '将文字写入工作表中的成功标记单元格
k.Offset(, 5).Value = InsertRange.Text '将找到的内容写入工作表中的关键字内容单元格
ActiveWorkbook.Save
ActiveDocument.Save '保存目标档
End If
End With
End Function
此处得到的Findlines只是所在页内的行数,而非整个文档的行数,这样的话不管找到的内容应该是在第1页之后的页内,但InsertRange.Text返回的总是第1页的内容.怎么办? |