# Google translate excel vba



## sadath (Dec 1, 2018)

Hi

I use below =Translate() function to translate a text

the below code works on all language except english to arabic  (it works arabic to english)
what is wrong in this code??   or can anyone suggest a alternate code for translating english to arabic


```
Function ConvertToGet(val As String)
    val = Replace(val, " ", "+")
    val = Replace(val, vbNewLine, "+")
    val = Replace(val, "(", "%28")
    val = Replace(val, ")", "%29")
    ConvertToGet = val
End Function
Function Clean(val As String)
    val = Replace(val, """, """")
    val = Replace(val, "%2C", ",")
    val = Replace(val, "'", "'")
    Clean = val
End Function
Public Function RegexExecute(str As String, reg As String, _
                             Optional matchIndex As Long, _
                             Optional subMatchIndex As Long) As String
    On Error GoTo ErrHandl
    Set regex = CreateObject("VBScript.RegExp"): regex.Pattern = reg
    regex.Global = Not (matchIndex = 0 And subMatchIndex = 0) 'For efficiency
    If regex.Test(str) Then
        Set matches = regex.Execute(str)
        RegexExecute = matches(matchIndex).SubMatches(subMatchIndex)
        Exit Function
    End If
ErrHandl:
    RegexExecute = CVErr(xlErrValue)
End Function

Public Function Translate(rng As Range, Optional translateFrom As String = "en", Optional translateTo As String = "ar")
    Dim getParam As String, trans As String, objHTTP As Object, URL As String
    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
    getParam = ConvertToGet(rng.Value)
    URL = "https://translate.google.pl/m?hl=" & translateFrom & "&sl=" & translateFrom & "&tl=" & translateTo & "&ie=UTF-8&prev=_m&q=" & getParam
    objHTTP.Open "GET", URL, False
    objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    objHTTP.send ("")
    If InStr(objHTTP.responseText, "div dir=""ltr""") > 0 Then
        trans = RegexExecute(objHTTP.responseText, "div[^""]*?""ltr"".*?>(.+?)")
        Translate = Clean(trans)
    Else
        Translate = CVErr(xlErrValue)
    End If
End Function
```


----------



## sadath (Dec 3, 2018)

I have found another one but english to arabic is not working.

any one please fix it

```
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
```


----------



## Dossfm0q (Mar 28, 2020)

Updated

IE.Document.getElementByID("result_box")
IE.Document.querySelector(".tlid-translation.translation")

```
'
' CLEAN_DATA = Split(Application.WorksheetFunction.Substitute(IE.Document.getElementByID("result_box").innerHTML, "</SPAN>", ""), "<")
CLEAN_DATA = Split(Application.WorksheetFunction.Substitute(IE.Document.querySelector(".tlid-translation.translation").innerHTML, "</SPAN>", ""), "<")
' or
CLEAN_DATA = Split(Application.WorksheetFunction.Substitute( IE.Document.querySelector(".tlid-translation.translation").innerText, "</SPAN>", ""), "<")
```


----------



## usui (Jun 11, 2020)

hi guys,

i need help for a vba code that translate German to English, do you have a code that you can share, and what to do to make it work?


----------



## BlandMosquito (Jul 24, 2020)

Dossfm0q said:


> Updated
> 
> IE.Document.getElementByID("result_box")
> IE.Document.querySelector(".tlid-translation.translation")
> ...


Thank you for the explanation. It is clear for me.


----------



## Dossfm0q (Jul 24, 2020)

usui said:


> hi guys,
> 
> i need help for a vba code that translate German to English, do you have a code that you can share, and what to do to make it work?



Try This


----------



## Dossfm0q (Jul 24, 2020)

```
Sub Text()
 
 'Vba Code See Cell B1
 [A1] = "Schöne Grüße"
 
 Range("B1") = GglTranslate(Range("A1").Value, "de", "en")
'As Function into Cell see Cell A2
 Range("A2").Formula = "=GglTranslate(A1,""de"",""en"")"
 
    MsgBox GglTranslate("Schöne Grüße", "de", "en")
    MsgBox GglTranslate("greetings", "en", "de")
 End Sub

Public Function GglTranslate(strInput As String, FrmLng As String, ToLng As String) As String

    Dim strURL As String
    Dim objHTTP As Object
    Dim objHTML As Object
    Dim objDivs As Object, objDiv As Object
    Dim strTranslated As String
    
    ' send query to web page
    strURL = "https://translate.google.com/m?hl=" & FrmLng & _
        "&sl=" & FrmLng & _
        "&tl=" & ToLng & _
        "&ie=UTF-8&prev=_m&q=" & strInput

    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP") 'late binding
    objHTTP.Open "GET", strURL, False
    objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    objHTTP.send ""
    ' create an html document
    Set objHTML = CreateObject("htmlfile")
    With objHTML
        .Open
        .Write objHTTP.responseText
        .Close
    End With
    
    Set objDivs = objHTML.getElementsByTagName("div")
  
    For Each objDiv In objDivs

        If objDiv.className = "t0" Then
            strTranslated = objDiv.innerText
            If strTranslated <> "" Then GglTranslate = strTranslated
          
        End If
        
    Next objDiv
    
    

    Set objHTML = Nothing
    Set objHTTP = Nothing

End Function
```


----------



## Hemantjain86 (Nov 26, 2020)

Somehow the above code for translate is not working for me, microsoft internal control is already activated in reference library. I dont have mozilla installed in my pc, is it might be the reason


----------

