我正在使用包含大量页面和公式的Word文档。
我有一个包含表达式的数组
dim YellowWord(1 to 100) as string
我想从单词text的开头开始查找每个单词,并查看该单词或表达式后跟一个或多个数字到括号中的实例 〔实施例: yellowword(2)=“蓝表”
使用通配符我可以找到:文本中的蓝色表(34,23)。 我想要的是填充另一个数组:
yellowwood_reference(2) = "(34, 23)"
我的代码就是这样:
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
在上面的代码中有几个问题: 我用大写字母写的第一个,将外卡的引用变为变量。 第二个是文本中可能有很多出现的YellowWord(2),我只需要/想要第一个引用,而不是其余的引用。这意味着代码第一次在将值“(24,26)”传递到另一个数组之后找到蓝色表(24,26)时,代码应继续而不是在文本中寻找更多蓝色表的实例。
不过,我使用通配符是因为可能存在引用很简单而不是括号的情况,因此我必须使用不同的通配符运行两次。顺便提一下,一旦我得到了数组yellowWord_reference(i),我会在那里添加那些有没有参考的YellowWord实例的引用。
我真的非常感谢帮助,因为我真的点击了许多网站但收效甚微。
非常感谢
欢呼声
PS:如果你认为有更好的方法可以不使用.find请提及它,我是Ms-Word的新手,来自VBA Excel我很头疼搞清楚选择点在哪里答案 0 :(得分:0)
我修改了您的代码,以便在找到您的“'”时,它会捕获后面的数字。
由于编译错误的数量,您发布的代码将无法正常工作...强烈建议您开始使用" Option Explicit"并发布实际代码而不是自己输入。
其他说明:
以下是代码:
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