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.

Author: brucemcp

2 thoughts on “Using JScript inside VBA

Leave a Reply

Your email address will not be published. Required fields are marked *