VBA使outlook和Word带谷歌翻译功能

本文介绍了一段VBA代码,用于通过Excel调用Google翻译API实现文本翻译功能。该代码能够将选中的文本进行URL编码,并发送HTTP请求获取翻译后的结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


 

 

 

VBA代码如下:

By wgscd

Date:2008/12/26

Blog:http://blog.youkuaiyun.com/wgsnet

  1. Macro1()
  2.    Dim transString As String
  3.    Dim baseUrl As String
  4.    Dim para As String
  5.    Dim content As String
  6.    baseUrl = "http://translate.google.com/translate_a/t?client=t&text="
  7.    para = "&sl=en&tl=zh-CN"
  8.   If Trim(Selection.Text) = "" Then
  9.       Exit Sub
  10.    End If
  11.    
  12.    content = URLEncode(Trim(Selection.Text))
  13.    content = Replace(content, "%D""%0D%0A")
  14.    content = Replace(content, "%A1%B1""%22")
  15.    content = Replace(content, "%A1%B0""%22")
  16.    transString = GetHttpPage(baseUrl & content & para)
  17.    transString = Replace(transString, "/r/n", vbNewLine)
  18.    
  19.    MsgBox transString
  20. End Sub
  21. Function GetHttpPage(HttpUrl)
  22.    If IsNull(HttpUrl) = True Or HttpUrl = "$False$" Then
  23.       GetHttpPage = "$False$"
  24.       Exit Function
  25.    End If
  26.    Dim http
  27.    Set http = CreateObject("MicroSoft.XMLHTTP")
  28.    http.Open "GET", HttpUrl, False
  29.    http.Send
  30.   
  31.    If http.ReadyState <> 4 Then
  32.       Set http = Nothing
  33.       GetHttpPage = "$False$"
  34.       Exit Function
  35.    End If
  36.    
  37.     If http.Status = 404 Then '找不到页面
  38.     
  39.          'MsgBox "404"
  40.            GetHttpPage = "$False$"
  41.         Exit Function
  42.      End If
  43.      
  44.     
  45.    GetHttpPage = BytesToBstr(http.responseBody, "utf-8")
  46.    Set http = Nothing
  47.    If Err.Number <> 0 Then
  48.       Err.Clear
  49.    End If
  50.    
  51.    
  52. End Function
  53. Function BytesToBstr(Body, Cset)
  54.    Dim Objstream
  55.    Set Objstream = CreateObject("adodb.stream")
  56.    Objstream.Type = 1
  57.    Objstream.Mode = 3
  58.    Objstream.Open
  59.    Objstream.Write Body
  60.    Objstream.Position = 0
  61.    Objstream.Type = 2
  62.    Objstream.Charset = Cset
  63.    BytesToBstr = Objstream.ReadText
  64.    Objstream.Close
  65.    Set Objstream = Nothing
  66. End Function
  67. Function GetBody(ConStr, StartStr, OverStr, IncluL, IncluR)
  68.    If ConStr = "$False$" Or ConStr = "" Or IsNull(ConStr) = True Or StartStr = "" Or IsNull(StartStr) = True Or 
  69. OverStr = "" Or IsNull(OverStr) = True Then
  70.       GetBody = "$False$"
  71.       Exit Function
  72.    End If
  73.    Dim ConStrTemp
  74.    Dim Start, Over
  75.    ConStrTemp = LCase(ConStr)
  76.    StartStr = LCase(StartStr)
  77.    OverStr = LCase(OverStr)
  78.    Start = InStrB(1, ConStrTemp, StartStr, vbBinaryCompare)
  79.    If Start <= 0 Then
  80.       GetBody = "$False$"
  81.       Exit Function
  82.    Else
  83.       If IncluL = False Then
  84.          Start = Start + LenB(StartStr)
  85.       End If
  86.    End If
  87.    Over = InStrB(Start, ConStrTemp, OverStr, vbBinaryCompare)
  88.    If Over <= 0 Or Over <= Start Then
  89.       GetBody = "$False$"
  90.       Exit Function
  91.    Else
  92.       If IncluR = True Then
  93.          Over = Over + LenB(OverStr)
  94.       End If
  95.    End If
  96.    GetBody = MidB(ConStr, Start, Over - Start)
  97. End Function
  98. Function ShowErr(ErrMsg)
  99. response.Write "<script>alert('" & ErrMsg & "');history.back();</script>"
  100. response.End
  101. End Function
  102. ''--------url编码函数-----------------
  103.  Public Function URLEncode(ByRef strURL As StringAs String
  104.   Dim I     As Long
  105.   Dim tempStr     As String
  106.   For I = 1 To Len(strURL)
  107.           If Asc(Mid(strURL, I, 1)) < 0 Then
  108.                 tempStr = "%" & Right(CStr(Hex(Asc(Mid(strURL, I, 1)))), 2)
  109.                 tempStr = "%" & Left(CStr(Hex(Asc(Mid(strURL, I, 1)))), Len(CStr(Hex(Asc(Mid(strURL, I, 1))))) - 2) & 
  110. tempStr
  111.                 URLEncode = URLEncode & tempStr
  112.           ElseIf (Asc(Mid(strURL, I, 1)) >= 65 And Asc(Mid(strURL, I, 1)) <= 90) Or (Asc(Mid(strURL, I, 1)) >= 97 And 
  113. Asc(Mid(strURL, I, 1)) <= 122) Then
  114.                 URLEncode = URLEncode & Mid(strURL, I, 1)
  115.           Else
  116.                 URLEncode = URLEncode & "%" & Hex(Asc(Mid(strURL, I, 1)))
  117.           End If
  118.   Next
  119.   End Function
  120.     
  121. ''---------URL反编码函数-------------------
  122.   Public Function URLDecode(ByRef strURL As StringAs String
  123.   Dim I     As Long
  124.     
  125.   If InStr(strURL, "%") = 0 Then URLDecode = strURL:                   Exit Function
  126.     
  127.   For I = 1 To Len(strURL)
  128.           If Mid(strURL, I, 1) = "%" Then
  129.                 If Val("&H" & Mid(strURL, I + 1, 2)) > 127 Then
  130.                       URLDecode = URLDecode & Chr(Val("&H" & Mid(strURL, I + 1, 2) & Mid(strURL, I + 4, 2)))
  131.                       I = I + 5
  132.                 Else
  133.                       URLDecode = URLDecode & Chr(Val("&H" & Mid(strURL, I + 1, 2)))
  134.                       I = I + 2
  135.                 End If
  136.           Else
  137.                 URLDecode = URLDecode & Mid(strURL, I, 1)
  138.           End If
  139.   Next
  140.   End Function

   

然后是自定义工具栏和自定义按钮,将宏关联到按钮上,不会自定义的自己google下,^_^…………。

 

当然改动参数 para = "&sl=en&tl=zh-CN" 可设置其他语言之间翻译。

 

希望对大家有用。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值