Function TranslationContent(Wat As String) As String On Error Resume Next Dim Http As Object Dim MyScript As Object, R As Object Dim Reg As Object Set Reg = CreateObject("Scripting.Regexp") Set Http = CreateObject("Microsoft.XMLHTTP") Set MyScript = CreateObject("MSScriptControl.ScriptControl") Reg.Pattern = "[\u4e00-\u9fa5]" Http.Open "POST", "https://www.translate.com/translator/ajax_translate", False Http.setRequestHeader "Content-Type", " application/x-www-form-urlencoded; charset=UTF-8" Wat = WorksheetFunction.Asc(Wat) Wat = WorksheetFunction.Trim(Wat) If Reg.Test(Wat) Then Http.send "text_to_translate=" & Wat & "&source_lang=" & "zh" & "&translated_lang=" & "en" & "&use_cache_only=false" Else Http.send "text_to_translate=" & Wat & "&source_lang=" & "en" & "&translated_lang=" & "zh" & "&use_cache_only=false" End If MyScript.Language = "JScript" Set R = MyScript.Eval("(" + Http.responseText + ")") TranslationContent = R.translated_text On Error GoTo 0 Set Http = Nothing End Function
标签:lang,VBA,Set,Http,互译,中英文,Object,Wat,Reg From: https://www.cnblogs.com/sonycat/p/sonycat_TranslationContent.html