Building upon the response I receive to my previous question here, I want to create a macro to add one link to multiple Arabic words.
Example: if I have a text that has: horse
or horses
or pony
, I need to link it to horses.com
.
I was able to modify the original macro to add links to all of the three words successfully, but I believe that my code is bloated with repetition.
My question is: is there a way to condence the code with better expressions?
Here is my working code so far:
Sub FindAndHyperlink3()
'set the search range
Dim rngSearch1 As Range, rngSearch2 As Range, rngSearch3 As Range
Set rngSearch1 = ActiveDocument.Range
Set rngSearch2 = ActiveDocument.Range
Set rngSearch3 = ActiveDocument.Range
'set the search string 3 words
'set the target address for the hyperlink
Dim strAddress As String
strAddress = "http:\\google.com"
Dim strSearch1 As String, strSearch2 As String, strSearch3 As String, Word1 As String, Word2 As String, Word3 As String
Dim valWord1 As Variant
Dim valWord2 As Variant
Dim valWord3 As Variant
Dim i As Long, j As Long, k As Long
Word1 = "01575,01604,01571,01606,01576,01575,00032,01594,01585,01610,01594,01608,01585,01610,01608,01587"
Word2 = "01603,01610,01585,01604,01587,00032,01575,01604,01585,01575,01576,01593"
Word3 = "01575,01604,01575,01603,01604,01610,01585,01603,01610,01577"
valWord1 = Split(Word1, ",")
valWord2 = Split(Word2, ",")
valWord3 = Split(Word3, ",")
For i = LBound(valWord1) To UBound(valWord1)
strSearch1 = strSearch1 & ChrW(valWord1(i))
Next
With rngSearch1.Find
Do While .Execute(findText:=strSearch1, MatchWholeWord:=True)
With rngSearch 'we will work with what is found as it will be the selection
ActiveDocument.Hyperlinks.Add Anchor:=rngSearch1, Address:=strAddress, Target:=blank
End With
rngSearch1.Collapse Direction:=wdCollapseEnd
'keep it moving
Loop
End With
For j = LBound(valWord2) To UBound(valWord2)
strSearch2 = strSearch2 & ChrW(valWord2(j))
Next
With rngSearch2.Find
Do While .Execute(findText:=strSearch2, MatchWholeWord:=True)
With rngSearch2 'we will work with what is found as it will be the selection
ActiveDocument.Hyperlinks.Add Anchor:=rngSearch2, Address:=strAddress, Target:=blank
End With
rngSearch2.Collapse Direction:=wdCollapseEnd
'keep it moving
Loop
End With
For k = LBound(valWord3) To UBound(valWord3)
strSearch3 = strSearch3 & ChrW(valWord3(k))
Next
With rngSearch3.Find
Do While .Execute(findText:=strSearch3, MatchWholeWord:=True)
With rngSearch3 'we will work with what is found as it will be the selection
ActiveDocument.Hyperlinks.Add Anchor:=rngSearch3, Address:=strAddress, Target:=blank
End With
rngSearch3.Collapse Direction:=wdCollapseEnd
'keep it moving
Loop
End With
End Sub
Thanks a lot.