How can I URL encode a string in Excel VBA?

后端 未结 15 2210
闹比i
闹比i 2020-11-22 11:39

Is there a built-in way to URL encode a string in Excel VBA or do I need to hand roll this functionality?

15条回答
  •  温柔的废话
    2020-11-22 12:00

    Although, this one is very old. I have come up with a solution based in this answer:

    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", "€ömE.sdfds")
    

    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.

提交回复
热议问题