1
votes

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).

Macro results

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:

Before applying the macro

Screenshot after applying the macro:

After applying the macro

What I want to achieve: What I want to achieve

1
I suspect you will need to explore language options in the .match methods of the .find method.freeflow
I'm condensing comments, so moving this from below the answer to here... Just FYI :-) The additional macro code posted as an edit can't possibly give the described result: .Text = ChrW("&H" & Val(strSearch(i))) makes no sense in this context. But the answer has been edited to include the additional request.Cindy Meister
@CindyMeister, ok thanks a lot. Now if I need an array of words to be replaced all with the same link (e.g.:"mango" or "mangoes" or "box of mangoes" to be linked to mangoes.com, instead of having 3 macros for this, do I edit the same question, or start a new question? thanks.Atef Wagih
I prefer you start a new question - thank you for asking :-) It will help tremendously to see the code you have, to use as a starting point. But FWIW, in "shorthand": Loop the array, performing a Find in each loop. The Replacement.Text would be the same throughout...Cindy Meister
@CindyMeister, thanks :). I created another question here. I created an array, but the new function works on the last term only. I'd appreciate if you have time to look at the other question. thanks.Atef Wagih

1 Answers

1
votes

You don't provide the code you used to search an Arabic term but based on the word posted in the question the issue appears to be related to Arabic being RTL in a LTR document. I wasn't able to test with Arabic characters in the macro code - even following the instructions in the link you provide my VBA editor only displays ? - possibly because I don't have the Arabic IME installed...

In any case, I was able to get it to work using the decimal number representation of the Unicode characters. The trick is to put them in reverse order (RTL) so:

strSearch = ChrW(1603) & ChrW(1604) & ChrW(1605) & ChrW(1577) 
'instead of ChrW(1577) & ChrW(1605) & ChrW(1604) & ChrW(1603)

The request has been changed to allowing the decimal number to be provided as a character-delimited string value. This can be split into an array, which is looped to put each value into the ChrW function and these concatenated into the search string:

Dim strSearch As String, Word1 As String
Dim valWord1 As Variant
Dim i As Long
Word1 = "1603,1604,1605,1577"
valWord1 = Split(Word1, ",")
For i = LBound(valWord1) To UBound(valWord1)
    strSearch = strSearch & ChrW(valWord1(i))
Next