Vb.net 如何在Word文档中选择全文

Vb.net 如何在Word文档中选择全文,vb.net,ms-word,copy,range,word-interop,Vb.net,Ms Word,Copy,Range,Word Interop,我不仅要复制字符串匹配(“表1234”),还要在同一行中包含字符串的其余部分。(“表1234-此处剩余文本”) 根据我的评论,单词模型没有“Line”对象。但是selection.move有一个units:=WdUnits.Line参数,用于按行移动选择。。。(谷歌告诉我) 那么你就可以这么做了(免责声明:它很粗糙,准备好了,可能不够健壮) 下面是我搜索单词“嵌入”并选择第2行的示例: 这是在vb.net还是vba?vb.net中,尽管我能够将vba转换为vb.net,它决定了“行的其余部分”的

我不仅要复制字符串匹配(“表1234”),还要在同一行中包含字符串的其余部分。(“表1234-此处剩余文本”)


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

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

下面是我搜索单词“嵌入”并选择第2行的示例:

这是在
vb.net
还是
vba
?vb.net中,尽管我能够将vba转换为vb.net,它决定了“行的其余部分”的结束位置-段落分隔符、手动换行符、表格单元格的结尾、自动换行符或段落分隔符。我相信这是通过使用/b来实现的,但我自己无法完全理解。请不要只发布代码作为答案,还要解释代码的作用以及它如何解决问题。带解释的答案通常更有帮助,质量也更好,更有可能吸引更多的人。Environment.newline equal vbCrLf是一个单词段落的结尾。这不是一行文字。据我所知,单词模型没有“文本行”
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
' 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()