Hyperlink to file where where PDF Page Count was performed

JerryKaminsky

New Member
Joined
Aug 12, 2009
Messages
21
Greetings,
I am a novice to VBA. I found a piece of code that counts the number of pages in each PDF located in a folder. I would like the macro to add a hyperlink to the filename in the report. This would cut-down on navigating back to the folder where the files that were counted are located. Here is the code I found.

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:B").ClearContents
Range("A1:B1").Font.Bold = True
xRg = "File Name"
xRg.Offset(0, 1) = "Pages"
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
End If
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi. I made a few changes to the code, but it should otherwise do what you ask for:

VBA Code:
Sub GeneratePDFList()
    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
    Dim xWorksheet             As Worksheet
    Dim xFullPath              As String
    
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    If xFd.Show = -1 Then
        xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
        xFileName = Dir(xFdItem & "*.pdf", vbDirectory)
        Set xRg = Application.ActiveSheet.Range("A1")
        Set xWorksheet = xRg.Parent
        xWorksheet.Range("A:B").ClearContents
        xWorksheet.Range("A1:B1").Font.Bold = True
        xRg.Value = "File Name"
        xRg.offset(0, 1).Value = "Pages"
        I = 2
        xStr = ""
        Set RegExp = CreateObject("VBscript.RegExp")
        RegExp.Global = True
        Do While xFileName <> ""
            xFullPath = xFdItem & xFileName
            xWorksheet.Cells(I, 1).Value = xFileName
            xWorksheet.Hyperlinks.Add Anchor:=xWorksheet.Cells(I, 1), Address:=xFullPath
            RegExp.Pattern = "/Type\s*/Page[^s]"
            xFileNum = FreeFile
            Open (xFullPath) For Binary As #xFileNum
            xStr = Space(LOF(xFileNum))
            Get #xFileNum, , xStr
            Close #xFileNum
            xWorksheet.Cells(I, 2) = RegExp.Execute(xStr).Count
            I = I + 1
            xFileName = Dir
        Loop
        xWorksheet.Columns("A:B").AutoFit
    End If
End Sub

Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

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