Ваш вопрос приводит к еще нескольким ключевым вопросам:
- Вы анализируете XML-документ? (исходя из вашего примера)
- Вы анализируете XML-документ в Word? (вероятно, не очень хорошая идея)
Однако, учитывая вопрос о номинальной стоимости, я предполагаю, что у вас есть документ, который выглядит примерно так:
<patientFirstname>Bob</patientFirstname>
<patientFirstname>Sally</patientFirstname>
И вы хотите извлечь все имена пациентов.
Вы можете найти следующий код проще, чем тот, который вы опубликовали:
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