您可以像这样在 VBa 中创建 javascript 函数。
** 复制自:How can I URL encode a string in Excel VBA?
** 解决方案:ozmike
** 添加 Microsoft Script Control 作为参考。
Function encodeURL(str As String)
Dim ScriptEngine As ScriptControl
Set ScriptEngine = New ScriptControl
ScriptEngine.Language = "JScript"
ScriptEngine.AddCode "function encode(str) {return encodeURIComponent(str);}"
Dim encoded As String
encoded = ScriptEngine.Run("encode", str)
encodeURL = encoded
End Function
编辑:17.04.15
这是与上述类似的解决方案,只是它使用了一个 Web 浏览器控件,该控件运行 javascript 并为您提供结果。
基本思路是这样的:
1) 我们将本地 html 文件上传到 webbrowser 控件。 html 文件中包含 javascript 函数。
html文件也有两个元素:
<p id='data'>_DATA_</p>
<p id='result'></p>
第一个将保存您想要编码(或采取行动)的数据。
DATA 是一个占位符。使用 vba 代码,我们将替换那个刺
用你想要的数据。
之后,html文件中的javascript会读取第一个元素中写入的内容,并将结果输出到第二个元素。
发生这种情况后,使用 VBA 代码将从第二个元素读取结果。
说明:
1) 创建一个名为 c:\temp.html 的空文本文件并将以下 html 代码粘贴到其中:
<!-- saved from url=(0014)about:internet -->
<!DOCTYPE html>
<html>
<head>
<script>
function encode1()
{
var sData;
// put the "input" to a variable.
sData = document.getElementById('data').innerHTML;
// do stuff with the data variable, for example
sData = encodeURIComponent(sData);
// write the manipulated data to the "output"
document.getElementById('result').innerHTML = sData;
}
</script>
</head>
<body onload='encode1();'>
<p id='data'>_DATA_</p>
<p id='result'></p>
</body>
</html>
在 vba 编辑器中:
2) 插入用户表单
3) 向表单添加一个 WebBrowser 控件。
在用户表单中添加以下代码:
Option Explicit
Public Sub MyEncode(str As String)
Const TEMPLATE_HTML As String = "c:\temp.html"
Const OUTPUT_HTML As String = "c:\tempWithData.html"
Dim s As String
s = ReadAllFile(TEMPLATE_HTML)
s = Replace(s, "_DATA_", str)
Call PrintToFile(OUTPUT_HTML, s)
' Load the HTML file to the WebBrowser control
UserForm1.WebBrowser1.Navigate OUTPUT_HTML
' wait for it to finish
While UserForm1.WebBrowser1.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
' wait some more
While UserForm1.WebBrowser1.Busy
DoEvents
Wend
' find the html element that holds the result data and put the value
' to the global variable.
g_result = (WebBrowser1.Document.getElementById("result").innerHTML)
' unload the form
Unload Me
End Sub
最后,添加一个模块并将以下代码放入其中:
Option Explicit
' this global variable will hold the returning result from the html file
Public g_result As String
' !!!! RUN THIS PROCEDURE !!!
' This should work fast enough so that you will not see the
' form opening and closing.
Public Sub TestEncode()
Dim sResult As String
Load UserForm1
' This runs the web browser control
Call UserForm1.MyEncode("https://stackoverflow.com/")
' The result will be: http%3A%2F%2Fstackoverflow.com%2F
MsgBox g_result
' Last remark: you can of course run the above using data from
' the sheet, for example:
' Call UserForm1.MyEncode(Range("A1").Text)
End Sub
' a helping method- read contents from a file.
Public Function ReadAllFile(fname As String) As String
Dim sFileData As String
Dim FileLength As Long
Dim iFree As Integer
iFree = FreeFile
Open fname For Input As iFree ' Open file for input.
sFileData = Input(LOF(iFree), iFree) ' read all the file
Close iFree ' Close file.
ReadAllFile = sFileData
End Function
' a helping method- write contents to a file.
Public Sub PrintToFile(fname As String, sFileData As String)
Dim iFree As Integer
iFree = FreeFile
Open fname For Output As iFree ' Open file for input.
Print #iFree, sFileData
Close iFree ' Close file.
End Sub