Excel vba Translate IE.Document empty

不羁岁月 提交于 2021-01-28 13:00:44

问题


This is a VBA script i am using to translate fields in a excell sheet. The problem is that the script works for me about 2 3 month ago, but now the IE.Document is empty after translating. The page comes up with correct translation but i can't get the result inside my excel sheet

inputstring = "en"
outputstring = "da"
text_to_convert = str

'open website

IE.Visible = True
IE.navigate "https://translate.google.com/#" & inputstring & "/" & outputstring & "/" & text_to_convert

Do Until IE.ReadyState = 4
    DoEvents
Loop

Application.Wait (Now + TimeValue("0:00:4"))

Do Until IE.ReadyState = 4
    DoEvents
Loop
test = IE.Document.getElementById("result_box")

回答1:


Use a proper page wait. I also use a different element selector

Option Explicit
Public Sub GetTranslation()
    Dim ie As New InternetExplorer, ws As Worksheet
    Dim inputString As String, outputString As String, text_to_convert As String, translation As String
    inputString = "en"
    outputString = "da"
    text_to_convert = "Banana"
    Set ws = ThisWorkbook.Worksheets("Sheet1")

    With ie
        .Visible = True
        .Navigate2 "https://translate.google.com/#" & inputString & "/" & outputString & "/" & text_to_convert
        While .Busy Or .readyState < 4: DoEvents: Wend
        translation = ie.document.querySelector(".translation").innerText
        ws.Cells(1, 1) = translation
        .Quit
    End With
End Sub


来源:https://stackoverflow.com/questions/54667280/excel-vba-translate-ie-document-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!