【问题标题】:Excel cells values with special letters not recognized when sent to Google Search via VBA通过 VBA 发送到 Google 搜索时无法识别带有特殊字母的 Excel 单元格值
【发布时间】:2020-12-12 02:34:10
【问题描述】:

我有一个 Excel 文件,其中的单元格包含用特殊字符(é、î、ù 等)编写的单词,我将其发送到 Google 搜索。 我为此使用 VBA,似乎 VBA 在使用 MsgBox 时可以毫无问题地识别它们。但是当这些词出现在 Google 搜索栏中时,重音字母会变成问号(例如:île 变成 ?le)。

(我的区域设置已经显示,可以读取这些特殊字母。)

这是我的 VBA 代码:

Sub SpecialLetters()
Dim objIe As Object

Set objIe = CreateObject("InternetExplorer.Application")
objIe.Visible = True

objIe.navigate "http://www.google.com/search?hl=en&ie=UTF-8&q=" & Sheets("Sheet1").Range("A1").Value
    
End Sub

【问题讨论】:

    标签: excel vba web-scraping automation


    【解决方案1】:

    为避免这种情况,您可以使用标准 URL 方式对搜索词进行编码。

    如果您有 Excel 2013 或更高版本,则可以使用 WorksheetFunction.EncodeURL 来执行此操作。您的代码将是:

    Sub SpecialLetters()
    Dim objIe As Object
    
    Set objIe = CreateObject("InternetExplorer.Application")
    objIe.Visible = True
    
    objIe.Navigate "http://www.google.com/search?hl=en&ie=UTF-8&q=" & WorksheetFunction.EncodeURL(Sheets("Sheet1").Range("A1").Value)
        
    End Sub
    

    对于 Excel 2010 或更低版本,如果不创建自己的函数就无法做到这一点,但幸运的是,Tomalak 已经在 VBA (see here) 中提供了一个函数来执行此操作,然后您可以简单地使用它:

    Public Function URLEncode( _
       ByVal StringVal As String, _
       Optional SpaceAsPlus As Boolean = False _
    ) As String
      Dim bytes() As Byte, b As Byte, i As Integer, space As String
    
      If SpaceAsPlus Then space = "+" Else space = "%20"
    
      If Len(StringVal) > 0 Then
        With New ADODB.Stream
          .Mode = adModeReadWrite
          .Type = adTypeText
          .Charset = "UTF-8"
          .Open
          .WriteText StringVal
          .Position = 0
          .Type = adTypeBinary
          .Position = 3 ' skip BOM
          bytes = .Read
        End With
    
        ReDim result(UBound(bytes)) As String
    
        For i = UBound(bytes) To 0 Step -1
          b = bytes(i)
          Select Case b
            Case 97 To 122, 65 To 90, 48 To 57, 45, 46, 95, 126
              result(i) = Chr(b)
            Case 32
              result(i) = space
            Case 0 To 15
              result(i) = "%0" & Hex(b)
            Case Else
              result(i) = "%" & Hex(b)
          End Select
        Next i
    
        URLEncode = Join(result, "")
      End If
    End Function
    

    确保您参考了 Microsoft ActiveX 数据对象库以使其正常工作。

    你的代码会变成:

    Sub SpecialLetters()
    Dim objIe As Object
    
    Set objIe = CreateObject("InternetExplorer.Application")
    objIe.Visible = True
    
    objIe.navigate "http://www.google.com/search?hl=en&ie=UTF-8&q=" & URLEncode(Sheets("Sheet1").Range("A1").Value)
        
    End Sub
    

    【讨论】:

    • 完美运行。感谢您清晰而详细的解释。
    【解决方案2】:

    请尝试EncodeURL功能(它适用于2010版本之后的Office安装):

    Sub SpecialLetters()
    Dim objIe As Object
    
    Set objIe = CreateObject("InternetExplorer.Application")
    objIe.Visible = True
    
      objIe.navigate "http://www.google.com/search?hl=en&ie=UTF-8&q=" & _
             WorksheetFunction.EncodeURL(Sheets("Sheet1").Range("A1").Value)
    End Sub
    

    【讨论】:

    • 完美运行!我不知道那个功能。非常感谢。
    • @Mister Propre:很高兴我能帮上忙!但是我们在这里,当有人回答我们的问题时,请勾选代码左侧的复选框,以使其接受答案。这样,其他搜索类似问题的人就会知道该代码有效...... :)
    猜你喜欢
    • 2015-04-04
    • 2022-07-21
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    相关资源
    最近更新 更多