简体   繁体   中英

Concatenate signs, find text in Word doc and Range.MoveEndUntil Cset:= paragraph sign & "2" & ". "

I have this macro that is run from Excel VBA editor and it searches strings in Word document, copies that string and pastes it into a cell in .Activesheet.Range("E27")

So I have this code and I'm already so close to making it work, but I need to concatenate four characters to make it work, paragraph sign (¶), & "2" & ". "

Sub AdjusFindFirstName()
    Sub AdjFindFirstName()
    'Variables declaration
    Dim WordApp As Word.Application
    Dim WordDoc As Word.Document
    Dim ExcelApp As Excel.Application
    Dim Rng As Word.Range

    Dim ws As Worksheet
    Dim TextToFind As String
    Dim ApartmentPrice As String
    Dim StartPos As Long
    Dim EndPos As Long
    Dim FullName As String
    Application.ScreenUpdating = False

    'Assigning object variables
    Set WordApp = GetObject(, "Word.Application")
    Set WordDoc = WordApp.ActiveDocument
    Set Rng = WordApp.ActiveDocument.Content
    'Searching
    With Rng.Find
    '.Text = TextToFind
    .Text = "REGON 364061169, NIP 951-24-09-783,"
    .Execute    '?Rng = REGON 364061169, NIP 951-24-09-783,
        If .Found = True Then
            Rng.MoveEndUntil Cset:="2" & ". "  'expands Rng variable until first "2. "                  
            Rng.MoveEnd wdWord, 3          'expands Rng variable wdWord, 3 further
                                           'wdParagraph, wdLine, wdSentence, wdScreen
            If Rng.Words.Last.Next.Text = "-" Then    'I want the text of the word following the last word in my range
               Rng.MoveEnd wdWord, 2    'expands Rng variable wdWord, 2 further
            End If
            StartPos = InStr(1, Rng.Text, "2. ")
            FullName = Mid(Rng.Text, StartPos + 3)
        End If
    End With
    Debug.Print FullName
End Sub

In fact I cannot even execute Range.MoveEndUntil Cset:= "2. "

I even tried Range.MoveEndUntil Cset:= "2" & ". "

These lines are not expanding my Rng variable by any word or character.

But what I really need is finding/MovingEndUntill Cset:= "¶" & "2" & ". "

Here is the printscreen, where I've selected the string I need to .MoveEndUntil

在此处输入图片说明

Maybe you could tell me how to expand my Rng variable until the end of the current paragraph?

Careful reading of the Help topic for MoveEndUntil reveals that it looks only at individual characters, not character combinations:

Moves the end position of the specified range until any of the specified characters are found in the document.

There is no "Move" method that works in that manner. Word's Find feature, however, can locate character combinations. Adding an additional Find that runs from the first "found" term locates the information specified in the question. For example

Dim WordDoc As Word.Document
Dim rngStart As Word.Range, rngEnd As Word.Range

Set WordDoc = WordApp.ActiveDocument
Set rngStart = WordDoc.Content
'Searching
With rngStart.Find
'.Text = TextToFind
    .Text = "REGON 364061169, NIP 951-24-09-783,"
    .Execute    '?Rng = REGON 364061169, NIP 951-24-09-783,
    If .found = True Then
        'Instantiate a Range for the next search, starting with the first "found"
        Set rngEnd = rngStart.Duplicate
        'Extend that range to the end of the document
        rngEnd.End = WordDoc.Content.End
        With rngEnd.Find
            .Text = vbCr & "2. "
            .Forward = True
            .Execute
            If .found Then
                rngEnd.MoveEnd wdWord, 3
                If rngEnd.Words.Last.Next.Text = "-" Then    'I want the text of the word following the last word in my range
                    rng.MoveEnd wdWord, 2    'expands Rng variable wdWord, 2 further
                End If
            End If
        End With

        startPos = InStr(1, rngEnd.Text, "2. ")
        FullName = Mid(rngEnd.Text, startPos + 3)
    End If
End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM