How to evaluate vbscript expression from javascript via VBA

喜欢而已 提交于 2020-01-30 11:33:27

问题


The latest MS Office update has disabled vbscript which I am using in Excel VBA to evaluate expressions. I have found this to be substantially faster than the VBA "Evaluate" function in the past so want to avoid doing that.

Javascript still works so I am trying to migrate to that. However some of our more complex expressions, which use powers or square roots, do not work without re-writing the expressions to use Math.Pow() etc.

So my question is if I can run a javascript which evaluates a vbscript expression.

Example of what I am doing currently as follows:

Sub TestVBScript()
Dim objscript As New ScriptControl
Dim var As Variant
Dim str As String
objscript.Language = "vbscript"
str = "2+2"
var = objscript.Eval(str)
MsgBox str & " = " & var
End Sub

What I want is something which evaluates the vbscript expression but using javascript, thereby bypassing the MS Office bug.

Here is information about the MS Office update issue in case anyone interested. I think likely to be a big problem for a lot of people: https://social.msdn.microsoft.com/Forums/en-US/45c14333-3685-4b2d-9a3a-6a109a5a2a86/access-2016-microsoft-script-control-stopped-working-error-380?forum=accessdev

Thank you for your help!


回答1:


Try this solution:

Sub Test()

    MsgBox Eval("1+2+3^4")

End Sub

Function Eval(s As String)

    Static oDoc As Object

    If oDoc Is Nothing Then
        Set oDoc = CreateObject("htmlfile")
        oDoc.parentWindow.execScript "Function EvalExpr(s): EvalExpr = Eval(s): End Function", "vbscript"
    End If
    Eval = oDoc.parentWindow.EvalExpr(s)

End Function


来源:https://stackoverflow.com/questions/51264509/how-to-evaluate-vbscript-expression-from-javascript-via-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!