用于搜索文本字符串并替换为超链接的 VBA 宏仅适用于英文文本,不适用于阿拉伯语 [英] VBA Macro to search a text string and replace with hyperlink works only with English text, but not Arabic

查看:19
本文介绍了用于搜索文本字符串并替换为超链接的 VBA 宏仅适用于英文文本,不适用于阿拉伯语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Microsoft Word 中有阿拉伯语文本,我需要将 hpyerlinks 引入其中的某些词.

编辑 01

我根据下面@Cindy_Meister 的回答修改了我的宏,但尝试将字符分组到列表中.结果是宏可以工作,但适用于十进制数字,而不是阿拉伯字符.

因此,要添加到短语 الأنبا غريغوريوس 的链接:

这是更新的宏:

Sub FindAndHyperlink2()'' FindAndHyperlink2 宏'''定义风格将 strStyle 调暗为字符串strStyle = "微妙的强调"'设置搜索范围将搜索变暗为范围设置 rngSearch = ActiveDocument.Range'设置搜索字符串Dim strSearch_list 作为字符串strSearch_list = "01575&01604&01571&01606&01576&01575&00032&01594&01585&01610&01590&01590&01590&01571&8&05&05&05&05&05&0108&0108&016Dim strSearch 作为变体strSearch = Split(strSearch_list, "&")对于 i = 0 到 UBound(strSearch)使用 rngSearch.Find.Text = ChrW("&H" & Val(strSearch(i)))'设置超链接的目标地址Dim strAddress As StringstrAddress = "http:\google.com"Do While .Execute(findText:=strSearch_list, MatchWholeWord:=True, Forward:=True) = True使用 rngSearch '我们将处理找到的内容,因为它将是选择ActiveDocument.Hyperlinks.Add Anchor:=rngSearch, Address:=strAddress结束于rngSearch.Collapse 方向:=wdCollapseEnd'保持移动环形结束于Selection.Find.Execute Replace:=wdReplaceAll接下来我结束子

但是当我将这个宏应用到一个同时包含十进制块和阿拉伯文本的 MS Word 文件时,发现十进制块是转换为链接的那个,如下面的屏幕截图:

屏幕截图之前应用宏:

应用宏之后的屏幕截图:

我想要达到的目标:

解决方案

您没有提供用于搜索阿拉伯语术语的代码,但根据问题中发布的单词,该问题似乎与阿拉伯语为 RTL 相关在 LTR 文档中.我无法在宏代码中使用阿拉伯字符进行测试 - 即使按照您提供的链接中的说明进行操作,我的 VBA 编辑器也只显示 ? - 可能是因为我没有安装阿拉伯语 IME...

在任何情况下,我都能够使用 Unicode 字符的十进制数表示来让它工作.诀窍是将它们放在相反的顺序 (RTL) 中,以便:

strSearch = ChrW(1603) &ChrW(1604) &ChrW(1605) &ChrW(1577)'而不是 ChrW(1577) &ChrW(1605) &ChrW(1604) &ChrW(1603)

请求已更改为允许将十进制数作为字符分隔的字符串值提供.这可以拆分为一个数组,循环将每个值放入 ChrW 函数中,并将这些值连接到搜索字符串中:

Dim strSearch As String, Word1 As StringDim valWord1 作为变体昏暗的我Word1 = "1603,1604,1605,1577"valWord1 = Split(Word1, ",")对于 i = LBound(valWord1) 到 UBound(valWord1)strSearch = strSearch &ChrW(valWord1(i))下一个

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:

解决方案

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

这篇关于用于搜索文本字符串并替换为超链接的 VBA 宏仅适用于英文文本,不适用于阿拉伯语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆