Saul
Saul

Reputation: 1397

Word VBA how to select text between two substrings and assign to variable?

This doesn't quite work, I get the text selected Ok butneed to read it into a variable.

Sub findTest()

Dim firstTerm As String
Dim secondTerm As String
Dim myRange As Range
Dim selRange As Range
Dim selectedText As String

Set myRange = ActiveDocument.Range
firstTerm = "<patientFirstname>"
secondTerm = "</patientFirstname>"
With myRange.Find
.Text = firstTerm
.MatchWholeWord = True
.Execute
myRange.Collapse direction:=wdCollapseEnd
Set selRange = ActiveDocument.Range
selRange.Start = myRange.End
.Text = secondTerm
.MatchWholeWord = True
.Execute
myRange.Collapse direction:=wdCollapseStart
selRange.End = myRange.Start
selectedText = selRange.Select
End With
End Sub

I'm trying to extract data from a small pseudo xml packet, so the search string will only occur once in each word document.

Upvotes: 4

Views: 26210

Answers (2)

Gaiane
Gaiane

Reputation: 11

The above code has an error - in the following line the last element should be Len(firstTerm) and not Len(secondTerm) like below:

Debug.Print Mid$(documentText, startPos + Len(firstTerm), stopPos - startPos - Len(firstTerm))

I used the above code and it works to me - I assigned the string to my variable like:

myString = Mid$(documentText, startPos + Len(firstTerm), stopPos - startPos - Len(firstTerm))

Regards

Upvotes: 1

ray
ray

Reputation: 8699

Your question leads to a couple of more key questions:

  • Are you parsing through an XML document? (assuming so based on your example)
  • Are you parsing through the XML document in Word? (probably not a good idea)

However, taking the question on face value, I’m going to assume you have a document that looks similar to this:

<patientFirstname>Bob</patientFirstname>
<patientFirstname>Sally</patientFirstname>

And you want to extract all the patient first names.

You may find the following code simpler than what you posted:

Sub findTest()

    Dim firstTerm As String
    Dim secondTerm As String
    Dim myRange As Range
    Dim documentText As String

    Dim startPos As Long 'Stores the starting position of firstTerm
    Dim stopPos As Long 'Stores the starting position of secondTerm based on first term's location
    Dim nextPosition As Long 'The next position to search for the firstTerm

    nextPosition = 1

    'First and Second terms as defined by your example.  Obviously, this will have to be more dynamic
    'if you want to parse more than justpatientFirstname.
    firstTerm = "<patientFirstname>"
    secondTerm = "</patientFirstname>"

    'Get all the document text and store it in a variable.
    Set myRange = ActiveDocument.Range
    'Maximum limit of a string is 2 billion characters.
    'So, hopefully your document is not bigger than that.  However, expect declining performance based on how big doucment is
    documentText = myRange.Text

    'Loop documentText till you can't find any more matching "terms"
    Do Until nextPosition = 0
        startPos = InStr(nextPosition, documentText, firstTerm, vbTextCompare)
        stopPos = InStr(startPos, documentText, secondTerm, vbTextCompare)
        Debug.Print Mid$(documentText, startPos + Len(firstTerm), stopPos - startPos - Len(secondTerm))
        nextPosition = InStr(stopPos, documentText, firstTerm, vbTextCompare)
    Loop

    MsgBox "I'm done"

End Sub

Upvotes: 6

Related Questions