VBScript - 如何让这些工作簿交谈?

时间:2012-04-23 16:28:58

标签: vba vbscript excel-vba excel

我之前发布过关于让我的VBScript等到流程完成后再继续(更多信息:VBScript - How to make program wait until process has finished?

经过一番讨论,我得到了足够的答案。但是,似乎我现在正朝着代码的新方向发展,因为解决方案提出了另一个问题,我希望你能帮助我。

基本上我有一些我在下面提供的代码。它接受4个参数,其中一个是包含许多文件的文件夹的PATH,我想在我的VBA宏中使用其他三个文件。

If WScript.Arguments.Count = 4 Then

    ' process input argument
    Set args = WScript.Arguments
    arg1 = args.Item(0)
    arg2 = args.Item(1)
    arg3 = args.Item(2)
    arg4 = args.Item(3)

    ' Create a WshShell instance 
    Dim WShell
    Set WShell = CreateObject("WScript.Shell")

    ' Create an Excel instance
    Dim x1
    Set x1 = CreateObject("Excel.Application")

    ' Disable Excel UI elements
    x1.DisplayAlerts = False
    x1.AskToUpdateLinks = False
    'x1.AlertBeforeOverwriting = False
    x1.FeatureInstall = msoFeatureInstallNone

    ' Open the Workbooks specified on the command-line
    Dim x1WB 
    Dim x2WB 
    Dim x3WB 
    Dim x4WB 
    Dim strWB1
    Dim strWB2
    Dim strWB3
    Dim strWB4

    Dim FSO
    Dim FLD
    Dim FIL
    Dim strFolder

    strWB1 = arg1
    Set x1WB = x1.Workbooks.Open(strWB1)
    ' Show the workbook/Excel program interface. Comment out for silent running.
    x1WB.Application.Visible = True

    strWB2 = arg2
    Set x2WB = x1.Workbooks.Open(strWB2)
    ' Show the workbook/Excel program interface. Comment out for silent running.
    x2WB.Application.Visible = True

    strWB3 = arg3
    Set x3WB = x1.Workbooks.Open(strWB3)
    ' Show the workbook/Excel program interface. Comment out for silent running.
    x3WB.Application.Visible = True

    'To hold the string of the PATH to the multiple files
    strFolder = arg4

    Set FSO = CreateObject("Scripting.FileSystemObject")
    'Get a reference to the folder I want to search
    set FLD = FSO.GetFolder(strFolder)

    Dim strMyMacro
    strMyMacro = "my_excel_sheet_with_vba_module.xlsm!Sheet1.my_vba_macro"

    'loop through the folder and get the file names
    For Each Fil In FLD.Files

        WshShell.run """C:\Program Files\Microsoft Office\Office14\EXCEL.exe"" " & Fil, 1, true

        x1.Run strMyMacro

        '~~> Problem - How do I get the macro to run before opening the above file but run after it has opened (due to setting the bWaitOnReturn to true)
        '~~> Problem - How do I get the file on current iteration to close after the macro has completed?
        '~~> Problem - If this is not the issue, can you identify it?

    Next

    x1WB.close
    x2WB.close
    x3WB.close
    'x4WB.close

    ' Clean up and shut down
    Set x1WB = Nothing
    Set x2WB = Nothing
    Set x3WB = Nothing
    Set x4WB = Nothing

    Set FSO = Nothing
    Set FLD = Nothing

    x1.Quit
    Set x1 = Nothing
    Set WshShell = Nothing

    WScript.Quit 0

Else
        WScript.Quit 1

End If

脚本的工作原理如下:

  1. 将4个参数传递给脚本。第三个参数是 .xlsm 文件,其中包含我的VBA宏。最后一个参数是包含多个文件的文件夹的PATH。
  2. 然后打开前三个Excel文件。
  3. 然后我运行一个循环来遍历指定为第4个参数的文件夹中的文件Fil。 AFAIK必须使用WScript.shell方法通过.run完成此操作,以便脚本的其余部分将挂起,直到处理完的Excel文件结束并打开文件夹中的下一个文件
  4. 打开文件Fil后,我运行宏(尽管此时此刻未成功)。
  5. 我很想简单地使用WScript.shell对象打开所有Excel文件,但是AFAIK我无法以这种方式运行宏。

    希望我已经能够定义这篇VBScript的目标,但如果我没有让我知道,我会澄清。你能帮忙吗?

    谢谢, QF。

1 个答案:

答案 0 :(得分:1)

这些内容可能对您有用(在Excel中)。我不清楚的一些事情:

  1. 你现有的VBA宏在哪里 - 我猜它是你正在打开的3个文件中的一个?
  2. 您正在循环的文件夹中包含哪些类型的文件?我猜对了。
  3. vbscript是如何运行的?看起来你正在从你的HTA中淘汰,但为什么不将它直接包含在HTA中呢?那样可以避免你不得不弹出参数......
  4. Option Explicit

    Dim wb1 As Workbook, wb2 As Workbook
    
    Sub ProcessFolder(path1, path2, sFolder)
    
        Dim wb As Workbook
        Dim s
    
        Set wb1 = Workbooks.Open(path1)
        Set wb2 = Workbooks.Open(path2)
        If Right(sFolder, 1) <> "\" Then sFolder = sFolder & "\"
    
        s = Dir(sFolder & "*.xls*", vbNormal)
    
        Do While Len(s) > 0
            Set wb = Workbooks.Open(sFolder & s)
            ProcessFile wb
            wb.Close False
            s = Dir()
        Loop
    
        wb1.Close False
        wb2.Close False
    
    End Sub
    
    
    Sub YourExistingMacro(wb As Workbook)
    'do stuff with wb and presumably the other 3 open files...
    End Sub
    

    Dim wb1 As Workbook, wb2 As Workbook Sub ProcessFolder(path1, path2, sFolder) Dim wb As Workbook Dim s Set wb1 = Workbooks.Open(path1) Set wb2 = Workbooks.Open(path2) If Right(sFolder, 1) <> "\" Then sFolder = sFolder & "\" s = Dir(sFolder & "*.xls*", vbNormal) Do While Len(s) > 0 Set wb = Workbooks.Open(sFolder & s) ProcessFile wb wb.Close False s = Dir() Loop wb1.Close False wb2.Close False End Sub Sub YourExistingMacro(wb As Workbook) 'do stuff with wb and presumably the other 3 open files... End Sub