Results 1 to 1 of 1

Google Translate VBA Function

Threaded View

  1. #1
    Registered User
    Join Date
    10-06-2018
    Location
    United States
    MS-Off Ver
    Office 365
    Posts
    1

    Google Translate VBA Function

    I am working on creating an Excel UDF that translates a specified language to another specified language. I previously used a few various sections of code I leveraged from various websites and my limited knowledge of VBA to call an unpaid Google translate API that worked well, but Google limits the amount of results it will provide when using an unpaid API. I created a Google account, set up the API, and received an API key to use, but I am having trouble with the syntax needed to implement the key.
    There is some additional code to make this work, but I have included the critical sections (and where I believe my problem originates) below.

    Note: I left off the first part of the link because I have not posted on this forum previously, and as such am not permitted to post links. I have replaced the first part of each link with "URL stuff" so that the important part of the URL can be viewed without actually linking a web page.

    Code that uses unpaid API and works on a very limited basis:
    Public Function TRANSLATE(ByVal strText As String, _
                              Optional ByVal eFrom As eLanguage = auto_detect, _
                              Optional ByVal eTo As eLanguage = english, _
                              Optional ByVal blnPhonetic As Boolean = False) As String
        
        Dim strUrl
        Dim strTransText As String
        Dim strResult As String
        Dim varSplitText As Variant
        Dim lngItem As Long
        
        If strText = "" Then
            TRANSLATE = ""
            Exit Function
        End If
        
        strText = URLEncode(strText)
        
        strUrl = "URL Stuff + translate.googleapis.com/translate_a/single?client=gtx&sl={F}&tl={T}&dt=t&q={S}"
        
        strUrl = Replace$(strUrl, "{S}", strText)
        strUrl = Replace$(strUrl, "{F}", Split(strSHORTCODES, ",")(eFrom))
        strUrl = Replace$(strUrl, "{T}", Split(strSHORTCODES, ",")(eTo))
    
        With CreateObject("MSXML2.XMLHTTP")
            Call .Open("get", strUrl, False)
            Call .Send
            strResult = .responseText
        End With
    
        varSplitText = Split(Split(strResult, "]],")(0), "[")
    
        If Not blnPhonetic Then
            For lngItem = 3 To UBound(varSplitText)
                strTransText = strTransText & Split(varSplitText(lngItem), ",")(0)
            Next
        Else
            For lngItem = 3 To UBound(varSplitText)
                strTransText = strTransText & Split(varSplitText(lngItem), ",")(2)
            Next
        End If
        strResult = Replace(strTransText, """", "")
    
    
        TRANSLATE = strResult
    End Function
    Code attempting to include the API key :
    Public Function TRANSLATE(ByVal strText As String, _
                              Optional ByVal eFrom As eLanguage = auto_detect, _
                              Optional ByVal eTo As eLanguage = english, _
                              Optional ByVal blnPhonetic As Boolean = False) As String
        
        Dim strUrl
        Dim apiKey As String
        Dim strTransText As String
        Dim strResult As String
        Dim varSplitText As Variant
        Dim lngItem As Long
        
        If strText = "" Then
            TRANSLATE = ""
            Exit Function
        End If
        
        strText = URLEncode(strText)
        
        apiKey = URLEncode("#################_###################")
        
        strUrl = "URL Stuff + translation.googleapis.com/language/translate/v2?q={S}&tl={T}&sl={F}&key={K}"
        
        strUrl = Replace$(strUrl, "{S}", strText)
        strUrl = Replace$(strUrl, "{F}", Split(strSHORTCODES, ",")(eFrom))
        strUrl = Replace$(strUrl, "{T}", Split(strSHORTCODES, ",")(eTo))
        strUrl = Replace$(strUrl, "{K}", apiKey)
        
        With CreateObject("MSXML2.XMLHTTP")
            Call .Open("get", strUrl, False)
            Call .Send
            strResult = .responseText
        End With
    
        varSplitText = Split(Split(strResult, "]],")(0), "[")
    
        If Not blnPhonetic Then
            For lngItem = 3 To UBound(varSplitText)
                strTransText = strTransText & Split(varSplitText(lngItem), ",")(0)
            Next
        Else
            For lngItem = 3 To UBound(varSplitText)
                strTransText = strTransText & Split(varSplitText(lngItem), ",")(2)
            Next
        End If
        strResult = Replace(strTransText, """", "")
    
        TRANSLATE = strResult
    End Function
    I can post additional parts of the code if that would be helpful, but this section seemed to be the most pertinent. Thanks in advance for any help.
    Last edited by beardguy; 10-06-2018 at 06:54 PM. Reason: Typo

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Get google translate result from vba?
    By JasperD in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 01-29-2021, 07:36 PM
  2. Macro: translate text from cell using google translate
    By Marc_excel_tips in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 01-05-2021, 06:18 AM
  3. Upload excel sheet to google translate (and pull the result) via VBA
    By JasperD in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-08-2017, 04:02 PM
  4. Save text as word document and pull that through google translate
    By JasperD in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-06-2016, 03:05 AM
  5. [SOLVED] Get Google translate Function isn't working anymore
    By Ravana in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-16-2015, 01:39 PM
  6. How to catch errors from Google Translate
    By MrsMac in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-29-2014, 09:18 AM
  7. need Google Translate in Excel 2003
    By appu_gusai in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-03-2013, 08:27 AM

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