Google Translate by Internet Explorer AutomationThis VBA Macro Code translates text using Google Translate by automation of Internet Explorer controlled through Excel.
ExplanationThe program automatically writes text from Excel into Internet Explorer and uses the Google Translate service to translate the text into desires language. It is possible to translate from and to many different languages, just change the language code and the program is set up accordingly. Today all the major languages are available using google translate. It is one of few services that enables translation of entire sentences not just words. The service is free of charge and can be executed through API. The exact technology used for the translation is not public. Google has started to translate entire web pages on the internet as well when using the google seach function. The entire VBA/Excel program is available for download at the bottom of this page, enjoy!
CodePublic Sub Google_Translate()
Dim Google_Translate_Internet_Explorer_Automation As Object Set Google_Translate_Internet_Explorer_Automation = CreateObject("InternetExplorer.Application") Google_Translate_Internet_Explorer_Automation.Navigate "http://translate.google.com/translate_t#" Google_Translate_Internet_Explorer_Automation.Visible = True Wait_Between_Google_Translate_Cycles = Range("G1").Value
Column = 0 While Range("f9").Offset(0, Column).Value <> tom
Do While Google_Translate_Internet_Explorer_Automation.busy Call WaitSeconds(Wait_Between_Google_Translate_Cycles) Loop to_language_code = Range("f9").Offset(0, Column).Value
Do While Google_Translate_Internet_Explorer_Automation.busy Call WaitSeconds(Wait_Between_Google_Translate_Cycles) Loop
Google_Translate_Internet_Explorer_Automation.document.forms("text_form").elements(5).Value = to_language_code
Do While Google_Translate_Internet_Explorer_Automation.busy Call WaitSeconds(Wait_Between_Google_Translate_Cycles) Loop
rad = 0 While Range("c10").Offset(rad, 0).Value <> tom
If Range("f10").Offset(rad, Column).Value = tom Then
from_language_code = Range("a10").Offset(rad, 0).Value Google_Translate_Internet_Explorer_Automation.document.forms("text_form").elements(4).Value = from_language_code
Do While Google_Translate_Internet_Explorer_Automation.busy Call WaitSeconds(Wait_Between_Google_Translate_Cycles) Loop
Google_Translate_Text = Range("c10").Offset(rad, 0).Value
While Google_Translate_Internet_Explorer_Automation.busy Call WaitSeconds(Wait_Between_Google_Translate_Cycles) Wend
Google_Translate_Internet_Explorer_Automation.document.forms("text_form").elements("source").Value = Google_Translate_Text
While Google_Translate_Internet_Explorer_Automation.busy Call WaitSeconds(Wait_Between_Google_Translate_Cycles) Wend
Google_Translate_Internet_Explorer_Automation.document.getElementById("text_form").submit
While Google_Translate_Internet_Explorer_Automation.busy Call WaitSeconds(Wait_Between_Google_Translate_Cycles) Wend
dd2 = Google_Translate_Internet_Explorer_Automation.document.forms(1).elements(4).Value
While Google_Translate_Internet_Explorer_Automation.busy Call WaitSeconds(Wait_Between_Google_Translate_Cycles) Wend
Google_Translate_Variable1 = Replace(dd2, Chr(13), "") Range("f10").Offset(rad, Column).Value = Google_Translate_Variable1
End If
rad = rad + 1 Wend
Column = Column + 1 Wend
Google_Translate_Internet_Explorer_Automation.Quit Set Google_Translate_Internet_Explorer_Automation = Nothing
End Sub
Public Sub WaitSeconds(sek)
newHour = Hour(Now()) newMinute = Minute(Now()) newSecond = Second(Now()) + sek waitTime = TimeSerial(newHour, newMinute, newSecond) Application.Wait waitTime
End Sub
Download excel file! Google_Translate_Internet_Explorer_Automation.xls |
But it does not pick up language codes given in Excel. The workaround: change the languages in Google translator webbrowser window (manually).
Thanks!
I would need your a little bit help. It only works translation from different language ex English / Polish to German, but it does not work fully translation to English.
Somehow it stopp on the second colum (example) DE_EN or PL_EN the translation to English and turn to translate to German.
Is it somethink you can help me ?
Regards
Maggie
Dim objGoog As Object
Dim strText As String
Dim waittime As Date
On Error GoTo Err_Handler
If strLangTo = strLangFrom Then
fnGoogTranslate = strData
Exit Function 'don't waste my time
End If
If strData "" Then
Set objGoog = CreateObject("InternetExplorer.Application")
objGoog.Navigate "http://translate.google.com/?sl=" & strLangTo & "&tl=" & strLangFrom & "#" & strLangFrom & "|" & strLangTo & "|" & strData
objGoog.Visible = False
'objGoog.Visible = True
waittime = Now + TimeValue("00:00:" & Timeout)
Do Until fnGoogTranslate ""
DoEvents
fnGoogTranslate = objGoog.Document.all("result_box").innertext
If Now() >= waittime Then
'the function timed out
'default to the origial language
fnGoogTranslate = strData
Exit Do
End If
Loop
If UniCodeID 0 Then
fnGoogTranslate = StrConv(fnGoogTranslate, vbUnicode, UniCodeID)
End If
If fnGoogTranslate = "" Or fnGoogTranslate = strData Then
fnGoogTranslate = ""
End If
objGoog.Quit
Set objGoog = Nothing
End If
Exit Function
Err_Handler:
Select Case Err.Number
Case Else
sbLogError Err.Number & " " & Err.Description & " In Function: fnGoogTranslate"
Resume Next
End Select
End Function
dd2 = Google_Translate_Internet_Explorer_Automation.document.forms(1).elements(4).Value
is no longer working. You should use:
.Document.all("result_box").innertext
from now on
@Mark Freund: Your mods were very helpful and led me to understand how to fix the part that others are still having trouble with, namely the variable 'dd2'. In addition to the adjustments @Mark has listed below, make the following adjustment to your code.
From:
dd2 = Google_Translate_Internet_Explorer_Automation.document.forms(1).elements(4).Value
To:
dd2 = Google_Translate_Internet_Explorer_Automation.document.getElementById("result_box").innerText
There are no guarantees that this will work going forward since Google is apt to change it again. But it works for now!
Happy translating!
Mark
I have the error message: "Object variable or With block variable not set" with this line:
dd2 = Google_Translate_Internet_Explorer_Automation.document.forms(1).elements(4).Value
Can you help please?
Thanks a lot
Thanks
I do have small query about this.
I noticed that macro do not always select source and target languages from XLS cells. Once it is set to particular language pair then it will only translate it into same language pair although you have specified different language pairs.
Could you please do something about this?
Thanks!
From
..elements(5).Value = to_language_code
To
..elements("sl").Value = to_language_code
and
From
..elements(4).Value = from_language_code
To
..elements("tl").Value = from_language_code
also added this
Range("f10").Offset(rad, Column).select
immediately after
If Range("f10").Offset(rad, Column).Value = tom Then
to keep a watch on progress.
Thanks again!!
Mark
Regards,
Admin
I have an excel file I'd like to translate from french into english
I don't understand how to do with your macro (bug messages, nothing runs)
could u help pls???
thanks