Google 使用 VBA 翻译单元格值
Google translate cell value using VBA
我正在尝试通过触发以下代码使用自动检测将其他语言转换为英语。
Sub transalte_using_vba()
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")
inputstring = "auto"
outputstring = "en"
text_to_convert = Sheet3.Range("A2")
'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 i = LBound(CLEAN_DATA) To UBound(CLEAN_DATA)
result_data = result_data & Right(CLEAN_DATA(i), Len(CLEAN_DATA(i)) - InStr(CLEAN_DATA(i), ">"))
Next
Sheet3.Range("B2") = result_data
ie.Quit
MsgBox "Done", vbOKOnly
End Sub
但是我在行 CLEAN_DATA = Split(Application.WorksheetFunction.Substitute(ie.Document.getElementById("result_box").innerHTML, "</SPAN>", ""), "<")
中遇到 需要运行时错误 424 对象
代码有什么问题?
此代码运行速度有点慢..因为我需要处理超过 70K 的批量数据,有什么快速的方法可以做到这一点吗?
在我的系统中,我有 google chrome 作为默认浏览器,我们可以用它来翻译吗,这可能有助于 运行 脚本更快?
根据定义,基于 Internet Explorer 的解决方案非常慢。请尝试下一个功能:
Private Function GTranslate(strInput As String, strFromLang As String, strToLang As String) As String
Dim strURL As String, objHTTP As Object, objHTML As Object, objDivs As Object, objDiv As Variant
strInput = WorksheetFunction.EncodeURL(strInput)
strURL = "https://translate.google.com/m?hl=" & strFromLang & _
"&sl=" & strFromLang & _
"&tl=" & strToLang & _
"&ie=UTF-8&prev=_m&q=" & strInput
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
objHTTP.Open "GET", strURL, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.Send ""
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
GTranslate = objDiv.innerText: Exit For
End If
Next objDiv
Set objHTML = Nothing: Set objHTTP = Nothing
End Function
可以通过这种简单的方式进行测试:
Sub testTranslateG()
Debug.Print GTranslate("Libro muy grande", "auto", "en")
End Sub
或者翻译一个范围内的单元格值:
Private Sub Google_translate()
Dim thisWbs As Worksheet
Dim i As Long, lastRow As Long
Set thisWbs = ActiveSheet
lastRow = thisWbs.Range("B" & rows.count).End(xlUp).row
thisWbs.Range("C2:C" & lastRow).Clear
For i = 2 To lastRow
thisWbs.Range("C" & i).Value = GTranslate(thisWbs.Range("B" & i).Value, "auto", "en")
Next i
MsgBox "Ready..."
End Sub
为了获得更准确的翻译,您可以使用(而不是“auto”):“es”代表西班牙语,“ru”代表俄语,“ro”代表罗马尼亚语,“nl”代表“Duch”等。您可以通过查看 Google 翻译源并搜索 'English' 来找到语言缩写。您会发现一个区域,其中显示所有可能的语言及其使用的缩写...
@FaneDuru 解决方案适用于这些修复:
类似于Youtube Automate Language Translations Using Excel VBA by Dinesh Kumar Takyar
"t0"
已更改。
将If objDiv.className = "t0" Then
替换为
If objDiv.className = "result-container" Then
注意:不要看Google翻译普通网页HTML,“/m?”代表“移动”,使用 Google 翻译移动页面,该页面具有不同且更简单的 HTML 代码。
我正在尝试通过触发以下代码使用自动检测将其他语言转换为英语。
Sub transalte_using_vba()
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")
inputstring = "auto"
outputstring = "en"
text_to_convert = Sheet3.Range("A2")
'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 i = LBound(CLEAN_DATA) To UBound(CLEAN_DATA)
result_data = result_data & Right(CLEAN_DATA(i), Len(CLEAN_DATA(i)) - InStr(CLEAN_DATA(i), ">"))
Next
Sheet3.Range("B2") = result_data
ie.Quit
MsgBox "Done", vbOKOnly
End Sub
但是我在行 CLEAN_DATA = Split(Application.WorksheetFunction.Substitute(ie.Document.getElementById("result_box").innerHTML, "</SPAN>", ""), "<")
代码有什么问题?
此代码运行速度有点慢..因为我需要处理超过 70K 的批量数据,有什么快速的方法可以做到这一点吗?
在我的系统中,我有 google chrome 作为默认浏览器,我们可以用它来翻译吗,这可能有助于 运行 脚本更快?
根据定义,基于 Internet Explorer 的解决方案非常慢。请尝试下一个功能:
Private Function GTranslate(strInput As String, strFromLang As String, strToLang As String) As String
Dim strURL As String, objHTTP As Object, objHTML As Object, objDivs As Object, objDiv As Variant
strInput = WorksheetFunction.EncodeURL(strInput)
strURL = "https://translate.google.com/m?hl=" & strFromLang & _
"&sl=" & strFromLang & _
"&tl=" & strToLang & _
"&ie=UTF-8&prev=_m&q=" & strInput
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
objHTTP.Open "GET", strURL, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.Send ""
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
GTranslate = objDiv.innerText: Exit For
End If
Next objDiv
Set objHTML = Nothing: Set objHTTP = Nothing
End Function
可以通过这种简单的方式进行测试:
Sub testTranslateG()
Debug.Print GTranslate("Libro muy grande", "auto", "en")
End Sub
或者翻译一个范围内的单元格值:
Private Sub Google_translate()
Dim thisWbs As Worksheet
Dim i As Long, lastRow As Long
Set thisWbs = ActiveSheet
lastRow = thisWbs.Range("B" & rows.count).End(xlUp).row
thisWbs.Range("C2:C" & lastRow).Clear
For i = 2 To lastRow
thisWbs.Range("C" & i).Value = GTranslate(thisWbs.Range("B" & i).Value, "auto", "en")
Next i
MsgBox "Ready..."
End Sub
为了获得更准确的翻译,您可以使用(而不是“auto”):“es”代表西班牙语,“ru”代表俄语,“ro”代表罗马尼亚语,“nl”代表“Duch”等。您可以通过查看 Google 翻译源并搜索 'English' 来找到语言缩写。您会发现一个区域,其中显示所有可能的语言及其使用的缩写...
@FaneDuru 解决方案适用于这些修复:
类似于Youtube Automate Language Translations Using Excel VBA by Dinesh Kumar Takyar
"t0"
已更改。
将If objDiv.className = "t0" Then
替换为
If objDiv.className = "result-container" Then
注意:不要看Google翻译普通网页HTML,“/m?”代表“移动”,使用 Google 翻译移动页面,该页面具有不同且更简单的 HTML 代码。