I am looking to translate Excel workbook files with some content in cells and other content in text box objects placed throughout each sheet as efficiently as possible. I've found some great examples for creating VBA macros to translate text in cells, but these don't work for text within the text boxes.
It would also be awesome if someone could figure out how to run my TranslateCell macro below and the new text box translate macro across all the sheets in a workbook. Some of these files have up to 70 sheets so having to manually select cells/objects on each sheet to translate is still pretty time consuming.
I created the macro below for translating text in cells using the example from "David Iracheta" in his post with adjustments from "Foxfire And Burns Burns" Google Translate Using VBA - (Excel Macro) Issue. Pretty sure I need to at least change the "Set cell = Selection" and most other references to cells throughout the macro to make a version that does the same for text in text box objects, but I'm too inexperienced to figure it out on my own.
Sub TranslateCell()
'English Spanish Translator Using Google Translate
Dim getParam As String, trans As String, translateFrom As String, translateTo As String
'In translateFrom we will select the language from which we will translate E.g. "es" = Spanish
translateFrom = "es"
'In translateTo we select the language that we want to translate to. "en" = English
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.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"".*?>(.+?)</div>")
cell.Value = Clean(trans)
Else
cell.Value = Clean(CStr(Split(Split(objHTTP.responsetext, "<div class=""result-container"">")(1), "</div>")(0)))
'MsgBox ("Error")
End If
Next cell
End Sub
'----Functions Used----
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 greater 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
You need to break down your code a little to make the "Translate" a function which just translates the text passed to it.
Example:
This just handles Range/Shape, bit you could add methods to translate text in other objects which might be found on a sheet.
FYI if you really need to do much of this type of thing, then setting up an account so you can call the Google Translate API instead of using this workaround would probably be worth it. It's not very costly - eg see https://cloud.google.com/translate/pricing#basic-pricing