PDA

View Full Version : [SOLVED:] Many Wild Card Searches - For different Texts - Array



dj44
02-27-2017, 07:26 AM
Hi folks,
Good Monday.:)

I have been pondering this dilemma over the weekend and need some advice.

Lets say I have multiple different wild card searches I need to run.

Can I do that from one script?

I hate to have to have 10 different wild card scripts.

So for example
Each wild card search targets a different text and applies some color to it







Sub MultipleWildCardsSearch()


Dim oRng As Word.Range
Dim lngIndex As Long

Dim oArraySearch() As String
Dim varLongColors

'Wild Cards Search -Can I store in an array or Select Case Statement?

oArraySearch = Split("ZX[0-9]", "\#b[0-9]", ",")


varLongColors = Array(RGB(0, 156, 250), RGB(256, 0, 0))


For lngIndex = 0 To UBound(oArraySearch)
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = oArraySearch(lngIndex)
'.Format = True
.MatchWholeWord = True

'Find the wild card text

.Replacement.Text = oArraySearch(lngIndex)
.Replacement.Font.Color = varLongColors(lngIndex)


.Execute Replace:=wdReplaceAll
End With
Next
lbl_Exit:
Exit Sub
End Sub




What would be the best way for me to set up something like this. I wasn't sure if this was a loop within a loop or something

Many thanks for your time

macropod
02-27-2017, 07:36 PM
See, for example:
http://www.msofficeforums.com/word-vba/12803-find-replace.html#post34254
or, for something much more elaborate:
http://www.msofficeforums.com/word-vba/29384-vba-word-find-formatted-text-version-only.html#post93796

dj44
02-27-2017, 08:09 PM
Hello Paul,

thank you for these threads they do are very impressive with an excel spreadsheet - i could store loads of these things in there,

It will take me time to slowly set these up and get togthere the various search fields i wanted to target.

Now i usually have 2 left feet when it comes to vba coding and can make a right mess.

Just as a baby step as well , so that i dont go off track with this idea of the search within an array as its quite hard to think like that for non techy folk

in the interim as well, i am curious to know about this loop array.

I have an array, but i never thought of before how to make it store a search criteria as

Search Criteria 1
Search Criteria 2
Search Crietria 3

and then run it from 1 script

It will help me to understand in the future to try and add it to other basic scripts so i can cut down on 10 searches for a paragraph or something else.


oArraySearch = Split("ZX[0-9]", "\#b[0-9]", ",")

.Text = oArraySearch(lngIndex) << Is this meant to be a Search within a search ?

:)

macropod
02-27-2017, 09:28 PM
oArraySearch(lngIndex) is simply the reference to the relevant element in the array. So, if lngIndex = 0, oArraySearch(lngIndex) = oArraySearch(0) and returns ("ZX[0-9]"

As for:
Dim oArraySearch() As String
...
oArraySearch = Split("ZX[0-9]", "\#b[0-9]", ",")
you might do better to use:
Dim oArraySearch
...
oArraySearch = Array("ZX[0-9]", "\#b[0-9]", ",")

dj44
02-28-2017, 04:43 PM
Hello Paul,

on this basic script I am getting a type mismatch
on this line

.Replacement.Font.Color = varLongColors(lngIndex)




Sub MultipleWildCardsSearch()


Dim oRng As Word.Range
Dim lngIndex As Long
Dim oArraySearch
Dim varLongColors

oArraySearch = Array("ZX[0-9]", "*Q[0-9]", ",")

varLongColors = Array(RGB(0, 156, 250), RGB(256, 0, 0))


For lngIndex = 0 To UBound(oArraySearch)
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting

.Text = oArraySearch(lngIndex)
'.Format = True
.MatchWholeWord = True
.Replacement.Font.Color = varLongColors(lngIndex)

.Replacement.Text = oArraySearch(lngIndex)
.Execute Replace:=wdReplaceAll
End With
Next
lbl_Exit:
Exit Sub
End Sub


well i looked at it for a long time , and i know I did something but i cant work it out now

macropod
02-28-2017, 11:56 PM
on this basic script I am getting a type mismatch
on this line

.Replacement.Font.Color = varLongColors(lngIndex)
Doubtless that's because oArraySearch has more elements (3) than varLongColors (2). Consequently, you'll get a 'subscript out of range' error because of that.

dj44
03-01-2017, 06:11 AM
ooops :doh:

I thought that comma was part of the split array function.

oArraySearch = Array("ZX[0-9]", "*Q[0-9]", ",")

So i must have tinkered with the wrong part


well thats sorted now

thanks for the pointers and the awesome threads Paul
im still setting them up :yes