Skip to content

Instantly share code, notes, and snippets.

@greenkey
Created April 29, 2015 08:31
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greenkey/dbb317fbf229d4f85e0f to your computer and use it in GitHub Desktop.
Save greenkey/dbb317fbf229d4f85e0f to your computer and use it in GitHub Desktop.
vbscript: URLEncode
' Encode special characters of a string
' this is useful when you want to put a string in the URL
' inspired by http://stackoverflow.com/questions/218181/how-can-i-url-encode-a-string-in-excel-vba
Public Function URLEncode( StringVal )
Dim i, CharCode, Char, Space
Dim StringLen
StringLen = Len(StringVal)
ReDim result(StringLen)
Space = "+"
'Space = "%20"
For i = 1 To StringLen
Char = Mid(StringVal, i, 1)
CharCode = AscW(Char)
If 97 <= CharCode And CharCode <= 122 _
Or 64 <= CharCode And CharCode <= 90 _
Or 48 <= CharCode And CharCode <= 57 _
Or 45 = CharCode _
Or 46 = CharCode _
Or 95 = CharCode _
Or 126 = CharCode Then
result(i) = Char
ElseIf 32 = CharCode Then
result(i) = Space
Else
result(i) = "&#" & CharCode & ";"
End If
Next
URLEncode = Join(result, "")
End Function
@greenkey
Copy link
Author

I needed this function to make correct URL when calling WebServices in a TestComplete project.

@Ekus
Copy link

Ekus commented Dec 6, 2023

I tried this in an SSRS report but it didn't work, I ended up using this built in function instead:
System.Uri.EscapeDataString(input)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment