且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何更改此vba以搜索可能包含或不包含子文件夹的pdfs文件夹。

更新时间:2023-01-15 19:15:44

要浏览子文件夹,首先必须找到它们。您的F_Folder变量包含当前文件夹,这使您可以访问SubFolders属性。使用它以相同的方式循环遍历子文件夹(请注意,您还必须跨每个子文件夹执行此操作)。为了方便自己,您应该将功能拆分出来,以便将文件迭代到一个单独的例程中,并从文件夹代码中调用它。实际上你会得到这样的结果:
  Set  F_Folder = FSO.GetFolder(Selected_Items)
IterateFolders(F_Folder,i )
.....

Sub IterateFolders( ByVal F_Folder, ByRef i)
对于 每个 SubFolder F_Folder
IterateFolders(SubFolder,i)
下一步
对于 每个 F_File F_Folder .Files
Selected_Items = UCase(F_File.Path)
If Right(Selected_Items, 4 )= 。PDF 然后an>
设置 Acrobat_File = Acrobat.AcroPDDoc
Acrobat_File.Open Selected_Items
Cells(i, 1 )。Value = Selected_Items
Cells(i, 2 ).Value = Acrobat_File.GetNumPages
i = i + 1
Acrobat_File.Close
设置 Acrobat_File = Nothing
结束 如果
下一步
结束 子跨度>


Sorry I am new to programming and going to school for it. I found this macro for excel a while back and tried getting some help on it. No replies. I tried to code it with other macros I have and no good outcomes.

What I want this macro to do is go through a folder that might or might not have sub-folders.

Sub PDFPageNumbers()
    Dim FSO As Object
    Dim F_Folder As Object
    Dim F_File As Object
    Dim Selected_Items As String
    Dim DialogFolder As FileDialog
    Dim Acrobat_File As Acrobat.AcroPDDoc
    Dim i As Long

    'Select PDF Directory
    Set DialogFolder = Application.FileDialog(msoFileDialogFolderPicker)
    If DialogFolder.Show = -1 Then
        Selected_Items = DialogFolder.SelectedItems(1)
    Else: Set DialogFolder = Nothing
    End If
    Set DialogFolder = Nothing
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set F_Folder = FSO.GetFolder(Selected_Items)
    i = 2
    For Each F_File In F_Folder.Files
        Selected_Items = UCase(F_File.Path)
        If Right(Selected_Items, 4) = ".PDF" Then
            Set Acrobat_File = New Acrobat.AcroPDDoc
            Acrobat_File.Open Selected_Items
            Cells(i, 1).Value = Selected_Items
            Cells(i, 2).Value = Acrobat_File.GetNumPages
            i = i + 1
            Acrobat_File.Close
            Set Acrobat_File = Nothing
        End If
    Next

    Range("A:B").Columns.AutoFit

    Set F_File = Nothing
    Set F_Folder = Nothing
    Set FSO = Nothing

End Sub

In order to go through the sub folders, you first have to find them. Your F_Folder variable contains the current folder, and this gives you access to a SubFolders property. Use this to loop through the sub folders in the same manner (note that you will have to perform this operation across each sub folder as well). To ease things for yourself, you should split the functionality out for iterating over the files into a separate routine and call that from your folder code. Effectively you would end up with something like this:
    Set F_Folder = FSO.GetFolder(Selected_Items)
    IterateFolders(F_Folder, i)
.....

Sub IterateFolders(ByVal F_Folder, ByRef i)
    For Each SubFolder In F_Folder
        IterateFolders(SubFolder, i)
    Next
    For Each F_File In F_Folder.Files
        Selected_Items = UCase(F_File.Path)
        If Right(Selected_Items, 4) = ".PDF" Then
            Set Acrobat_File = New Acrobat.AcroPDDoc
            Acrobat_File.Open Selected_Items
            Cells(i, 1).Value = Selected_Items
            Cells(i, 2).Value = Acrobat_File.GetNumPages
            i = i + 1
            Acrobat_File.Close
            Set Acrobat_File = Nothing
        End If
    Next
End Sub