|
老师好,我想把它引入word 模板中,红字的地方作修改(不修改显然不能运行)
却出现乱码:
Public Function EnToCh(rng As String)
Dim xml
Dim url$, EngSentence$
Set xml = CreateObject("MSXML2.XMLHTTP")
'EngSentence = URLEncodePlus(rng.Text)
'url = "https://translate.google.cn/m?hl=en&sl=enN&tl=zh-CN&ie=UTF-8&prev=_m&q=" & EngSentence
'Debug.Print Asc(rng)
aasc = Asc(rng)
If aasc > 64 And aasc < 123 Then
url = "https://translate.google.cn/m?hl=en&sl=enN&tl=zh-CN&ie=UTF-8&prev=_m&q=" & rng
Else
url = "https://translate.google.cn/m?hl=en&sl=zh-CN&tl=enN&ie=UTF-8&prev=_m&q=" & GetURL(rng)
End If
With xml
.Open "GET", url, False
.send
If InStr(.responseText, "<div dir=""ltr"" class=""t0"">") > 0 Then
EnToCh = Split(Split(.responseText, "<div dir=""ltr"" class=""t0"">")(1), "</div><")(0)
End If
End With
End Function
Function GetURL$(txt$)
'Dim a() As Byte
'a = StrConv(txt, vbFromUnicode, &H804)
For i = 1 To Len(txt)
txt1 = Mid(txt, i, 1)
If Abs(Asc(txt1)) < 128 Then
GetURL = GetURL & txt1
Else
With Excel.Application.WorksheetFunction
GetURL1 = .Hex2Bin(Left(Hex(AscW(txt1)), 2), 8)
GetURL1 = GetURL1 & .Hex2Bin(Right(Hex(AscW(txt1)), 2), 8)
GetURL1 = VBA.Replace(VBA.Replace(GetURL1, 11, 10), 5, 10)
'因为不能运行,
GetURL2 = "%E" & .Bin2Hex(Left(GetURL1, 4))
GetURL2 = GetURL2 & "%" & .Bin2Hex(Mid$(GetURL1, 5, 8))
GetURL2 = GetURL2 & "%" & .Bin2Hex(Mid$(GetURL1, 13, 8))
GetURL = GetURL & GetURL2
End With
End If
Next
End Function
'如果一律转码是: GetURL = GetURL & "%" & Right("0" & Hex(a(i)), 2)
Sub 测试()
MsgBox EnToCh("中国")
End Sub
你看看是不是出现乱码,不知是什么原因。
请指点,谢谢你。
|
|