Excel函数将英文名称转换为阿拉伯语

时间:2016-01-13 20:11:04

标签: excel excel-vba vba

我正在尝试将一个VBA功能添加到我的Excel工作表中,我可以将这些名称翻译或转换为阿拉伯语,以便在我的数据库中停止它们。我添加了开发人员选项卡和打开编辑器,我添加了以下功能,但它无法正常工作...请帮助

Function Translate_To_Arabic(str) As String
' Tools Refrence Select Microsoft internet Control


    Dim IE As Object, i As Long
    Dim inputstring As String, outputstring As String, text_to_convert As String, result_data As String, CLEAN_DATA

    Set IE = CreateObject("InternetExplorer.application")
    '   TO CHOOSE INPUT LANGUAGE

    inputstring = "auto"

    '   TO CHOOSE OUTPUT LANGUAGE

    outputstring = "ar"

    text_to_convert = str

    'open website

    IE.Visible = False
    IE.navigate "http://translate.google.com/#" & inputstring & "/" & outputstring & "/" & text_to_convert

    Do Until IE.ReadyState = 4
        DoEvents
    Loop

    Application.Wait (Now + TimeValue("0:00:5"))

    Do Until IE.ReadyState = 4
        DoEvents
    Loop

    CLEAN_DATA = Split(Application.WorksheetFunction.Substitute(IE.Document.getElementById("result_box").innerHTML, "</SPAN>", ""), "<")

    For j = LBound(CLEAN_DATA) To UBound(CLEAN_DATA)
        result_data = result_data & Right(CLEAN_DATA(j), Len(CLEAN_DATA(j)) - InStr(CLEAN_DATA(j), ">"))
    Next


    IE.Quit
    transalte_using_vba = result_data


End Function

2 个答案:

答案 0 :(得分:2)

问题是你的代码的最后一行应该是:

Translate_To_Arabic = result_data

你误导了“翻译”

还要确保添加了对Microsoft Internet Controls的引用

答案 1 :(得分:0)

如果要将此功能用作UDF,请使用以下代码:

Function Translate_To_Arabic(Rng As Range) As String
' Tools Refrence Select Microsoft internet Control


    Dim IE As Object, i As Long
    Dim inputstring As String, outputstring As String, text_to_convert As String, result_data As String, CLEAN_DATA

    Set IE = CreateObject("InternetExplorer.application")
    '   TO CHOOSE INPUT LANGUAGE

    inputstring = "auto"

    '   TO CHOOSE OUTPUT LANGUAGE

    outputstring = "ar"

    text_to_convert = Rng.Text

    'open website

    IE.Visible = False
    IE.navigate "http://translate.google.com/#" & inputstring & "/" & outputstring & "/" & text_to_convert

    Do Until IE.ReadyState = 4
        DoEvents
    Loop

    Application.Wait (Now + TimeValue("0:00:5"))

    Do Until IE.ReadyState = 4
        DoEvents
    Loop

    CLEAN_DATA = Split(Application.WorksheetFunction.Substitute(IE.Document.getElementById("result_box").innerHTML, "</SPAN>", ""), "<")

    For j = LBound(CLEAN_DATA) To UBound(CLEAN_DATA)
        result_data = result_data & Right(CLEAN_DATA(j), Len(CLEAN_DATA(j)) - InStr(CLEAN_DATA(j), ">"))
    Next


    IE.Quit
    Translate_To_Arabic = result_data


End Function

您可以在B1:=Translate_To_Arabic(A1)中使用此公式在工作簿中使用此功能。现在,如果您在单元格A1中键入book,则会在单元格B1中看到كتاب。 如果您想将此功能用作加载项,请参阅this link