Excel将希伯来语字符解析为谷歌的乱码

时间:2015-01-04 06:59:56

标签: excel vba excel-vba

我使用VB将Excel电子表格中的值解析为Google搜索。

ActiveWorkbook.FollowHyperlink Address:="http://www.google.com./search?hl=en&q=" + Sheet1.name1, NewWindow:=True

当我试图解析希伯来字母时,我得到了胡言乱语。无法找到原因...

1 个答案:

答案 0 :(得分:2)

这是一个编码问题,您可以更改编码,这有点复杂,因为excel默认为您的系统编码。

我认为在您的情况下最简单的方法是URL Encode您的查询。

在Excel 2013之前,可以使用此代码段来完成此操作,该片段取自here

Public Function URLEncode( _
   StringVal As String, _
   Optional SpaceAsPlus As Boolean = False _
) As String

  Dim StringLen As Long: StringLen = Len(StringVal)

  If StringLen > 0 Then
    ReDim result(StringLen) As String
    Dim i As Long, CharCode As Integer
    Dim Char As String, Space As String

    If SpaceAsPlus Then Space = "+" Else Space = "%20"

    For i = 1 To StringLen
      Char = Mid$(StringVal, i, 1)
      CharCode = Asc(Char)
      Select Case CharCode
        Case 97 To 122, 65 To 90, 48 To 57, 45, 46, 95, 126
          result(i) = Char
        Case 32
          result(i) = Space
        Case 0 To 15
          result(i) = "%0" & Hex(CharCode)
        Case Else
          result(i) = "%" & Hex(CharCode)
      End Select
    Next i
    URLEncode = Join(result, "")
  End If
End Function

在Excel 2013中,您可以使用EncodeUrl功能

ActiveWorkbook.FollowHyperlink Address:="http://www.google.com./search?hl=en&q=" + WorksheetFunction.EncodeUrl(Sheet1.name1), NewWindow:=True