Excel 发送到 Google 时无法识别带有特殊字母的单元格值 通过 VBA 搜索
Excel cells values with special letters not recognized when sent to Google Search via VBA
我有一个 Excel 文件,其中包含我发送给 Google 搜索的用特殊字符(é、î、ù 等)书写的单词。
为此,我使用 VBA 并且在使用 MsgBox 时,似乎 VBA 可以毫无问题地识别它们。但是当这些词出现在 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
为避免这种情况,您可以按标准 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
请尝试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
我有一个 Excel 文件,其中包含我发送给 Google 搜索的用特殊字符(é、î、ù 等)书写的单词。 为此,我使用 VBA 并且在使用 MsgBox 时,似乎 VBA 可以毫无问题地识别它们。但是当这些词出现在 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
为避免这种情况,您可以按标准 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
请尝试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