I have this function that translates all selected cells with Google Translate.
I was working fine for years but it suddenly stopped to work for some reason.
Any idea why? I'm using Excel 2010.
Thanks in advance
Sub TranslateCell()
Dim getParam As String, trans As String, translateFrom As String, translateTo As String
translateFrom = "en"
translateTo = "fr"
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.fr/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
'----Used functions----
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
ConvertToGet()
andRegexExecute()
andClean()
. – WurtzobjHTTP.responseText
is when you reach that if statement? Does it still includediv dir="ltr"
? I'm guessing not, so that would mean that google changed something that you now have to figure out. – Outfight