Replace File Dialog Picker with Path

scotsrule08

New Member
Joined
Jun 21, 2018
Messages
45
Good day friends,

I have a macro which selects a folder and then makes a Dir list and counts the number of pages in the pdf.

I am needing to replace the folder picker portion of the code with the static path of a folder. (This would prevent me from having to select the same folder every time)

Any help is greatly appreciated.

Code:
Sub Test()
    Dim I As Long
    Dim xRg As Range
    Dim xStr As String
    Dim xFd As FileDialog
    Dim xFdItem As Variant
    Dim xFileName As String
    Dim xFileNum As Long
    Dim RegExp As Object
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    If xFd.Show = -1 Then
        xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
        xFileName = Dir(xFdItem & "*.pdf", vbDirectory)
        Set xRg = Range("A1")
        Range("A:C").ClearContents
        Range("A1:C1").Font.Bold = True
        xRg = "File Name"
        xRg.Offset(0, 1) = "Pages"
        xRg.Offset(0, 2) = "New Name"
        I = 2
        xStr = ""
        Do While xFileName <> ""
            Cells(I, 1) = xFileName
            Set RegExp = CreateObject("VBscript.RegExp")
            RegExp.Global = True
            RegExp.Pattern = "/Type\s*/Page[^s]"
            xFileNum = FreeFile
            Open (xFdItem & xFileName) For Binary As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=xFileNum]#xFileNum[/URL] 
                xStr = Space(LOF(xFileNum))
                Get [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=xFileNum]#xFileNum[/URL] , , xStr
            Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=xFileNum]#xFileNum[/URL] 
            Cells(I, 2) = RegExp.Execute(xStr).Count
            I = I + 1
            xFileName = Dir
        Loop
        Columns("A:B").AutoFit
    End If
    Call FillFormula
End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try this, replace the part in red with the path to the folder from which you want to get a list of files.
Rich (BB code):
Sub Test()
Dim I As Long
Dim xRg As Range
Dim xStr As String
Dim xFd As FileDialog
Dim xFdItem As String
Dim xFileName As String
Dim xFileNum As Long
Dim RegExp As Object

    xFdItem = "C:\YourPathHere\"

    xFileName = Dir(xFdItem & "*.pdf", vbDirectory)
    
    Set xRg = Range("A1")
    Range("A:C").ClearContents
    Range("A1:C1").Font.Bold = True
    xRg = "File Name"
    xRg.Offset(0, 1) = "Pages"
    xRg.Offset(0, 2) = "New Name"
    
    I = 2
    
    xStr = ""
    
    Do While xFileName <> ""
        Cells(I, 1) = xFileName
        Set RegExp = CreateObject("VBscript.RegExp")
        RegExp.Global = True
        RegExp.Pattern = "/Type\s*/Page[^s]"
        xFileNum = FreeFile
        Open (xFdItem & xFileName) For Binary As #xFileNum 
        xStr = Space(LOF(xFileNum))
        Get #xFileNum , , xStr
        Close #xFileNum 
        Cells(I, 2) = RegExp.Execute(xStr).Count
        I = I + 1
        xFileName = Dir
    Loop
    
    Columns("A:B").AutoFit

    Call FillFormula

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top