Google translate excel vba

sadath

Board Regular
Joined
Oct 10, 2004
Messages
244
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:

Some videos you may like

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

sadath

Board Regular
Joined
Oct 10, 2004
Messages
244
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:

Dossfm0q

Board Regular
Joined
Mar 9, 2009
Messages
225
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>", ""), "<")
 

usui

New Member
Joined
Apr 20, 2020
Messages
9
Office Version
2016, 2013
Platform
Windows
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

Spammer
Joined
Jul 24, 2020
Messages
2
Office Version
365
Platform
Windows
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.
 

Dossfm0q

Board Regular
Joined
Mar 9, 2009
Messages
225
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
 

Watch MrExcel Video

Forum statistics

Threads
1,102,351
Messages
5,486,356
Members
407,541
Latest member
Emilybuhman

This Week's Hot Topics

Top