Google仅在非英语的情况下翻译文本

时间:2019-02-04 09:18:10

标签: excel vba

我正在将google警报导入到我的excel工作表中,如果文本不是英语,我想做的就是仅使用我拥有的google翻译代码。由于代码很慢。我最多可以导入1000行文本。其中大多数已经是英文了。但是目前,我的代码会翻译每一行。

Public Sub Translate()

    Const MAX_WAIT_SEC As Long = 5
    Dim IE As New InternetExplorer
    Dim t As Date
    Dim ws As Worksheet
    Dim ftext As String
    Dim x
    Dim y As Long
    Dim translation As Object
    Dim translationText As String

    y = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
    Set ws = ThisWorkbook.Worksheets("Google_Notifications")
    For x = 1 To y
        With IE
            .Visible = False
            .Navigate "https://translate.google.com/#view=home&op=translate&sl=auto&tl=en"
            While .Busy Or .ReadyState < 4: DoEvents: Wend
            ftext = Sheet1.Range("C" & x).Value
            .Document.querySelector("#source").Value = ftext
            While .Busy Or .ReadyState < 4: DoEvents: Wend
            t = Timer
            Do
                On Error Resume Next
                Set translation = .Document.querySelector(".tlid-translation.translation")
                translationText = translation.textContent
                On Error GoTo 0
                If Timer - t > MAX_WAIT_SEC Then Exit Do
            Loop While translationText = vbNullString
            Sheet1.Range("C" & x).Value = translationText
            Sheet1.Range("E" & x).Value = "Translated"
            .Quit
            Set IE = Nothing
            Set translation = Nothing
            translationText = ""
        End With
    Next x

End Sub

0 个答案:

没有答案