Vba VBScript-如何让这些工作簿对话?

Vba VBScript-如何让这些工作簿对话?,vba,vbscript,excel,Vba,Vbscript,Excel,我之前发布了关于让我的VBScript等待进程完成后再继续的消息(进一步信息: 经过一番讨论后,我得到了一个充分的答案。然而,我现在似乎正朝着代码的新方向前进,因为解决方案提出了另一个问题,我希望您能够帮助我 基本上,我有下面提供的一些代码。它包含4个参数,其中一个参数是指向包含许多文件的文件夹的路径,我想与VBA宏中的其他三个参数一起使用 If WScript.Arguments.Count = 4 Then ' process input argument Set args

我之前发布了关于让我的VBScript等待进程完成后再继续的消息(进一步信息:

经过一番讨论后,我得到了一个充分的答案。然而,我现在似乎正朝着代码的新方向前进,因为解决方案提出了另一个问题,我希望您能够帮助我

基本上,我有下面提供的一些代码。它包含4个参数,其中一个参数是指向包含许多文件的文件夹的路径,我想与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
脚本的工作原理如下:

  • 向脚本传递4个参数。第3个参数是包含我的VBA宏的.xlsm文件。最后一个参数是包含多个文件的文件夹的路径
  • 然后打开前三个Excel文件
  • 然后,我运行一个循环来迭代指定为第四个参数的文件夹中的文件
    Fil
    。那么这必须通过
    WScript.shell
    使用
    .run
    方法来完成,以便脚本的其余部分将挂起,直到它正在处理的Excel文件完成,然后再关闭并打开下一个File在文件夹中
  • 打开file
    Fil
    后,我运行宏(尽管此时没有成功)
  • 我很想简单地用
    WScript.shell
    对象打开所有Excel文件,但恐怕我无法以这种方式运行宏

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

    谢谢,
    QF.

    类似于这些内容的东西可能适合您(在Excel中)。但有几件事我不太清楚:

  • 你现有的VBA宏在哪里?我猜它在你打开的3个文件中的一个
  • 你正在循环的文件夹中有哪些类型的文件?我猜是Excel
  • vbscript是如何运行的?看起来您正在从HTA中剥离,但为什么不直接将其包含在HTA中?这样可以避免您必须剥离并传递参数
  • 
    选项显式

    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作为工作簿,wb2作为工作簿
    子进程文件夹(路径1、路径2、文件夹)
    将wb设置为工作簿
    暗淡的
    设置wb1=工作簿。打开(路径1)
    设置wb2=工作簿。打开(路径2)
    如果正确(sFolder,1)“\”则sFolder=sFolder&“\”
    s=Dir(sFolder&“*.xls*”,vbNormal)
    当Len(s)>0时执行
    设置wb=工作簿。打开(s文件夹(&s)
    进程文件wb
    wb.关闭错误
    s=Dir()
    环
    wb1.关闭错误
    wb2.关闭错误
    端接头
    Sub-YourExistingMacro(wb作为工作簿)
    '使用wb和其他3个打开的文件进行操作。。。
    端接头
    

    似乎最好使用
    x1.Workbooks.Open(Fil)
    然后使用Shell?如果您将大部分代码移动到xlsm中的VB宏中,并且只将vbscript参数传递给它(您可以使用Application.Run传递参数),总体上会容易得多。这使整个过程更易于管理。谢谢。但我需要将其作为VBScript,因为它与HTA前端一起处理文件。此外,如果这是在VBA中,则意味着手动打开每个文件并从Excel运行宏,然后关闭并处理我拥有的20多个文件。重点是实现自动化。因此我真的被困在这里了。还有其他想法吗?此外,如果我要执行
    x1.run
    ,我不能提供参数来告诉它等待,可以吗?也许我遗漏了什么,但我看不出有任何理由不将目录循环作为一个单独的宏移动到xlsm中:vbscript然后只将四个参数传递给该宏。这样更容易实现管理从Excel内部循环而不是从外部循环。@qwerty_face,我觉得Tim是对的,我会使用vbscript作为起始点,而不是HTA,你可以使用fileselector或IE对象获取变量,然后使用VBA来做Excel的事情。既然你可以混合使用vbscript和VBA,这应该不会有问题。也不需要宏,它们也可以在rporated in vbscript/vba code.OK Tim…因此,如果我将大部分代码移动到.xlsm文件中的vba代码中,我仍然可以使用vbscript获取所需的变量,即3个文件字符串和1个文件夹路径?然后我将这些变量从vbscript传递到vba宏-是吗?您建议我将哪些代码部分放入vba?@peter:can你指给我一些资源,让我了解这个‘IE对象文件选择器’?你还说:“不需要宏,它们可以合并到vbscript/vba代码中。”为了确保我们在同一页上,我可能使用了“宏”一词当我指的是我的VBA代码时。谢谢你的输入。此后我所做的是废弃HTA和VBScript,并在VBA中包含文件和文件夹选择器,因此此问题现在得到解决。但回顾性地回答你的问题:1.VBA宏位于.xlsm文件中,该文件是打开的前3个文件之一,2.是的,文件夹中的所有文件都是either.xls或.xlsx,3.正在从HTA调用VBScript,HTA在其中接受3个文件和1个文件夹路径,并将它们作为参数传递给VBScript-因此,是的,我从HTA进行了外壳处理。也就是说,您的代码很有用。