Google translate excel vba

sadath

Active Member
Joined
Oct 10, 2004
Messages
265
Office Version
  1. 365
Platform
  1. Windows
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

Code:
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
 
Last edited by a moderator:

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I have found another one but english to arabic is not working.

any one please fix it
HTML:
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
 
Last edited by a moderator:
Upvote 0
Updated

IE.Document.getElementByID("result_box")
IE.Document.querySelector(".tlid-translation.translation")
VBA Code:
'
' 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>", ""), "<")
 
Upvote 0
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?
 
Upvote 0
Updated

IE.Document.getElementByID("result_box")
IE.Document.querySelector(".tlid-translation.translation")
VBA Code:
'
' 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 [URL='https://www.mcdvoice.page/']mcdvoice[/URL]
CLEAN_DATA = Split(Application.WorksheetFunction.Substitute( IE.Document.querySelector(".tlid-translation.translation").innerText, "</SPAN>", ""), "<")
Thank you for the explanation. It is clear for me.
 
Upvote 0
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
 
Last edited:
Upvote 0
VBA Code:
 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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,223,721
Messages
6,174,096
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top