vba Word VBA如何在两个子字符串之间选择文本并分配给变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6425595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Word VBA how to select text between two substrings and assign to variable?
提问by Saul
This doesn't quite work, I get the text selected Ok butneed to read it into a variable.
这不太有效,我将文本选择为 Ok 但需要将其读入变量。
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.
我试图从一个小的伪 xml 数据包中提取数据,因此搜索字符串在每个 word 文档中只会出现一次。
回答by ray
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)
- 您是否正在解析 XML 文档?(根据您的示例假设如此)
- 您是否在 Word 中解析 XML 文档?(可能不是一个好主意)
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
回答by Gaiane
The above code has an error - in the following line the last element should be Len(firstTerm) and not Len(secondTerm) like below:
上面的代码有一个错误 - 在下面的行中,最后一个元素应该是 Len(firstTerm) 而不是 Len(secondTerm),如下所示:
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
问候