[英]vba ms-word find text and get adjacent number
I am working with Word Docs containing quite a lot of pages and formulas. 我正在使用包含很多页面和公式的Word Docs。
I have an array containing expressions 我有一个包含表达式的数组
dim YellowWord(1 to 100) as string
I want to start at the beginning of the word text to look for every of those words and have a look the instances where that word or expression is followed by a number or numbers into brackets EXMAPLE: yellowword(2)="the blue table" 我想从单词文本的开头开始查找所有这些单词,并查看该单词或表达式后跟一个或多个括号的数字的实例。EXMAPLE:yellowword(2)=“ the blue table”
using wildcards I can find: the blue table (34, 23) in the text. 使用通配符,我可以找到:文本中的蓝色表格(34,23) 。 what I want is filling another array that would be: 我想要的是填充另一个数组,将是:
yellowwood_reference(2) = "(34, 23)"
the code I have is so: 我的代码是这样的:
for i=1 to NRofYellowWords
with active document.content.find
.clearformating
.text = yellowWord(i) & " " & "\((*)\)"
with .replacement
.clearformating
.text = yellowWord(i) & "(\1)"
'HERE IS WHERE I WANT TO SAY TO WORD:
'PUT THAT PART "(\1)" INTO A VARIABLE YELLOWWORD_REFERENCE(i)
'HOWW??????
.font.color = wdcolorred
'here i changed the color of the string with the references into red.
end with
.fordward = true
.wrap = wdfindcontinue
.format = true
.matchcase = false
.matchewholeword = false
.matchwildcards = true
.matchsoundslike = false
.matchallwordforms= false
.execute replace:=wdreplaceall
end with
next i
In the above code there are several problems: the first one I wrote it in capital letters, getting that reference of the wild card into a variable. 在上面的代码中,存在几个问题:第一个问题是我用大写字母编写的,将通配符的引用转换为变量。 The second one is that there might be many appearances of the YellowWord(2) in the text, I only need/want the first reference, not the rest. 第二个是在文本中可能出现了许多YellowWord(2),我只需要/想要第一个引用,而不需要其他引用。 That means that the first time the code finds the blue table (24,26) after passing the value "(24, 26)" into another array the code should move on and not look for more instances of the blue table in the text. 这意味着代码在将值“ (24,26)”传递到另一个数组中后第一次找到蓝表(24,26)时 ,代码应继续前进,而不要在文本中查找蓝表的更多实例。
btw, i used wildcards because there might be the case that the references are simple not into brackets, so i would have to run everything twice with a different wildcard. 顺便说一句,我使用通配符是因为在某些情况下,引用很简单而不放在方括号中,因此我必须使用不同的通配符将所有内容运行两次。
By the way as you can imagine, once I get the array yellowWord_reference(i) I would add the references there where there are instances of YellowWord without refferences. 用您可以想象的方式,一旦我得到了数组yellowWord_reference(i),我就会在存在没有引用的YellowWord实例的地方添加引用。
I would really appreciate help since I really clicked many websites with little success. 我非常感谢您的帮助,因为我确实点击了很多网站,但收效甚微。
thanks a lot 非常感谢
cheers 干杯
PS: If you think that there is a better way to do all that without using .find just mention it please, i am quite new in Ms-Word and coming from VBA Excel i get headaches figuring out where is the selection point. PS:如果您认为有一种更好的方法可以不使用.find而做所有这些事情,请只提及它,我在Ms-Word领域是一个新手,来自VBA Excel,我很头疼,想知道选择点在哪里。
I modified your code so that if it finds your 'words', it will capture the numbers that follow. 我修改了您的代码,以便如果找到您的“单词”,它将捕获后面的数字。
The code you posted would never work due to the number of compile errors ... strongly suggest you start using "Option Explicit" and posting actual code rather than typing in in yourself. 由于编译错误的数量,您发布的代码将永远无法工作...强烈建议您开始使用“ Option Explicit”并发布实际代码,而不要自己输入。
Other notes: 其他说明:
Here's the code: 这是代码:
Option Explicit
Sub Find_Words()
Dim yellowWord(100) As String
Dim yellowwood_reference(100) As String
Dim NRofYellowWords As Integer
Dim i As Integer
Dim lS As Long
Dim lE As Long
Dim sFound As String
Dim rng As Range
yellowWord(1) = "blue table"
yellowWord(2) = "little"
yellowWord(3) = "big"
yellowWord(4) = "xxx last xxx"
NRofYellowWords = 4
Set rng = ActiveDocument.Range
For i = 1 To NRofYellowWords
With rng.Find
.Text = yellowWord(i) & " " & "\((*)\)"
With .Replacement
.Text = yellowWord(i) & "(\1)"
End With
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
If .Found Then
' Find (numbers) & save
lS = InStr(rng.Start, ActiveDocument.Range.Text, "(")
If lS > 0 Then
lE = InStr(lS, ActiveDocument.Range.Text, ")")
sFound = Mid(ActiveDocument.Range.Text, lS, lE - lS + 1)
yellowwood_reference(i) = sFound
Debug.Print "Found: " & yellowWord(i) & vbTab & sFound
Else
MsgBox "Bad format; missing '('" & vbTab & Mid(ActiveDocument.Range.Text, lS, 50)
End If
Else
Debug.Print "Not Found: " & yellowWord(i)
End If
End With
Next i
Debug.Print "Finished"
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.