How do I open all files in the same folder with the same file extension?

DanSMT

Board Regular
Joined
Sep 13, 2019
Messages
203
Office Version
  1. 2013
Platform
  1. Windows
I have run into an issue. Originally the following code worked for what I was intending, however when multiple files were involved the code did not work.

Does anyone know how to open all files in the current folder the workbook is in that end in the same .pdf file extension?

Current code below;

Private Sub Workbook_Open()
Dim strFileName As String
Dim strFileExists As String
Dim sPath As String
sPath = Application.ActiveWorkbook.Path
strFileName = Dir$(sPath & "\*.pdf")
strFileExists = Dir(strFileName)

If strFileExists = "" Then
ThisWorkbook.FollowHyperlink Dir$(sPath & "\*.pdf")
Else

End If

End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Here is code for you to loop through the folder. Make sure "Microsoft Scripting Runtime" is checked in your references.
Snag_1b10a1cc.png



VBA Code:
Private Sub Workbook_Open()
Dim strFileName As String, file As Object, filesInFolder As Object, folderName As Object, Folders As Object
Dim strFileExists As String
strFileName = Application.ActiveWorkbook.Path
Set filesInFolder = CreateObject("Scripting.FileSystemObject")
Set folderName = filesInFolder.GetFolder(strFileName)
Set Folders = folderName.Files
For Each file In Folders
    If Right(file, 4) = ".pdf" Or Right(file, 4) = ".PDF" Then
        
    Else

    End If
Next file
End Sub
 
Upvote 0
I left the rest of the code for you, Dan. Please see the green text below, that is what you have to fill out

VBA Code:
Private Sub Workbook_Open()
Dim strFileName As String, file As Object, filesInFolder As Object, folderName As Object, Folders As Object
Dim strFileExists As String
strFileName = Application.ActiveWorkbook.Path
Set filesInFolder = CreateObject("Scripting.FileSystemObject")
Set folderName = filesInFolder.GetFolder(strFileName)
Set Folders = folderName.Files
For Each file In Folders
    If Right(file, 4) = ".pdf" Or Right(file, 4) = ".PDF" Then
        'INSERT YOUR CODE HERE FOR WHAT YOU WANT TO DO WITH PDF FILES'
    Else
        'INSERT YOUR CODE HERE FOR WHAT YOU WANT TO DO WITH NON PDF FILES'
    End If
Next file
End Sub
 
Upvote 0
I saw that now. Thanks for the assistance!!
 
Upvote 0
I used.

ThisWorkbook.FollowHyperlink Dir$(strFileName & "\*.pdf")
 
Upvote 0
I used.

ThisWorkbook.FollowHyperlink Dir$(strFileName & "\*.pdf")
If you do that, it will only open whatever your string file is name and .pdf. If you do
VBA Code:
ActiveWorkbook.FollowHyperlink file
it will open every ".pdf" that is in that folder. Do you want to grab just the sheet with the same name and ".pdf" or do you want to open every ".pdf" file in that folder?
 
Upvote 0
I agree. Your way opens all files independently. The other way works, however opens most of the pdfs in a single tab.
 
Upvote 0

Forum statistics

Threads
1,215,261
Messages
6,123,945
Members
449,134
Latest member
NickWBA

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