优秀的编程知识分享平台

网站首页 > 技术文章 正文

在word中通过VBA调用百度翻译API在线翻译

nanyue 2025-05-26 17:47:01 技术文章 6 ℃

一天的时间,借助各种AI终于解决了这个问题:在word中通过VBA调用百度翻译API进行在线翻译。给我的word又添加了一项神技。


先上代码:

Sub 宏5()

'

' 宏5 宏

Dim appid As String

Dim key As String

Dim salt As String

Dim sign As String

Dim query As String

Dim fromLang As String

Dim toLang As String

Dim url As String

Dim http As Object

Dim responseText As String

Dim translatedText As String


' 设置API信息

appid = "20240903002139***" ' 您的APPID

key = "BKbLnNjh8Xc_Bps7I***" ' 您的密钥

salt = "1435660288" ' 固定随机数以简化测试

query = Selection.text

fromLang = "en"

toLang = "zh"


' 构建待加密字符串

Dim toEncrypt As String

toEncrypt = appid & query & salt & key

Debug.Print "待加密字符串: " & toEncrypt


' 使用UAPI进行MD5加密

Set http = CreateObject("MSXML2.XMLHTTP")

url = "https://uapis.cn/api/textmd5?text=" & URLEncode(toEncrypt)

http.Open "GET", url, False

http.send

Debug.Print "MD5加密请求已发送"


If http.Status = 200 Then

responseText = http.responseText

Debug.Print "MD5加密API响应: " & responseText

End If


' 清理可能的空格和换行符(可选,取决于响应的确切格式)

responseText = Replace(responseText, vbCrLf, "")

responseText = Replace(responseText, " ", "")


' 查找"text"字段的值

Dim startPos As Long

Dim endPos As Long


startPos = InStr(1, responseText, """text"":""", vbTextCompare)

If startPos > 0 Then

startPos = startPos + Len("""text"":""")

endPos = InStr(startPos, responseText, """", vbTextCompare)

If endPos > startPos Then

sign = Mid(responseText, startPos, endPos - startPos)

Debug.Print "生成的MD5签名: """ & sign & """"

Else

Debug.Print "提取MD5签名时出错:无法找到有效的结束位置"

sign = "错误的签名"

End If

Else

Debug.Print "提取MD5签名时出错:无法找到'text'字段"

sign = "错误的签名"

End If


' 构建请求URL

url = "https://fanyi-api.baidu.com/api/trans/vip/translate?q=" & URLEncode(query) & _

"&from=" & fromLang & "&to=" & toLang & "&appid=" & appid & _

"&salt=" & salt & "&sign=" & sign


Debug.Print "翻译请求URL: " & url


' 发送百度翻译API请求

Set http = CreateObject("MSXML2.XMLHTTP")

http.Open "GET", url, False

http.send

Debug.Print "翻译请求已发送"


If http.Status = 200 Then

responseText = http.responseText

Debug.Print "翻译API响应: " & responseText


' 提取翻译结果

startPos = InStr(1, responseText, """dst"":""", vbTextCompare)

If startPos > 0 Then

startPos = startPos + Len("""dst"":""")

endPos = InStr(startPos, responseText, """", vbTextCompare)

If endPos > startPos Then

translatedText = Mid(responseText, startPos, endPos - startPos)


' 转换 Unicode 转义字符到实际中文字符

translatedText = DecodeUnicode(translatedText)


' 将翻译结果插入到选定文本的后面

Selection.Collapse Direction:=wdCollapseEnd

Selection.TypeText text:=" " & translatedText


Debug.Print "翻译结果: " & translatedText

End If

Else

MsgBox "无法找到翻译结果。"

End If

Else

MsgBox "翻译API请求失败,HTTP状态码:" & http.Status

End If


' 释放对象

Set http = Nothing

End Sub


' URL 编码函数

Function URLEncode(ByVal text As String) As String

Dim i As Integer

Dim result As String

Dim c As String


result = ""


For i = 1 To Len(text)

c = Mid(text, i, 1)

Select Case asc(c)

Case 48 To 57, 65 To 90, 97 To 122

result = result & c

Case Else

result = result & "%" & Right("0" & Hex(asc(c)), 2)

End Select

Next i


URLEncode = result

End Function


' 将 \uXXXX 形式的 Unicode 转义序列转换为实际字符

Function DecodeUnicode(text As String) As String

Dim i As Long

Dim result As String

Dim unicodeChar As String


i = 1

Do While i <= Len(text)

If Mid(text, i, 2) = "\u" Then

' 读取 Unicode 代码

unicodeChar = Mid(text, i + 2, 4)

' 转换为字符并添加到结果

result = result & ChrW("&H" & unicodeChar)

' 跳过 \uXXXX

i = i + 6

Else

' 非 Unicode 转义字符,直接添加

result = result & Mid(text, i, 1)

i = i + 1

End If

Loop


DecodeUnicode = result

End Function

上述代码成功的解决了在word中调用百度翻译API进行在线翻译的问题。虽然代码可能不是那么健壮,但是目前测试下来还是可以使用的。

这个代码是提出需求后让AI给编写的。

首先在百度翻译平台申请了账号,获得了APPID和密钥。然后成功的利用UAPI提供的接口解决了字符串MD5加密的问题。最后进行了百度翻译响应的转化,最后得到了期望的结果。

这个代码基本上是AI给编写的。但是在编写过程中,发现,AI会陷入一种死循环,开始的时候,我使用的是ChatGPT-4o,在解决字符串的MD5加密过程中,第一次陷入了死循环,开始他希望调用系统自带的一些功能,但是我的系统有问题,不行,于是他就改为自己用VBA编写程序计算字符串的MD5哈希值,结果还是不行,然后,就开始在这两两种方式之间进行了来回的切换。后来我想到了利用网上提供的在线功能,找到了一个计算MD5的API接口。然后,在提取API给出的密码时,又出现了死循环,先是希望调用VBA-JSON库,结果不行,然后开始手动解析JSON代码,还是不行。最后,开始换别的AI进行实验,先是换了通义,结果和ChatGPT一样,再换Kimi,也是一样。最后换了文心一言,终于走出了思维误区。

通过这个过程,也发现,不同的AI在解决问题的时候还是容易陷入思维误区里面出不来的。这时候,人一定要有想法,否则,就很难解决问题了。


#VBA##chatGTP人工智能##百度翻译##API##MD5#

#word中调用VBA#

#word#

Tags:

最近发表
标签列表