簡體   English   中英

從 Excel VBA 宏執行 VBScript 文件

[英]Executing VBScript file from Excel VBA macros

I need some excel vba examples, where with in the VBA code(Excel Macro) i could call a VBScript and will get some values like filename and directory information from the vbscript and assign it to the variables in VBA code. 先感謝您

像這樣的東西

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

可以做到,但我必須同意 Tomalak 和其他人的觀點,即這不是 go 的最佳方式。 不過,話說回來,VBScript 如果你把它當作一種“一勞永逸”的機制,偶爾會產生奇跡。 它可以非常有效地用於模擬 VBA 中的多線程,從而您可以分解有效負載並將其分配給單獨的 VBScript 以獨立運行。 例如,您可以安排一個“群”的單個 VBScripts 在后台從網站大量下載,而 VBA 繼續使用其他代碼。

下面是一些 VBA 代碼,我已經簡化以顯示可以做什么,並即時編寫一個簡單的 VBScript。 通常我更喜歡使用'wshShell.Run """" & SFilename & """"運行它,這意味着我可以忘記它,但我在這個例子中包含了這個方法Set proc = wshShell.exec(strexec)它允許完成 object 測試

把它放在 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

這展示了可以傳遞變量的幾種方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM