问题
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 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)
This is the URL i am navigating to:
https://translate.google.com/#view=home&op=translate&sl=ja&tl=en
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(議事板書)」を送付いたします。よろしくお願い致します。
結論:途中 下記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つまりデータの再確認を実施する事(アルミボア化)
<\Jp-nml-fs-a01\U00\PT共通(インフラ)\A01_集約(フリー)\3悪格納フォルダ2\111117_NAM_L42L_TR2k2_3悪>
回答1:
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.
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.
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
来源:https://stackoverflow.com/questions/53640216/google-translate-not-translating-in-ie-when-website-opened-from-vba