excelvbagoogle-translatemsgbox

Translate text using vba


Probably could be a rare petition, but here is the issue.

I am adapting an excel of a third-party to my organization. The excel is developed in English and the people of my organization just speaks Spanish. I want to use exactly the same code that the original worksheet have, I prefer don't touch it (although I can do it), so I want to use a function that every time that a msgbox appears (with the text in English), I translate the msgbox messages but without touching the original script. I am looking for a mask that could be called every time that a msgbox is invoked in the original code.

I prefer don't touch the original code because the third-party developer could change it frequently, and it could be very annoying to change the code every time that they do any little change.

Is that possible?


Solution

  • Here you go.

    Sub test()
        Dim s As String
        s = "hello world"
        MsgBox translate_using_vba(s)
    
    End Sub
    

    Function translate_using_vba(str) As String
    ' Tools Refrence Select Microsoft internet Control
    
    
        Dim IE As Object, i As Long
        Dim inputstring As String, outputstring As String, text_to_convert As String, result_data As String, CLEAN_DATA
    
        Set IE = CreateObject("InternetExplorer.application")
        '   TO CHOOSE INPUT LANGUAGE
    
        inputstring = "auto"
    
        '   TO CHOOSE OUTPUT LANGUAGE
    
        outputstring = "es"
    
        text_to_convert = str
    
        'open website
    
        IE.Visible = False
        IE.navigate "http://translate.google.com/#" & inputstring & "/" & outputstring & "/" & text_to_convert
    
        Do Until IE.ReadyState = 4
            DoEvents
        Loop
    
        Application.Wait (Now + TimeValue("0:00:5"))
    
        Do Until IE.ReadyState = 4
            DoEvents
        Loop
    
        CLEAN_DATA = Split(Application.WorksheetFunction.Substitute(IE.Document.getElementById("result_box").innerHTML, "</SPAN>", ""), "<")
    
        For j = LBound(CLEAN_DATA) To UBound(CLEAN_DATA)
            result_data = result_data & Right(CLEAN_DATA(j), Len(CLEAN_DATA(j)) - InStr(CLEAN_DATA(j), ">"))
        Next
    
    
        IE.Quit
        transalte_using_vba = result_data
    
    
    End Function