Vbscript 在子文件夹中搜索Excel

Vbscript 在子文件夹中搜索Excel,vbscript,Vbscript,使用我从web上提取的以下代码,我能够在单个目录中搜索包含某行字符串的excel文件。我如何允许它在所有子文件夹中也是递归的?我已经找到了一些答案,但我不明白如何在代码中实现它们。我昨天才开始使用VBScript,我对如何使其工作感到非常困惑 strComputer = "CAA-W74109188" Set objExcel = CreateObject("Excel.Application", strComputer) Set objWMIService = GetObject("win

使用我从web上提取的以下代码,我能够在单个目录中搜索包含某行字符串的excel文件。我如何允许它在所有子文件夹中也是递归的?我已经找到了一些答案,但我不明白如何在代码中实现它们。我昨天才开始使用VBScript,我对如何使其工作感到非常困惑

strComputer = "CAA-W74109188"

Set objExcel = CreateObject("Excel.Application", strComputer)

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set FileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='c:\TDRS'} Where " _
    & "ResultClass = CIM_DataFile")


 For Each objFile In FileList
  If (objFile.Extension = "xlsm" or  objFile.Extension = "xls") Then
    Set objWorkbook = objExcel.Workbooks.Open(objFile.Name)
    Set objWorksheet = objWorkbook.Worksheets(1)
    If objExcel.Cells(3,10) = "Complete" or objExcel.Cells(3,9) = "Released" Then
        Wscript.Echo objFile.FileName
    End If

objExcel.DisplayAlerts = False
objworkbook.Saved = False
    objWorkbook.Close False
End If
Next

objExcel.Quit

这是一个我用来删除文件的脚本,我根据您的需要对它进行了修改。递归函数是完成这项工作所需要的,我一直觉得它们很有趣,也有点难以理解

Dim Shell : Set Shell = WScript.CreateObject( "WScript.Shell" )
Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
Dim objExcel : Set objExcel = CreateObject("Excel.Application")

Dim Paths(0)
Paths(0) = "c:\temp"

For Each Path in Paths
   FolderScan(Path)
Next

Sub FolderScan(Folder) 
 Set base = oFSO.GetFolder(Folder) 
    If base.SubFolders.Count Then
       For Each folder in Base.SubFolders
         FolderScan(folder.Path)
       Next
    End If
 Set files = base.Files
    If files.Count Then
       For Each File in files
          If LCase(oFSO.GetExtensionName(File.Path) = "xlsm") or _
            LCase(oFSO.GetExtensionName(File.Path) = "xls") Then

              Dim objWorkbook : Set objWorkbook = objExcel.Workbooks.Open(File.Path)
              Dim objWorkSheet : Set objWorkSheet = objWorkbook.Worksheets(1)

                If (objExcel.Cells(3,10) = "Complete" or _
                  objExcel.Cells(3,9) = "Released") Then
                    Wscript.echo File.Path
                End if
                objExcel.DisplayAlerts = False
                objExcel.Quit
          End If
       Next
    End If
 End Sub

这是一个通用的递归函数,它迭代给定文件夹对象的所有文件和子文件夹


可能重复Hey Dan,你知道有什么方法可以阻止宏在我打开和关闭excel工作表时运行吗?有一个“Do you want Save”宏在关闭时运行,它会破坏代码。请尝试在
Dim objExcel
objExcel.EnableEvents=False
之后添加这一行。您是各种各样的帮助对象。再次感谢你。
Dim FileSystem
Set FileSystem = CreateObject("Scripting.FileSystemObject")

DoFolder FileSystem.GetFolder("c:\somefolder")

Sub DoFolder(Folder)

    Dim SubFolder
    For Each SubFolder In Folder.SubFolders
        DoFolder SubFolder
    Next

    Dim File
    For Each File In Folder.Files
        ' Operate on each file
    Next

End Sub