Results 1 to 2 of 2

Thread: Speed up and improve Google Translate code

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    San Jose, CA

    Speed up and improve Google Translate code

    I have the following code which is used to translate the text in a worksheet's cells from any language to English. It is called from a For-Each-cell loop. It works most of the time but once in a while the source cell is blanked out because objDiv doesn't return a translation. If I restore the cell and run the loop again, that cell or randomly other cells my be blanked out. Also calling the routine from a loop means that every time it's called the objects need to be recreated which I assume is wasteful of time. Finally, if there is a lot of data to translate, Excel bogs down and eventually doesn't respond. Can someone improve this for me?

    Private Function GoogleTranslate(strInput As String, strFromLang As String, strToLang As String) As String
    Dim strURL As String
    Dim objHTTP As Object
    Dim objHTML As Object
    Dim objDivs As Object
    Dim objDiv As Variant
        strURL = "" & strFromLang & _
            "&sl=" & strFromLang & _
            "&tl=" & strToLang & _
            "&ie=UTF-8&prev=_m&q=" & strInput
        Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
        objHTTP.Open "GET", strURL, False
        objHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
        objHTTP.send ""
        Set objHTML = CreateObject("htmlfile")
        With objHTML
            .Write objHTTP.ResponseText
        End With
        Set objDivs = objHTML.getElementsByTagName("div")
        For Each objDiv In objDivs
            If objDiv.className = "t0" Then
                GoogleTranslate = objDiv.innerText
                Exit For
            End If
        Next objDiv
        Set objHTML = Nothing
        Set objHTTP = Nothing
        Set objDivs = Nothing
    End Function

  2. #2
    Join Date
    Dec 2004

    Re: Speed up and improve Google Translate code

    does google have any API for google translate?

    or would it be possible to submit the whole page to google to translate rather than translating all the parts?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width