Excel VBA-制作翻译程序时出现ie.navigate功能问题

时间:2017-06-29 06:34:24

标签: excel vba excel-vba internet-explorer

我尝试通过制作ie.visible(true)来解决问题。事实证明,当我使用时:

 ie.navigate "https://translate.google.com/#" & ilang & "/" & olang & "/" & cell.Value

它不会重新加载页面。 例如,请参阅图像:

http://i.imgur.com/udr7Iqo.jpg

http://imgur.com/33HOyNB.jpg

因此,它会在地址栏中输入新网址,但实际上导航到该网址。
代码在这里(子程序):

Sub Test()
    Dim str As String
    str = Translate(Range("B3:B7"), "auto", "hi")
End Sub

实际代码:

'A function called translate
Public Function Translate(r As Range, ilang As String, olang As String) As String
    'Declaring all variables
    Dim ie As InternetExplorer
    Dim y As Integer
    Dim ys As String
    Dim urmsg As String
    Dim result As String

    y = 0
    Set ie = New InternetExplorer

    ie.Visible = True

    For Each cell In r
        ie.navigate "https://translate.google.com/#" & ilang & "/" & olang & "/" & cell.Value
        Do While ie.Busy = True Or ie.readyState <> 4: DoEvents: Loop

        y = y + 1
        result = ie.document.getElementById("result_box").innerText
        Sheets("Sheet1").Range("G" & y).Value = result
    Next cell

    urmsg = MsgBox(ys & " Translations Executed...", vbOK, "Prompt")
    ie.Quit
    Translate = " "
End Function

请告诉我这是什么问题,以便我能解决。另外,如果代码非常不稳定并且有不必要的行,请放轻松我,我只研究了VBA几天。

谢谢。

0 个答案:

没有答案