Excel Translate from English Language to Others via VBA Code

Tarek_CTG

Board Regular
Joined
Apr 27, 2015
Messages
160
Office Version
  1. 2016
Platform
  1. Windows
Dear VBA Expert,
I want to translate some lines from English Language to other languages. Sourcing help from others, I have tried to build up following codes to create a function named GoogleTranslate, but it's not working. When I try to debug the code, nothing error is showing. The code is as follows:

Function GoogleTranslate(strSourceText As String, strSourceLangCode As String, strTargetLangCode As String) As String

'1st - creating a dynamic url

Dim strURL As String
strURL = "Google Translate" & strSourceLangCode & "&tl=" & strTargetLangCode & "&hl=en&ie=UTF-8&q=" & strSourceText

'2nd - do a web server API using XML HTTP Request

Dim XMLHTTP As Object
Set XMLHTTP = CreateObject("MSXML2.ServerXMLHTTP")
XMLHTTP.Open "GET", strURL, False
XMLHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 10.0)"
XMLHTTP.send ""

'3rd - Create HTML Using HTTP Request response text

Dim ObjHTML As Object
Set ObjHTML = CreateObject("HTMLFile")
With ObjHTML
.Open
.write XMLHTTP.responseText
.Close
End With

'4th - Read the translated from HTML File Using HTML Web elements
'Microsoft HTML object Library

Dim HTMLDoc As HTMLDocument
Set HTMLDoc = ObjHTML

Dim ObjClass As Object
Set ObjClass = HTMLDoc.getElementsByClassName("result-container")(0)
If Not ObjClass Is Nothing Then
GoogleTranslate = ObjClass.innerText
End If

'releasing the memory
Set ObjClass = Nothing
Set ObjHTML = Nothing
Set XMLHTTP = Nothing

End Function

Can anyone please solve the issue? Thanks in advance.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I want to translate some lines from English Language to other languages.
Have you tried this code?


That links to the latest code and there are improvements suggested by other posters after it.
 
Upvote 0
Have you tried this code?


That links to the latest code and there are improvements suggested by other posters after it.
yes, I tried, what I need is a fixed google translate function in excel, like the function of sum, average etc. So that i can use the function wherever I need in a workbook/sheet.
 
Upvote 0
The GoogleTranslate function in that thread is a UDF so you can use it in a cell formula exactly like a normal Excel function. For example:

Excel Formula:
=GoogleTranslate(A1,"en","es")
translates the text in A1 from English to Spanish.

The language codes are listed at Language support | Cloud Translation | Google Cloud

The code also includes the RegisterGoogleTranslateFunction macro which, if run, adds help for the function arguments to the Excel UI:

1649968902892.png
 
Upvote 0

Forum statistics

Threads
1,223,883
Messages
6,175,167
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top