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.
Bookmarks