I am using the Google Translate API in Excel so I can translate Chinese into English. However, if I want to translate English into Chinese, I only get the Chinse transliteration and not translation in Chinese letter/alphabet.
Do I have to change sth. within the code or do I have to change Excel settings?
Thank you!
Public Function Translate(rng As Range, Optional translateFrom As String = "en", Optional translateTo As String = "zh")
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.com/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"".*?>(.+?)</div>")
Translate = Clean(trans)
Else
Translate = CVErr(xlErrValue)
End If
End Function
Sub TranslateCell()
Dim getParam As String, trans As String, translateFrom As String, translateTo As String
translateFrom = "zh"
translateTo = "en"
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
Dim r As Range, cell As Range
Set cell = Selection
For Each cell In Selection.Cells
getParam = ConvertToGet(cell.Value)
URL = "https://translate.google.com/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"".*?>(.+?)</div>")
cell.Value = Clean(trans)
Else
MsgBox ("Error")
End If
Next cell
End Sub