如何更改此vba以搜索可能包含或不包含子文件夹的pdfs文件夹。 [英] How do I change this vba to search for pdfs folders that might or might not have sub-folders.

查看:75
本文介绍了如何更改此vba以搜索可能包含或不包含子文件夹的pdfs文件夹。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我是编程和上学的新手。我发现这个宏有一段时间了,并尝试获得一些帮助。没有回复。我尝试使用其他宏来编写它并且没有好的结果。



我想要这个宏做的是通过一个文件夹,可能有也可能没有sub -folders。



  Sub  PDFPageNumbers()
< span class =code-keyword> Dim FSO As Object
< span class =code-keyword> Dim F_Folder As Object
< span class =code-keyword> Dim F_File 作为 对象
< span class =code-keyword> Dim Selected_Items As String
< span class =code-keyword> Dim DialogFolder As FileDialog
Dim Acrobat_File As Acrobat.AcroPDDoc
Dim i As

' 选择PDF目录
设置 DialogFolder = Application.FileDialog(msoFileDialogFolderPicker)
如果 DialogFolder.Show = -1 然后
Selected_Items = DialogFolder.SelectedItems( 1
否则: 设置 DialogFolder =
结束 如果
设置 DialogFolder = Nothing
设置 FSO = CreateObject( Scripting.FileSystemObject
设置 F_Folder = FSO.GetFolder(Selected_Items)
i = 2
对于 每个 F_File F_Folder.Files
Selected_Items = UCase(F_File.Path)
如果右(Selected_Items, 4 )= 。PDF 然后
设置 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
结束 如果
下一步

范围( A:B)。Columns.AutoFit

设置 F_File =
设置 F_Folder = Nothing
设置 FSO = 没有

结束 Sub

解决方案

要浏览子文件夹,首先必须找到它们。您的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 然后
设置 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


这篇关于如何更改此vba以搜索可能包含或不包含子文件夹的pdfs文件夹。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆