vbscript - 从 Excel VBA 宏执行 VBScript 文件

标签 vbscript excel vba

我需要一些 Excel VBA 示例,在 VBA 代码(Excel 宏)中,我可以调用 VBScript,并从 vbscript 中获取一些值,例如文件名和目录信息,并将其分配给 VBA 代码中的变量。 预先感谢您

像这样的事情

VBA 宏:

Sub Foo2Script
Dim x As Long
x=2
'Call VBscript here
MsgBox scriptresult
End Sub

VBScript:

Dim x, y ,Z
x = x_from_macro
y = x + 2
Z = X+Y
scriptresult = y,Z

最佳答案

这是可以做到的,但我必须同意托马拉克和其他人的观点,即这不是最好的方法。然而,话虽如此,如果您将 VBScript 用作一种即发即忘机制,它有时会产生奇迹。它可以非常有效地用于模拟 VBA 中的多线程,从而可以分解有效负载并将其分配给各个 VBScript 以独立运行。例如,您可以安排一群单独的 VBScript 在后台从网站上进行大量下载,同时 VBA 继续处理其他代码。

下面是我简化的一些 VBA 代码,以显示可以做什么,并即时编写一个简单的 VBScript。通常我更喜欢使用 'wshShell.Run """"& SFilename & """" 运行它,这意味着我可以忘记它,但我在这个示例中包含了这个方法 Set proc = wshShell.exec(strexec) 允许测试对象是否完成

将其放入 MODULE1

Option Explicit

Public path As String

Sub writeVBScript()
Dim s As String, SFilename As String
Dim intFileNum As Integer, wshShell As Object, proc As Object


Dim test1 As String
Dim test2 As String

test1 = "VBScriptMsg - Test1 is this variable"
test2 = "VBScriptMsg - Test2 is that variable"

    'write VBScript (Writes to Excel Sheet1!A1 & Calls Function Module1.ReturnVBScript)
    s = s & "Set objExcel = GetObject( , ""Excel.Application"") " & vbCrLf
    s = s & "Set objWorkbook = objExcel.Workbooks(""" & ThisWorkbook.Name & """)" & vbCrLf
    s = s & "Set oShell = CreateObject(""WScript.Shell"")" & vbCrLf
    s = s & "Msgbox (""" & test1 & """)" & vbCrLf
    s = s & "Msgbox (""" & test2 & """)" & vbCrLf
    s = s & "Set oFSO = CreateObject(""Scripting.FileSystemObject"")" & vbCrLf
    s = s & "oShell.CurrentDirectory = oFSO.GetParentFolderName(Wscript.ScriptFullName)" & vbCrLf
    s = s & "objWorkbook.sheets(""Sheet1"").Range(""" & "A1" & """) = oShell.CurrentDirectory" & vbCrLf
    s = s & "Set objWMI = objWorkbook.Application.Run(""Module1.ReturnVBScript"", """" & oShell.CurrentDirectory & """")  " & vbCrLf
    s = s & "msgbox(""VBScriptMsg - "" & oShell.CurrentDirectory)" & vbCrLf
    Debug.Print s

    ' Write VBScript file to disk
    SFilename = ActiveWorkbook.path & "\TestVBScript.vbs"
    intFileNum = FreeFile
    Open SFilename For Output As intFileNum
    Print #intFileNum, s
    Close intFileNum
    DoEvents

    ' Run VBScript file
    Set wshShell = CreateObject("Wscript.Shell")
    Set proc = wshShell.exec("cscript " & SFilename & "") ' run VBScript
    'could also send some variable
    'Set proc = wsh.Exec("cscript VBScript.vbs var1 var2") 'run VBScript passing variables

    'Wait for script to end
    Do While proc.Status = 0
    DoEvents
    Loop

    MsgBox ("This is in Excel: " & Sheet1.Range("A1"))
    MsgBox ("This passed from VBScript: " & path)

    'wshShell.Run """" & SFilename & """"

    Kill ActiveWorkbook.path & "\TestVBScript.vbs"
End Sub


Public Function ReturnVBScript(strText As String)
path = strText
End Function

这演示了变量传递的几种方式。

关于vbscript - 从 Excel VBA 宏执行 VBScript 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6238215/

相关文章:

variables - VBScript 读取特定行并提取字符并将其存储为变量

sql - For 循环控制变量、嵌套 ResultSet SQL 无效

vbscript - SystemUtil.Run 在 vb 脚本中不起作用

vba - Excel 2010 VBA 使用不同颜色的单元格突出显示多个列中具有不同重复值的单元格

vba - 即使是静态的,Property Get 也不会保留值

vba - 打开另存为窗口并自动填充文件名和类型的宏

excel - 如何从左到右对列进行排序?

vbscript - 在 VBScript 中停止服务并等待

Excel/GSheets 为动态列表中的重复值添加唯一标志

vba - 是否有用于打开和关闭循环以及 if 语句的 VBA 语法突出显示?