【问题标题】:Open pdf file looping through folders and subfolders打开 pdf 文件循环遍历文件夹和子文件夹
【发布时间】:2019-03-18 16:38:45
【问题描述】:

我编写了一个使用 Beforedoubleclick() 范围的宏:双击第 5 列的单元格,然后宏进入特定文件夹以查找在单元格内写入代码的 pdf 文件。

此宏无法扫描子文件夹,但我的 pdf 文件可能位于子文件夹中。

我在网上搜索,似乎我必须使用循环或类似的东西。这段代码我不知道怎么写。

我的宏:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
Dim testo As String
Dim nomefile As String
Dim path As String

On Error Resume Next

If Target.Column = 5 Then

    path = "C:\Users\Alex\"
    testo = path & Cells(Target.Row, 5)
    nomefile = Dir(Left(testo, Len(testo)) & "*.pdf")

    If nomefile = "" Then
        MsgBox "File non trovato", vbCritical, "ATTENZIONE"
        Exit Sub
    End If

    Do
        Shell "C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe " & path & nomefile, vbMaximizedFocus
        nomefile = Dir
    Loop While nomefile <> ""
End If
End Sub

【问题讨论】:

  • 您应该在这里查看如何使用FileSystemObjectFSO,这正是您所需要的。
  • 在 SO 中有很多例子可以通过子文件夹进行递归搜索。如果您使用Cmd 窗口Dir 命令,如this question 所示,您甚至可以在字符串中包含通配符。虽然,如果您的文件名包含代码 > 255 的字符,您可能需要使用 fileSystemObject 命令以外的其他内容来读取它们。

标签: excel vba


【解决方案1】:

试试这样的:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
    Dim testo As String
    Dim FSO As Object
    Dim Folder As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")

    Set Folder = FSO.GetFolder("C:\Users\Alex\")
    ...
    ...
    testo = path & Cells(Target.Row, 5)
    nomefile = FindFile(testo, Folder, FSO)
End Sub

Function FindFile(FileName As String, Folder As Object, ByRef FSO As Object) As String
    DoEvents

    If FSO.FileExists(Folder & "\" & FileName & ".pdf") Then
        FindFile = Folder & "\" & FileName & ".pdf"
        Exit Function
    End If

    Dim SubFolder As Object
    For Each SubFolder In Folder.SubFolders
        FindFile = FindFile(FileName, SubFolder, FSO)
    Next
End Function

FindFile 函数从一个文件夹开始。我找到的文件在那里,很好 - 将它归还。如果没有,请获取每个子文件夹并调用 FindFile 并尝试。 DoEvents 在那里,否则很难打断。

【讨论】:

    【解决方案2】:

    感谢山姆的建议;我尝试使您的代码适应我的情况,我写了这个:

    Function FindFile(FileName As String, Folder As Object, ByRef FSO As Object) As String
    DoEvents
    
    If FSO.FileExists(Folder & "\" & FileName & "*.pdf*") Then
        FindFile = Folder & "\" & FileName & "*.pdf*"
        Exit Function
    End If
    
    Dim SubFolder As Object
    For Each SubFolder In Folder.SubFolders
        FindFile = FindFile(FileName, SubFolder, FSO)
    Next
    

    结束函数

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
    Dim testo As String
    Dim nomefile As String
    Dim FSO As Object
    Dim Folder As Object
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set Folder = FSO.GetFolder("C:\Users\Alex")
    If Target.Column = 5 Then
        testo = Cells(Target.Row, 5)
        nomefile = FindFile(testo, Folder, FSO)
    
        If nomefile = "" Then
            MsgBox "File not found!", vbCritical, "WARNING"
            Exit Sub
        End If
    
        CreateObject("WScript.Shell").Run nomefile
    End If
    

    结束子

    问题是我要查找的文件(名为“Test.pdf”)不直接位于“Alex”文件夹中,而是位于子文件夹中(...\Alex\TestFolder\")。 宏总是返回 MsgBox“找不到文件!”。 我的代码有什么问题?

    谢谢, 亚历克斯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-11
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多