从VBA打开网站时,Google翻译无法在IE中进行翻译 [英] Google translate not translating in IE when website opened from vba

查看:112
本文介绍了从VBA打开网站时,Google翻译无法在IE中进行翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google翻译不翻译文本(日语到英语).打开网站并使用以下代码输入文本时,该函数始终返回":

Google translate is not translating text (Japanese to English). The function always returns "" when the website is opened and the text is input using the following code:

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

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

IE gives this error when i try to manually click the translate arrow/button on the website: SCRIPT5025: The URI to be decoded is not a valid encoding translate_m.js (207,484)

这是我导航到的URL:

This is the URL i am navigating to:

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

这是我设置为trans_text的文本,该文本是使用其.body属性从Outlook电子邮件中提取的:

Here is the text i am setting as trans_text this text is taken from an outlook email using its .body property:

关系各位

いつもお世话になっております. "111117_NAM_L42L_TR2k2_3悪_第1部第2部.pdf(议事板书)"を送付いたします.よろしくお愿い致します.

いつもお世話になっております。 「111117_NAM_L42L_TR2k2_3悪_第1部第2部.pdf(議事板書)」を送付いたします。よろしくお願い致します。      

论文:途中 下记13件のスがありフ ォ ロ ーを実施する

  結論:途中   下記13件の課題がありフォローを実施する

エ ン担,プ ロ セ ン ・残渣対応を実施対応を実事(D レ ン ジ ア イ ド​​ ル回転検讨,ア イ ド​​ ル振动デ ー タ入手,制御不具合対応,全ROM定数チ ェ ッ ク,燃料PUMP不具合调查,冷房性能未达対応) ・ T/M IPのFKとFKk2との比较を提示する事 ・ サ ー モ ス タ ッ トのメ ー カ ーを再确认すること ・始动エ ン ス ト チ ェ ッ ク リ ス トの再チ ェ ッ クを実施する事(エ ン ス ト余裕率,パ ー ジの判断,AF/M电源摄取特性) 始动ISC
・不悬残项目の対応を実施する事(车両振动,始动即R セ レ ク ト エ ン ス ト,A/F回転総チ ェ ッ ク,常温始动軽质确认,かぶり,セ レ ク ト ア イ フ ラ,PCV ハ ン チ ン グ确认,即始动确认,押し出され确认) ・ H1を最终仕様でまとめる事 ・各相场チ ェ ッ ク デ ー タの古いL42Lのラ イ ンを削除する事 ・ ブ レ ー キ エ ン ス ト回転低下时の车両振动への影响を确认する事 ・低温始动时のTpを确认する事 ・ ク ラ ン キ ン グ回転数の0℃の目标値の出所を确认する事 台上适合 ・ H2全ROM定数チ ェ ッ クを実施する事 ・不悬残项目の対応を実施する事(R/L サ ー ジ,暖机中ラ フ ア イ ド​​ ル,ロ ジ ッ ク バ グ修正后の确认,ブ ロ ッ ク _ ヘ ッ ト変更后のノ ッ ク确认) ・ BCV确认のTH/Cつまりデ ー タの再确认を実施する事(ア ル ミ ボ ア化)

     エン担,プロセン       ・残課題対応を実施する事(Dレンジアイドル回転検討,アイドル振動データ入手,制御不具合対応,全ROM定数チェック,燃料PUMP不具合調査,冷房性能未達対応)        ・T/M IPのFKとFKk2との比較を提示する事        ・ サーモスタットのメーカーを再確認すること       ・始動エンストチェックリストの再チェックを実施する事(エンスト余裕率,パージの判断,AF/M電源投入特性)     始動ISC  
        ・不懸残項目の対応を実施する事(車両振動,始動即Rセレクトエンスト,A/F回転総チェック,常温始動軽質確認,かぶり,セレクトアイフラ,PCVハンチング確認,即始動確認,押し出され確認)         ・H1を最終仕様でまとめる事             ・各相場チェックデータの古いL42Lのラインを削除する事         ・ブレーキエンスト回転低下時の車両振動への影響を確認する事         ・低温始動時のTpを確認する事         ・クランキング回転数の0℃の目標値の出所を確認する事     台上適合         ・H2全ROM定数チェックを実施する事         ・不懸残項目の対応を実施する事(R/Lサージ,暖機中ラフアイドル,ロジックバグ修正後の確認,ブロック_ヘット変更後のノック確認) ・BCV確認のTH/Cつまりデータの再確認を実施する事(アルミボア化)

< \ Jp-nml-fs-a01 \ U00 \ PT共通(インフラ)\ A01_集约(フリー)\ 3悪格纳フ ォ ル ダ 2 \ 111117_NAM_L42L_TR2k2_3悪>

<\Jp-nml-fs-a01\U00\PT共通(インフラ)\A01_集約(フリー)\3悪格納フォルダ2\111117_NAM_L42L_TR2k2_3悪>

推荐答案

略微更改您的URL,并让浏览器处理URL的编码.在这里,我正在从一个单元格中读取您的文本.当站点进行翻译时,它会生成一个新的URL,该URL的查询字符串参数为text=<your string to translate>.因此,将text=添加到您的起始URL上,并连接成短语进行翻译.

Alter your URL slightly and let the browser handle the encoding of the URL. Here I am reading your text from a cell. When the site does a translation it generates a new URL which has a query string parameter of text=<your string to translate>; so add the text= onto your start URL and concatenate in your phrase for translation.

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

Remember you need to close your IE instance at some point. I am not a fan of passing IE as an object in this manner and would probably look to having it held in a class which generates the IE object in class Class_Initialize(). You then instantiate that class with a variable in the sub.

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

I frequently forget that the appropriate navigate method I believe is now .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

这篇关于从VBA打开网站时,Google翻译无法在IE中进行翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆