在不同场景的两行中选择特定文本

Selection of particular text in two lines with different scenarios

我曾遇到过需要 select 两行特定文本的情况。我一直在通过以下代码执行此操作:

Selection.Paragraphs(1).Range.Select
Selection.MoveRight Unit:=wdWord, Count:=2, Extend:=wdExtend

但以上代码并不适用于以下四种情况。我正在寻找可以输出第一行和第二行 selection 直到 'comma' 的代码。我需要尽可能简单的代码,请帮助。

场景一

Infraestructura Energetica Nova SAB De CV

IENOVA* MM,购买

场景二

Infraestructura Energetica Nova SAB De CV

IENOVA13 MM,卖出

场景三

Infraestructura Energetica Nova SAB De CV

IENOVA* MM

场景 4 已编辑

新星 SAB

IENOVA MM

插图配图:

从字面上理解你的问题:

...I'm in search of code which would output selection of first line and second line till 'comma'.

您可以对代码的第 2 行进行如下调整;

Selection.Paragraphs(1).Range.Select
Selection.MoveEndUntil ",", wdForward

这样做是将选择的末尾向前移动,直到找到“,”。

但是,如果根据您的 'Scenarios',某些选择可能不包含逗号,则以下内容将起作用:

子选择测试()

Dim mySel As String

With Selection
    .Paragraphs(1).Range.Select
        mySel = Selection
            If InStr(1, mySel, ",") Then
                .MoveEndUntil ",", wdForward
            Else
                .Extend "M"
                .Extend "M"
            End If

End With

结束子

它所做的是选择段落,将字符串设置为变量 mySel 并使用 InStr 函数测试字符串是否包含逗号,如果包含,则执行与上面相同的代码,但如果没有逗号,它将选择范围扩展到字符 "M"(大写 M),然后再次将选择范围扩展到下一个 "M".

如您的评论所述,您文本的 "MM" 部分是一个变量,因此:

Sub SelectionTest()

    Dim mySel As String

        With Selection
             .Paragraphs(1).Range.Select
             .MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
              mySel = Selection
                 If InStr(1, mySel, ",") Then
                     .Paragraphs(1).Range.Select
                     .MoveEndUntil ","
                 Else: Exit Sub
                 End If
        End With
End Sub

这样做是选择第一段,然后将选择范围扩展到第二行的末尾,将所选文本设置为变量 mySel 并使用 InStr 函数测试字符串是否包含一个逗号,如果有,它会执行与上面相同的代码,但如果没有逗号,它会保持选中 2 行,仅此而已。

这使代码更短,而不是为每个国家("MM"、"RO"、"TI" 等)提供 ElseIf 语句,但确实不依赖于国家代码。否则按照答案的前一部分重复 ElseIf 为每个国家变量。

我在你的所有场景中测试了这个(通过 copy/pasting 你的场景段落到 word 中)并且每个结果都和你的 'target selection' 一样,只要光标在开头当代码为 运行.

时需要段落

或者您可以省略指定逗号的部分并只使用(可能根据需要进行调整并将其放在 if 语句中以允许您使用变量):

With Selection
    .Paragraphs(1).Range.Select
    .Extend "M"
    .Extend "M"
End With

这些代码将根据您在问题中提出的问题和提供的内容起作用,但可能不是当前形式中最通用的代码。

有关以下链接中使用的函数和方法的更多信息:

以下将两个段落作为单独的范围。第一段原封不动地选取,用作获取第二段的起点。

使用Instr函数判断是否存在逗号-Instrreturns如果有none则为0,否则为正数。

如果没有逗号,段落标记被截断。不清楚你是否想要这个Chr(13),如果你想要,就把那行注释掉,然后该段就被选中了,没有任何变化。

如果有逗号,Range 会折叠到它的起点,然后扩展到逗号的位置,减 1(省略逗号)。

然后将两个字符串连接起来 debug.print。然后将第一个 Range 的端点扩展到第二个 Range 的终点,这样您就有了一个 Range(如果这是您需要的 - 不清楚)。

Sub SelectInfo()
    Dim rngLine1 As Word.Range
    Dim rngLine2 As Word.Range
    Dim isComma As Long

    Set rngLine1 = Selection.Range.Paragraphs(1).Range
    Set rngLine2 = rngLine1.Duplicate
    rngLine2.Collapse (wdCollapseEnd)
    Set rngLine2 = rngLine2.Paragraphs(1).Range

    isComma = InStr(rngLine2.Text, ",")
    If isComma = 0 Then
        'No comma, assume we don't want the last paragraph mark...
        rngLine2.MoveEnd wdCharacter, -1
    Else
        rngLine2.Collapse wdCollapseStart
        rngLine2.MoveEnd wdCharacter, isComma - 1
    End If

    Debug.Print rngLine1.Text & rngLine2.Text
    'Get a single Range instead of the string:
    rngLine1.End = rngLine2.End
End Sub