Mar 09 2023 12:51 PM
I am trying to convert my vba codes from french to english; there are 11 sheets and each has at least one code. I've just been slowly translating the codes with ChatGPT and Google Translate, but both of those have a limit to how much they can translate at a time.
Is there a quicker way for me to translate the codes?
Mar 10 2023 12:37 AM
Attached is a link and VBA Code with a solution approach
With Google translator service.
The idea of using this automatically with VB/VBA.
Requirements are: Connection to the web and Internet Explorer/Edge.
Here is the procedure:
Code:
Public Function TranslateByGoogle(OrigineText As String, _
LangCodeFrom As String, _
LangCodeTo As String, _
TranslateText As String, _
Optional UniCodeID As Long, _
Optional TimeOutSeconds As Integer = 3, _
Optional ErrSilent As Boolean = False) As Boolean
'Copyright by Jean Pierre Allain
Dim ieOBJ As Object, WaitTime As Date
' Google website
Const WebSite As String = "http://translate.google.com"
' error handling
OnError GoTo ErrHandler
If Len(OrigineText) > 0 And Not LangCodeFrom = LangCodeTo Then
' Create IE object (instance).
Set ieOBJ = CreateObject("InternetExplorer.Application")
'Call up website with parameters
ieOBJ.Navigate WebSite & "/?sl=" & LangCodeFrom & _
"&tl=" & LangCodeTo & "#" & LangCodeTo & "|" & _
LangCodeFrom & "|" & OrigineText
' Set TimeOut
WaitTime = Now + TimeValue("00:00:" & TimeOutSeconds)
On Error Resume Next
do
' Read Google result
TranslateText = ieOBJ.Document.getElementById("result_box").innerText
If Now() >= WaitTime Then Exit Do
Loop While TranslateText = ""
OnError GoTo ErrHandler
' Read result (translation)
If Len(TranslateText) > 0 And Not TranslateText = OrigineText Then
' If necessary, convert the translation into the specified national language
If UniCodeID <> 0 Then
TranslateText = StrConv(TranslateText, vbUnicode, UniCodeID)
End If
TranslateByGoogle = True
End If
End If
ExitProc:
On Error Resume Next
' Destroy objects
ieOBJ.Quit
Set ieOBJ = Nothing
Exit function
ErrHandler:
If Not ErrSilent Then
MsgBox Err.Description, vbCritical, Err.Number
End If
Resume ExitProc
End function
Code
Dim Result As String
' Language codes must be passed as ISO country codes
' Example: German = DE
' English = EN
' French = FR
' Polish = PL
' etc.
If TranslateByGoogle("That was easy!", "DE", "EN", Result) Then
MsgBoxResult
End If
Maybe it will help you
Mar 13 2023 09:48 AM