VBA Function to Revert URL Encoded String to Regular String

URLEncode function puts legal string into the navigator url. I would use the following function to revert the URLEncoded/Escaped string to human-readable text, used in parsing web document values for example.

'revert urlencoded characters to regular texts
Function UnEncodeURL(EscapedURL As String) As String
Dim i As Integer
Dim txt As String
Dim hexChr As String

   txt = EscapedURL

   'replace '+' with space
   txt = Replace(txt, "+", " ")

   For i = 1 To 255
      Select Case i
         Case 1 To 15
            hexChr = "%0" & hex(i)
         Case 37
            'skip '%' character
         Case Else
            hexChr = "%" & hex(i)
      End Select

      txt = Replace(txt, UCase(hexChr), Chr(i))
      txt = Replace(txt, LCase(hexChr), Chr(i))

   'replace '%' character
   txt = Replace(txt, "%25", "%")
   UnEncodeURL = txt

End Function

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s