0

我有一个调用 Chrome 的宏,但是在不同的机器上部署这段代码会被每台机器上不同的 chrome 的完整文件路径破坏。为了缓解这种情况,我想一次性搜索“chrome.exe”并将文件路径存储为变量。以下似乎可行,但是递归会产生堆栈空间问题(运行时错误 28)。

这似乎是一个如此简单的操作,并且递归搜索遍布通常的论坛,但我无法正确!

Function Recurse(sPath As String) As String
Dim FSO As New FileSystemObject
Dim myFolder As Folder
Dim mySubFolder As Folder
Dim myFile As File
Application.ScreenUpdating = False
Set myFolder = FSO.GetFolder("C:\")
For Each mySubFolder In myFolder.SubFolders
    For Each myFile In mySubFolder.Files
        If myFile.Name = "chrome.exe" Then
            Debug.Print myFile.Name & " in " & myFile.Path
            Exit For
        End If
    Next
    Recurse = Recurse(mySubFolder.Path)
Next
Application.ScreenUpdating = True
End Function

Sub ChromeSearch()
Call Recurse("C:\")
End Sub
4

1 回答 1

0

请注意,您正在将参数传递sPath给递归函数。

然后,您将忽略此参数并在C:\每次调用时搜索文件夹。现在你有一个没有终止条件的递归函数,所以你当然会得到一个堆栈溢出异常。

只需更换

Set myFolder = FSO.GetFolder("C:\")

Set myFolder = FSO.GetFolder(sPath)

并替换:

Recurse = Recurse(mySubFolder.Path)

Recurse = Recurse(sPath + "\\" + mySubFolder.Path);

(或者你用这种语言连接文件夹)


关于查找 Chrome.exe 的话题,如果您首先查看两个地方,您将节省大量时间:

  • "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
  • (环境变量 LOCALAPPDATA) + "\Google\Chrome\Application\Chrome.exe"
于 2016-01-14T22:11:19.280 回答