0
votes

I am working with Word Docs containing quite a lot of pages and formulas.

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"

using wildcards I can find: the blue table (34, 23) in the text. 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. 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.

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.

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.

1

1 Answers

0
votes

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.

Other notes:

  1. The numbers are enclosed in parenthesis () - not brackets []
  2. You were using a 'ReplaceAll'; if you only wanted the first occurance, change from '...All'
  3. I removed the 'red font' and 'Replace' ... add it back if needed.
  4. Your code would remove the space between the word and the number - is that what you wanted?

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