如何在 Excel VBA 中对字符串进行 URL 编码?
是否有内置方法可以在 Excel VBA 中对字符串进行 URL 编码,或者我是否需要手动执行此功能?
Is there a built-in way to URL encode a string in Excel VBA or do I need to hand roll this functionality?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
不,没有任何内置内容(直到 Excel 2013 - 查看此答案)。
此答案中有
URLEncode()
的三个版本。一个支持 UTF-8 编码并基于
ADODB.Stream
的变体(包括对最新版本的引用)项目中的“Microsoft ActiveX Data Objects”库):此函数在 freevbcode.com 上找到:
我已经纠正了其中的一个小错误。
我会使用上面的更高效(大约2倍快)的版本:
请注意,这两个函数都不支持UTF -8编码。
No, nothing built-in (until Excel 2013 - see this answer).
There are three versions of
URLEncode()
in this answer.A variant that supports UTF-8 encoding and is based on
ADODB.Stream
(include a reference to a recent version of the "Microsoft ActiveX Data Objects" library in your project):This function was found on freevbcode.com:
I've corrected a little bug that was in there.
I would use more efficient (~2× as fast) version of the above:
Note that neither of these two functions support UTF-8 encoding.
为了使这一点保持最新,自 Excel 2013 起,现在有一种使用工作表函数
ENCODEURL
对 URL 进行编码的内置方法。要在 VBA 代码中使用它,您只需调用
文档
For the sake of bringing this up to date, since Excel 2013 there is now a built-in way of encoding URLs using the worksheet function
ENCODEURL
.To use it in your VBA code you just need to call
Documentation
以上支持 UTF8 的版本:
尽情享受吧!
Version of the above supporting UTF8:
Enjoy!
虽然,这个已经很老了。 我提出了一个基于 this 答案的解决方案:
添加 Microsoft Script Control 作为参考,您就完成了。
顺便说明一下,由于 JS 部分,它完全兼容 UTF-8。 VB 将正确地从 UTF-16 转换为 UTF-8。
Although, this one is very old. I have come up with a solution based in this answer:
Add Microsoft Script Control as reference and you are done.
Just a side note, because of the JS part, this is fully UTF-8-compatible. VB will convert correctly from UTF-16 to UTF-8.
与Michael-O的代码类似,只是不需要引用(后期绑定)并且少了一行。
* 我读到,在 excel 2013 中可以更轻松地完成,如下所示:
WorksheetFunction.EncodeUrl(InputString)
Similar to Michael-O's code, only without need to reference (late bind) and with less one line .
* I read, that in excel 2013 it can be done more easily like so:
WorksheetFunction.EncodeUrl(InputString)
自 Office 2013 起,请在此处使用此内置函数。
如果在 Office 2013 之前
添加 Microsoft Script Control 作为参考,您就完成了。
与上一篇文章相同,只是完成功能..有效!
Since office 2013 use this inbuilt function here.
If before office 2013
Add Microsoft Script Control as reference and you are done.
Same as last post just complete function ..works!
通过
htmlfile
ActiveX 的另一个解决方案:将
htmlfile
DOM 文档对象声明为静态变量,在第一次调用时由于 init 只给出很小的延迟,并且使该函数对于许多人来说非常快调用,例如对我来说,它在大约 2 秒内将 100 个字符长度的字符串转换 100000 次。One more solution via
htmlfile
ActiveX:Declaring
htmlfile
DOM document object as static variable gives the only small delay when called first time due to init, and makes this function very fast for numerous calls, e. g. for me it converts the string of 100 chars length 100000 times in 2 seconds approx..(撞上旧线程)。 只是为了好玩,这里有一个使用指针来组装结果字符串的版本。 它的速度大约是已接受答案中更快的第二个版本的 2 倍 - 4 倍。
(Bump on an old thread). Just for kicks, here's a version that uses pointers to assemble the result string. It's about 2x - 4x as fast as the faster second version in the accepted answer.
与支持 UTF-8 的
WorksheetFunction.EncodeUrl
相同:Same as
WorksheetFunction.EncodeUrl
with UTF-8 support:接受的答案的代码在 Access 2013 中因 Unicode 错误而停止,因此我为自己编写了一个具有高可读性的函数,该函数应遵循 RFC 3986 根据 Davis Peixoto,并在各种环境中造成最小的麻烦。
注意:必须首先替换百分号本身,否则它将对任何先前编码的字符进行双重编码。 添加用 + 替换空格,不是为了符合 RFC 3986,而是为了提供不会因格式而中断的链接。 它是可选的。
The accepted answer's code stopped on a Unicode error in Access 2013, so I wrote a function for myself with high readability that should follow RFC 3986 according to Davis Peixoto, and cause minimal trouble in various environments.
Note: The percent sign itself must be replaced first, or it will double-encode any previously encoded characters. Replacing space with + was added, not to conform with RFC 3986, but to provide links that don't break due to formatting. It is optional.
如果您还希望它在 MacO 上运行,请创建一个单独的函数
If you also want it to work on MacOs create a seperate function
我在将西里尔字母编码为 URF-8 时遇到问题。
我修改了上述脚本之一以匹配西里尔字符映射。
的西里尔语部分
实现的是https://en.wikipedia.org/wiki/UTF-8
和
http://www.utf8-chartable.de/unicode-utf8 -table.pl?start=1024
其他部分开发是示例,需要用真实数据进行验证并计算字符映射偏移量
以下是脚本:
祝你好运!
I had problem with encoding cyrillic letters to URF-8.
I modified one of the above scripts to match cyrillic char map.
Implmented is the cyrrilic section of
https://en.wikipedia.org/wiki/UTF-8
and
http://www.utf8-chartable.de/unicode-utf8-table.pl?start=1024
Other sections development is sample and need verification with real data and calculate the char map offsets
Here is the script:
Good luck!
我在我的应用程序中使用了此代码片段来对 URL 进行编码,因此这可能可以帮助您执行相同的操作。
This snippet i have used in my application to encode the URL so may this can help you to do the same.
这里没有一个解决方案对我来说是开箱即用的,但这很可能是由于我缺乏 VBA 经验。 也可能是因为我只是复制并粘贴了上面的一些函数,而不知道使它们在应用程序环境的 VBA 上工作可能必需的细节。
我的需求只是使用包含挪威语言的一些特殊字符的 URL 发送 xmlhttp 请求。 上面的一些解决方案甚至对冒号进行编码,这使得网址不适合我的需要。
然后我决定编写自己的 URLEncode 函数。 它没有使用更聪明的编程,例如 @ndd 和 @Tom 的编程。 我不是一个非常有经验的程序员,但我必须尽快完成这件事。
我意识到问题是我的服务器不接受 UTF-16 编码,因此我必须编写一个将 UTF-16 转换为 UTF-8 的函数。 此处 和此处。
我还没有对它进行广泛的测试来检查它是否适用于具有较高 unicode 值的字符的 url,并且会产生超过 2 个字节的 utf-8 字符。 我并不是说它会解码需要解码的所有内容(但很容易修改以在
select case
语句中包含/排除字符),也不是说它可以处理更高的字符,因为我没有没有完全测试。 但我分享代码是因为它可能会帮助那些试图理解这个问题的人。欢迎任何评论。
None of the solutions here worked for me out of the box, but it was most likely due my lack of experience with VBA. It might also be because I simply copied and pasted some of the functions above, not knowing details that maybe are necessary to make them work on a VBA for applications environment.
My needs were simply to send xmlhttp requests using urls that contained some special characters of the Norwegian language. Some of the solutions above encode even colons, which made the urls unsuitable for what I needed.
I then decided to write my own URLEncode function. It does not use more clever programming such as the one from @ndd and @Tom. I am not a very experienced programmer, but I had to make this done sooner.
I realized that the problem was that my server didn't accept UTF-16 encodings, so I had to write a function that would convert UTF-16 to UTF-8. A good source of information was found here and here.
I haven't tested it extensively to check if it works with url with characters that have higher unicode values and which would produce more than 2 bytes of utf-8 characters. I am not saying it will decode everything that needs to be decoded (but it is easy to modify to include/exclude characters on the
select case
statement) nor that it will work with higher characters, as I haven't fully tested. But I am sharing the code because it might help someone who is trying to understand the issue.Any comments are welcome.
VBA-tools 库具有以下功能:
http:// /vba-tools.github.io/VBA-Web/docs/#/WebHelpers/UrlEncode
它的工作方式似乎与 JavaScript 中的
encodeURIComponent()
类似。The VBA-tools library has a function for that:
http://vba-tools.github.io/VBA-Web/docs/#/WebHelpers/UrlEncode
It seems to work similar to
encodeURIComponent()
in JavaScript.两全其美的。 如果工作簿在 Excel 2013 或更高版本中打开,此函数将使用新的工作表函数
ENCODEURL()
。如果它是旧版本的 Excel,则此函数将使用
htmlfile
代替。您还可以通过传递
True
作为可选的bForceOldSchool
参数来强制此函数使用htmlfile
。The best of both worlds. This function uses the new(ish) worksheet function
ENCODEURL()
if the workbook is open in Excel 2013 or newer.If it's an older version of Excel then this function uses
htmlfile
instead.You can also force this function to use
htmlfile
by passingTrue
as the optionalbForceOldSchool
argument.