URL Encode




Function URLEncode(sRawURL As String) As String


    On Error Goto Catch
   
    Dim iLoop As Integer
    Dim sRtn As String
    Dim sTmp As String
    Dim sValidChars As String
   
    sValidChars = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz:/.?=_-$(){}~&"


    If Len(sRawURL) > 0 Then
        ' Loop through each char

        For iLoop = 1 To Len(sRawURL)
            sTmp = Mid(sRawURL, iLoop, 1)


            If InStr(1, sValidChars, sTmp, vbBinaryCompare) = 0 Then
                ' If not ValidChar, convert to HEX and p
                '     refix with %
                sTmp = "%" & Hex(Asc(sTmp))
                ' If sTmp is %20 then replace with +
                sTmp = Replace(sTmp, "%20", "+")
            End If

            sRtn = sRtn & sTmp
        Next iLoop

        URLEncode = sRtn
    End If

    Finally:
    Exit Function
    Catch:
    URLEncode = ""
    Resume Finally
End Function