如何 Select Word 文档中的整行文本
How to Select Full Line of Text in Word Document
我不仅要复制字符串匹配项 ("Table 1234"),还要在同一行中包含字符串的其余部分。 (“Table 1234 - 此处剩余文本”)。
Dim rng As Word.Range = oWord.ActiveDocument.Range
rng.Find.ClearFormatting()
rng.Find.Text = "Table [0-9]{3}"
rng.Find.MatchWildcards = True
Do While rng.Find.Execute(Forward:=True) = True
rng.Copy()
rng.Collapse(WdCollapseDirection.wdCollapseEnd)
Loop
public function ReturnLineOfTable() as string
dim txt as string = ...
for each Line in txt.split(environment.newline)
'If regex is found then
Return Line
' End if
next
end function
根据我的评论,Word 模型没有 'Line' 对象。但是... selection.move 有一个单位:=WdUnits.Line 参数来按行移动选择...(Google 告诉我)
那么你就可以这样做了(免责声明:它很粗糙,准备就绪,可能不够健壮)
' select the found text and go up one line and collapse the selection to the end
' that gives you the starting position of the next line of text
' that's the line that has the fount text
rng.Select
Selection.Move(Unit:=WdUnits.wdLine, Count:=-1)
Selection.Collapse(WdCollapseDirection.wdCollapseEnd)
Dim startPos As Integer = Selection.End
' Now do same for the end position of the line of text that has the found text
rng.Select()
Selection.Move(Unit:=WdUnits.wdLine, Count:=1)
Selection.Collapse(WdCollapseDirection.wdCollapseStart)
Dim endPos As Integer = Selection.Start
Selection.SetRange(startPos, endPos)
Selection.Select()
下面是我搜索单词 'embed' 并选择第 2 行的示例:
我不仅要复制字符串匹配项 ("Table 1234"),还要在同一行中包含字符串的其余部分。 (“Table 1234 - 此处剩余文本”)。
Dim rng As Word.Range = oWord.ActiveDocument.Range
rng.Find.ClearFormatting()
rng.Find.Text = "Table [0-9]{3}"
rng.Find.MatchWildcards = True
Do While rng.Find.Execute(Forward:=True) = True
rng.Copy()
rng.Collapse(WdCollapseDirection.wdCollapseEnd)
Loop
public function ReturnLineOfTable() as string
dim txt as string = ...
for each Line in txt.split(environment.newline)
'If regex is found then
Return Line
' End if
next
end function
根据我的评论,Word 模型没有 'Line' 对象。但是... selection.move 有一个单位:=WdUnits.Line 参数来按行移动选择...(Google 告诉我)
那么你就可以这样做了(免责声明:它很粗糙,准备就绪,可能不够健壮)
' select the found text and go up one line and collapse the selection to the end
' that gives you the starting position of the next line of text
' that's the line that has the fount text
rng.Select
Selection.Move(Unit:=WdUnits.wdLine, Count:=-1)
Selection.Collapse(WdCollapseDirection.wdCollapseEnd)
Dim startPos As Integer = Selection.End
' Now do same for the end position of the line of text that has the found text
rng.Select()
Selection.Move(Unit:=WdUnits.wdLine, Count:=1)
Selection.Collapse(WdCollapseDirection.wdCollapseStart)
Dim endPos As Integer = Selection.Start
Selection.SetRange(startPos, endPos)
Selection.Select()
下面是我搜索单词 'embed' 并选择第 2 行的示例: