如何通过 VBA 评估来自 javascript 的 vbscript 表达式 [英] How to evaluate vbscript expression from javascript via VBA

查看:17
本文介绍了如何通过 VBA 评估来自 javascript 的 vbscript 表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最新的 MS Office 更新禁用了我在 Excel VBA 中用来计算表达式的 vbscript.我发现这比过去的 VBA评估"函数快得多,所以想避免这样做.

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 仍然有效,所以我正在尝试迁移到它.然而,我们的一些更复杂的表达式,使用幂或平方根,如果不重新编写表达式以使用 Math.Pow() 等,则无法工作.

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.

所以我的问题是我是否可以运行一个 javascript 来评估 vbscript 表达式.

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

我想要的是评估 vbscript 表达式但使用 javascript 的东西,从而绕过 MS Office 错误.

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

这里是有关 MS Office 更新问题的信息,以防有人感兴趣.我认为对很多人来说可能是一个大问题:https://social.msdn.microsoft.com/Forums/en-US/45c14333-3685-4b2d-9a3a-6a109a5a2a86/access-2016-microsoft-script-control-停止工作错误380?forum=accessdev

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

感谢您的帮助!

推荐答案

试试这个解决方案:

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

这篇关于如何通过 VBA 评估来自 javascript 的 vbscript 表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆