Using JScript inside VBA

Those of you that read this blog, and the ramblings site, will know that the topic content is about equally split between VBA and javaScript.

Until today, I had no idea that there was a scripting object that could be used from VBA, but would allow you to write your code in JScript. The ScriptControl object allows you to do that.

Consider this (you’ll need to add a reference to scriptControl v1.0 in the VBE)

Public Sub x()
    Dim o As New ScriptControl
    o.Language = "JScript"
    With o
        .AddCode "function x(a,b) {return 'the answer is:' +(a+b);}"
        Debug.Print .Run("x", 1, 2)
    End With
End Sub

 

the answer is:3

… you will see more about this in future posts as I dig into how far this can be pushed.

 

About brucemcp 225 Articles
I am a Google Developer Expert and decided to investigate Google Apps Script in my spare time. The more I investigated the more content I created so this site is extremely rich. Now, in 2019, a lot of things have disappeared or don’t work anymore due to Google having retired some stuff. I am however leaving things as is and where I came across some deprecated stuff, I have indicated it. I decided to write a book about it and to also create videos to teach developers who want to learn Google Apps Script. If you find the material contained in this site useful, you can support me by buying my books and or videos.