+ Reply to Thread
Results 1 to 9 of 9

Translate text from French to English

Hybrid View

  1. #1
    Registered User
    Join Date
    11-26-2018
    Location
    india
    MS-Off Ver
    2007
    Posts
    22

    Translate text from French to English

    Hello, I am looking for a macro which will translate text from French to english. In my excel, text is available from B6 to down and I need translated text from C6 to down. I am using Excel 2016. So can some one help me.
    I was using below macro but recently it is throwing error that "unable to get the webservice property of the worksheet function class".
    So can some one help me to fix this error. Or Help me with another code.

    Function Translate$(sText$, FromLang$, ToLang$)

    Dim p1&, p2&, url$, resp$

    Const DIV_RESULT$ = "<div class=""result-container"">"

    Const URL_TEMPLATE$ = "https://translate.google.com/m?hl=[from]&sl=[from]&tl=[to]&ie=UTF-8&prev=_m&q="

    url = URL_TEMPLATE & WorksheetFunction.EncodeURL(sText)

    url = Replace(url, "[to]", ToLang)

    url = Replace(url, "[from]", FromLang)

    resp = WorksheetFunction.WebService(url)

    p1 = InStr(resp, DIV_RESULT)

    If p1 Then

    p1 = p1 + Len(DIV_RESULT)
    p2 = InStr(p1, resp, "</div>")

    Translate = Mid$(resp, p1, p2 - p1)

    End If

    End Function

    Sub trans_click()
    Dim finrow As Integer
    Dim frstring, engstring As String

    finrow = Sheets("Input").Cells(Rows.Count, 2).End(xlUp).Row
    Sheets("Input").Range("C6").End(xlDown).Clear
    For i = 6 To finrow

    frstring = Sheets("Input").Cells(i, 2)


    engstring = Translate(Sheets("Input").Cells(i, 2), "fr", "en")
    Sheets("Input").Cells(i, 3) = engstring

    Next i

    MsgBox ("Translation completed!!")

    End Sub
    Attached Images Attached Images

  2. #2
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2503
    Posts
    1,485

    Re: Translate text from French to English

    You can give the below function a try as it does not rely on WebService:
    Function GTranslate(strInput As String, strFromLang As String, strToLang As String) As String
        Dim strURL As String, objHTTP As Object, objHTML As Object, objDivs As Object, objDiv As Variant
        
        strInput = WorksheetFunction.EncodeURL(strInput)
        strURL = "https://translate.google.com/m?hl=" & 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
            .Open
            .Write objHTTP.responseText
            .Close
        End With
        Set objDivs = objHTML.getElementsBytagName("DIV")
        GTranslate = objHTML.getElementsByClassName("result-container")(0).innertext
        Set objHTML = Nothing: Set objHTTP = Nothing
    End Function
    Last edited by CheeseSandwich; 10-12-2022 at 04:14 AM.
    If things don't change they stay the same

  3. #3
    Registered User
    Join Date
    11-26-2018
    Location
    india
    MS-Off Ver
    2007
    Posts
    22

    Re: Translate text from French to English

    Thanks for your quick reply. I have used code as below but getting error that "An error occurred in secure channel support".

    Function GTranslate(strInput As String, strFromLang As String, strToLang As String) As String
    Dim strURL As String, objHTTP As Object, objHTML As Object, objDivs As Object, objDiv As Variant

    strInput = WorksheetFunction.EncodeURL(strInput)
    strURL = "https://translate.google.com/m?hl=" & 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
    .Open
    .Write objHTTP.responseText
    .Close
    End With
    Set objDivs = objHTML.getElementsBytagName("DIV")
    GTranslate = objHTML.getElementsByClassName("result-container")(0).innertext
    Set objHTML = Nothing: Set objHTTP = Nothing
    End Function

    Sub trans_click()
    Dim finrow As Integer
    Dim frstring, engstring As String

    finrow = Sheets("Input").Cells(Rows.Count, 2).End(xlUp).Row
    Sheets("Input").Range("C6").End(xlDown).Clear
    For i = 6 To finrow

    frstring = Sheets("Input").Cells(i, 2)


    engstring = GTranslate(Sheets("Input").Cells(i, 2), "fr", "en")
    Sheets("Input").Cells(i, 3) = engstring

    Next i

    MsgBox ("Translation completed!!")

    End Sub

  4. #4
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2503
    Posts
    1,485

    Re: Translate text from French to English

    It could be a change in security settings at your end?

  5. #5
    Registered User
    Join Date
    11-26-2018
    Location
    india
    MS-Off Ver
    2007
    Posts
    22

    Re: Translate text from French to English

    Not sure. But https://translate.google.com ink is accessible through Internet browser. Any other way for translation? While checking I got one excel sheet for translator. But it shows error that ""The code in this project must be updated for use on 64-bit systems. Please review and update declare statement and then mark them with PtrSafe attribute." I have attached that excel sheet. I am using 32-bit system and 2016 excel. Please help such type excel is possible.
    Attached Files Attached Files

  6. #6
    Forum Expert ByteMarks's Avatar
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    3,175

    Re: Translate text from French to English

    This works for me.

     Function googletranslate(strSourceText As String, strSourceLangCode As String, strTargetLangcode As String) As String
        Dim p1&, p2&, url$, resp$
        Const DIV_RESULT$ = "<div class=""result-container"">"
        Const URL_TEMPLATE$ = "https://translate.google.com/m?hl=[from]&sl=[from]&tl=[to]&ie=UTF-8&prev=_m&q="
        url = URL_TEMPLATE & WorksheetFunction.EncodeURL(strSourceText)
        url = Replace(url, "[to]", strTargetLangcode)
        url = Replace(url, "[from]", strSourceLangCode)
        resp = WorksheetFunction.WebService(url)
        p1 = InStr(resp, DIV_RESULT)
        If p1 Then
            p1 = p1 + Len(DIV_RESULT)
            p2 = InStr(p1, resp, "</div>")
            googletranslate = Mid$(resp, p1, p2 - p1)
        End If
    End Function

  7. #7
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Translate text from French to English


    Hello,

    a sample in this thread ...

  8. #8
    Registered User
    Join Date
    11-26-2018
    Location
    india
    MS-Off Ver
    2007
    Posts
    22

    Re: Translate text from French to English

    Hi Marc. Thanks for reply. But how to use for my requirement...like in my case, on sheet 1, French text is in column B and I want to convert those text on same sheet 1 in column C in English.
    Code available in your provided thread is little bit complicated for me to modify as per my requirement since I just know only basic. Please help.

  9. #9
    Registered User
    Join Date
    11-26-2018
    Location
    india
    MS-Off Ver
    2007
    Posts
    22

    Re: Translate text from French to English

    Hello, Can someone help me?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Translate English to French without using Google Translate
    By shaunafink in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-29-2019, 12:08 PM
  2. Replies: 3
    Last Post: 04-29-2016, 04:10 AM
  3. Sample Translate English to Arabic Text Using MS Translate
    By pidyok in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-21-2013, 08:18 AM
  4. [SOLVED] French to english problem
    By YounesB3 in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 07-09-2013, 02:57 PM
  5. [SOLVED] Translate Yes/No message Box to French
    By ahng in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-28-2012, 10:38 AM
  6. English VBA on a French computer
    By BillWilts in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-10-2011, 11:07 AM
  7. formula french to english
    By Sophie in forum Excel General
    Replies: 4
    Last Post: 08-05-2005, 12:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1