VBA Function to Revert URL Encoded String to Regular String

Posted on March 29, 2011

0


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))
   Next

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

End Function
Advertisements