How to pass a string containing quotes into eval'ed code

drgs

Board Regular
Joined
Apr 23, 2011
Messages
58
Hello,

I'm trying to use the Escape function from VBScript to encode some URL addresses (not available in VBA). Looks like this:

Code:
Function Escape(strString As String) As String
    Dim objVBScript As Object
    Dim str As String
    
    Set objVBScript = CreateObject("MSScriptControl.ScriptControl")
    objVBScript.Language = "VBScript"
    objVBScript.Reset
    Escape = objVBScript.Eval("escape(""" & strString & """)")
End Function

Works fine unless strString is something like
strString = """hehe"

I've tried replacing single " with "", but it keeps generating syntax errors.
What can I do in this case?
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Perhaps something like

Code:
Escape = objVBScript.Eval("escape(""" & Replace(strString, """", vbNullString) & """)")
 
Upvote 0
Yes, deleting all quotes will help:) but I want to preserve the contents

The string """hehe" should be "%22hehe" when encoded
 
Upvote 0
This works for me:

Code:
Sub tests()
    
    'test 1
    MsgBox Escape(Chr(34) & "wigi" & Chr(34)) 'or: MsgBox Escape("""wigi""")
    
    'test 2
    MsgBox Escape("wigi")

End Sub

Function Escape(strString As String) As String
    
    Const s = """"    
    With CreateObject("MSScriptControl.ScriptControl")
        .Language = "VBScript"
        .Reset
        Escape = .Eval("escape(" & s & IIf(Left(strString, 1) = Chr(34), s, "") & strString & s & IIf(Right(strString, 1) = Chr(34), s, "") & ")")
    End With
    
End Function

Wigi
 
Last edited:
Upvote 0
Actually false alarm, my first method seems to work after all
Thanks

Can you explain this? Your code below generates an error...

Code:
Sub tests()
    
    'this does not work
    MsgBox Escape("""hehe")

End Sub

Function Escape(strString As String) As String
    Dim objVBScript As Object
    Dim str As String
    
    Set objVBScript = CreateObject("MSScriptControl.ScriptControl")
    objVBScript.Language = "VBScript"
    objVBScript.Reset
    Escape = objVBScript.Eval("escape(""" & strString & """)")
End Function

Wigi
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,789
Members
452,942
Latest member
VijayNewtoExcel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top