How can I URL encode a string in Excel VBA?

后端 未结 15 2294
闹比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 11:55

    This snippet i have used in my application to encode the URL so may this can help you to do the same.

    Function URLEncode(ByVal str As String) As String
            Dim intLen As Integer
            Dim x As Integer
            Dim curChar As Long
            Dim newStr As String
            intLen = Len(str)
            newStr = ""
    
            For x = 1 To intLen
                curChar = Asc(Mid$(str, x, 1))
    
                If (curChar < 48 Or curChar > 57) And _
                    (curChar < 65 Or curChar > 90) And _
                    (curChar < 97 Or curChar > 122) Then
                                    newStr = newStr & "%" & Hex(curChar)
                Else
                    newStr = newStr & Chr(curChar)
                End If
            Next x
    
            URLEncode = newStr
        End Function
    

提交回复
热议问题