I have Arabic texts in Microsoft Word that I need to introduce hpyerlinks to some of its words.
The reply to this question here works for me, but only for English words. When I replace the word "google" with any Arabic string (Whether one word or multiple words), the macro does not work.
I can display the Arabic characters correctly in VBA, using the answer to this question here, so there are no problems displaying the text in the macro.
Can you please help me understanding what code modifications I need to have, in order to get the macro to recognize the arabic text?
Needless to say, the Arabic language is a UTF-8 regulated language.
Here is an Arabic word to test on:
كلمة
and here is the macro I am using, based on the link I provided above:
Sub FindAndHyperlink()
'define the style
Dim strStyle As String
strStyle = "Subtle Emphasis"
'set the search range
Dim rngSearch As Range
Set rngSearch = ActiveDocument.Range
'set the search string
Dim strSearch As String
strSearch = "google"
'set the target address for the hyperlink
Dim strAddress As String
strAddress = "http:\\google.com"
With rngSearch.Find
Do While .Execute(findText:=strSearch, MatchWholeWord:=True, Forward:=True) = True
With rngSearch 'we will work with what is found as it will be the selection
ActiveDocument.Hyperlinks.Add Anchor:=rngSearch, Address:=strAddress
.Style = ActiveDocument.Styles(strStyle) 'throw the style on it after the link
End With
rngSearch.Collapse Direction:=wdCollapseEnd
'keep it moving
Loop
End With
End Sub
Thanks in advance.
Here is a screenshot of the outcome, after running the macro on a 3-word English phrase (worked), then changing it to one Arabic word (did not work).
EDIT 01
I modified my macro, based on @Cindy_Meister's answer below, but tried to group the characters in a list. The results are that the macro works, but on the decimal numbers, not the Arabic characters.
so, to add links to the phrase الأنبا غريغوريوس
:
Here is the updated macro:
Sub FindAndHyperlink2()
'
' FindAndHyperlink2 Macro
'
'
'define the style
Dim strStyle As String
strStyle = "Subtle Emphasis"
'set the search range
Dim rngSearch As Range
Set rngSearch = ActiveDocument.Range
'set the search string
Dim strSearch_list As String
strSearch_list = "01575&01604&01571&01606&01576&01575&00032&01594&01585&01610&01594&01608&01585&01610&01608&01587"
Dim strSearch As Variant
strSearch = Split(strSearch_list, "&")
For i = 0 To UBound(strSearch)
With rngSearch.Find
.Text = ChrW("&H" & Val(strSearch(i)))
'set the target address for the hyperlink
Dim strAddress As String
strAddress = "http:\\google.com"
Do While .Execute(findText:=strSearch_list, MatchWholeWord:=True, Forward:=True) = True
With rngSearch 'we will work with what is found as it will be the selection
ActiveDocument.Hyperlinks.Add Anchor:=rngSearch, Address:=strAddress
End With
rngSearch.Collapse Direction:=wdCollapseEnd
'keep it moving
Loop
End With
Selection.Find.Execute Replace:=wdReplaceAll
Next i
End Sub
but when I apply this macro on an MS Word file that has both the decimal block and the Arabic text, find that the decimal block is the one that was converted to a link, like the screenshots below:
Screenshot before applying the macro:
Screenshot after applying the macro:
mangoes.com
, instead of having 3 macros for this, do I edit the same question, or start a new question? thanks. – Atef WagihReplacement.Text
would be the same throughout... – Cindy Meister