0

我不仅想复制字符串匹配(“Table 1234”),还想在同一行包含字符串的其余部分。(“表 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
4

2 回答 2

1

根据我的评论,Word 模型没有“Line”对象。但是...... selection.move 有一个 units:=WdUnits.Line 参数来逐行移动选择......(谷歌告诉我)

那么你可以这样做(免责声明:它很粗糙,准备好了,可能不健壮)

' 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()

下面是一个示例,其中我搜索了“嵌入”一词并选择了第 2 行: 在此处输入图像描述

于 2021-03-27T17:17:11.683 回答
0
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
于 2021-03-27T02:16:50.647 回答