VBA Macro to search a text string and replace with hyperlink works only with English text, but not Arabic

ぃ、小莉子 提交于 2019-12-31 06:43:26

问题


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:

What I want to achieve:


回答1:


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


来源:https://stackoverflow.com/questions/53509921/vba-macro-to-search-a-text-string-and-replace-with-hyperlink-works-only-with-eng

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!