PDA

View Full Version : "object expected" suddenly popping up



Sebastian H
03-04-2016, 01:56 AM
I dug out an old macro which worked well three years ago, but this time it gets stuck in the ScriptEngine.Run line:

Function encodeHTML(str As String)
' found at http://stackoverflow.com/questions/218181/how-can-i-url-encode-a-string-in-excel-vba; replaced "URL" by "HTML"

Dim ScriptEngine As ScriptControl
Set ScriptEngine = New ScriptControl
ScriptEngine.Language = "JScript"

ScriptEngine.AddCode "function encode(str) {return encodeHTMLComponent(str);}"
Dim encoded As String

encoded = ScriptEngine.Run("encode", str) ' <-- error "object expected"
encodeHTML = encoded
End Function
Where does it expect the object? The obvious assumption is ScriptEngine, but if that weren't an object, the .AddCode method wouldn't work, before.

GTO
03-04-2016, 04:14 AM
I dug out an old macro which worked well three years ago, but this time it gets stuck in the ScriptEngine.Run line: ...

This: ScriptEngine.AddCode "function encode(str) {return encodeHTMLComponent(str);}"
Needs to be: ScriptEngine.AddCode "function encode(str) {return encodeURIComponent(str);}"

Does that fix it?

Mark

Sebastian H
03-04-2016, 07:49 AM
Does that fix it?

Yes! Thank you Mark!

I don't understand what happened here, because I did use this macro successfully before, and I thought this was the latest version. But anyway, I'm happy it's working now!

GTO
03-06-2016, 11:29 PM
You are most welcome and glad it was an easy fix:beerchug: