By: Shamima Sultana

In reply to NILANJAN.

Hello Nilanjan,

Excel for Mac doesn’t support the WorksheetFunction.WebService method due to differences in how Excel for Mac interacts with web services compared to Excel for Windows. In Mac, you need to directly access Google Translate’s API using HTTP requests or using a different method provided by Google Translate.

Function TranslateText(text_str As String, src_lang As String, trgt_lang As String) As String
Dim url As String
Dim xmlhttp As Object
Dim response As String
Dim json As Object

' Construct the URL for Google Translate API
url = "https://translation.googleapis.com/language/translate/v2?key=YOUR_API_KEY&q=" & _
VBA.URLEncode(text_str) & "&source=" & src_lang & "&target=" & trgt_lang

' Send HTTP request to Google Translate API
Set xmlhttp = CreateObject("MSXML2.XMLHTTP")
xmlhttp.Open "GET", url, False
xmlhttp.send

' Parse the JSON response
response = xmlhttp.responseText
Set json = JsonConverter.ParseJson(response)

' Extract and return the translated text
TranslateText = json("data")("translations")(1)("translatedText")
End Function

Replace “YOUR_API_KEY” with your actual Google Cloud Platform API key.
Remember to enable the “Microsoft XML, v6.0” reference in the VBA editor (under Tools > References) to use CreateObject(“MSXML2.XMLHTTP”) for making HTTP requests.

Regards
ExcelDemy