Google 从 vba 打开网站时在 IE 中不翻译

Google translate not translating in IE when website opened from vba

Google translate 不是在翻译文本(日语到英语)。]

Function OutlookGetTransItem(IE As Object, URL As String, trans_text As String) As String
Dim t As Date

If trans_text = "" Then OutlookGetTransItem = trans_text: Exit Function

Const MAX_WAIT_SEC As Long = 5

With IE
    .Visible = True
    .navigate URL

    While .Busy Or .ReadyState < 4: DoEvents: Wend

    .Document.querySelector("#source").Value = trans_text

    Dim translation As Object, translationText As String
    t = Timer
    Do
        On Error Resume Next
        Set translation = .Document.querySelector(".tlid-translation.translation")
        translationText = translation.innerText
        On Error GoTo 0
        If Timer - t > MAX_WAIT_SEC Then Exit Do
    Loop While translationText = vbNullString
    OutlookGetTransItem = translationText
End With
End Function

当我尝试手动单击网站上的翻译 arrow/button 时,IE 出现此错误: SCRIPT5025:要解码的 URI 不是有效编码 translate_m.js (207,484)

这是我要导航到的 URL:

https://translate.google.com/#view=home&op=translate&sl=ja&tl=en

这是我设置为 trans_text 的文本,此文本是使用 .body 属性:

从 Outlook 电子邮件中提取的

亲爱的大家,

感谢您一直以来的支持。 我们将向您发送“111117_NAM_L42L_TR2k2_3 Evil_Part 1 Part 2.pdf(会议记录委员会)”。非常感谢你。 〉〉〈〉〈〉

结论:中途 〉〉下面有13个问题,我们会跟进

恩 负责,专业 ・应对遗留问题(D档怠速旋转的研究、怠速振动数据的获取、控制问题的应对、所有ROM常数的检查、燃油泵问题的调查、未达到的冷却性能的支持) ‍‍‍‍‍‍・T/M 比较IP的FK和FKk2 ・ 再次确认温控器的制造商 ・重新检查启动发动机失速检查表(发动机失速裕度、吹扫确定、AF/M 通电特性) ‍‍‍‍开始ISC‍‍‍
・对无关项实施措施(车辆振动、启动后R选择启动、A/F总旋转检查、室温启动亮度检查、起雾、选择eyefla、PCV狩猎检查、立即启动检查、推出验证) ‍‍‍‍‍‍‍用最终规格总结H1 ‍‍‍‍‍‍‍ · 删除各行情查询数据的旧L42L行 〉〉〉〈〉〉确认制动发动机转速下降时对车辆振动的影响。 〉〉〈〉〉低温启动时检查Tp ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍ 适用于长凳 〉〉〈〉〉H2所有ROM常量检查 ‍‍‍‍‍‍・针对无关项的实施措施(R/L 浪涌,预热期间的粗暴空闲,逻辑错误修正后的确认,更改块头后的敲击确认) ・确认BCVTH/C,即再次确认数据(铝孔)

<\Jp-nml-fs-a01\U00\PTCommon (Infrastructure)\A01_Consolidation (Free)\3 Bad Storage Folder21117_NAM_L42L_TR2k2_3 Bad>

稍微改变一下 URL,让浏览器处理 URL 的编码。在这里,我正在从一个单元格中读取您的文本。当该站点进行翻译时,它会生成一个新的 URL,其中的查询字符串参数为 text=<your string to translate>;所以将 text= 添加到您的开头 URL 并连接到您的短语中以进行翻译。

请记住,您有时需要关闭 IE 实例。我不喜欢以这种方式将 IE 作为对象传递,并且可能希望将其保存在 class 中,该 class 在 class Class_Initialize() 中生成 IE 对象。然后,您使用子中的变量实例化 class。

我经常忘记我认为合适的导航方法现在是 .navigate2

Option Explicit

Public Sub test()
    Dim ie As InternetExplorer, trans_text As String
    Const URL As String = "https://translate.google.com/#view=home&op=translate&sl=ja&tl=en&text="
    Set ie = New InternetExplorer
    trans_text = [A1].Value
    Debug.Print OutlookGetTransItem(ie, URL, trans_text)
    ie.Quit
End Sub

Public Function OutlookGetTransItem(ByVal ie As Object, ByVal URL As String, ByVal trans_text As String) As String
    Dim t As Date

    If trans_text = vbNullString Then OutlookGetTransItem = trans_text: Exit Function

    Const MAX_WAIT_SEC As Long = 5

    With ie
        .Visible = True
        .navigate2 URL & trans_text

        While .Busy Or .readyState < 4: DoEvents: Wend

        Dim translation As Object, translationText As String
        t = Timer
        Do
            On Error Resume Next
            Set translation = .document.querySelector(".tlid-translation.translation")
            translationText = translation.innerText
            On Error GoTo 0
            If Timer - t > MAX_WAIT_SEC Then Exit Do
        Loop While translationText = vbNullString
        OutlookGetTransItem = translationText
    End With
End Function