VBA to search directory for file name and open

Caveman1964

Board Regular
Joined
Dec 14, 2017
Messages
121
I have searched the forum, lots of info but I can't find exaclty what I need....I am sure its here but spent too long.

I want to enter a file name in cell B3 click a macro button that says search, and it finds the file and opens it.
Or it can list the file and they can click that. but the first option I think better for user.

Specifics.
User enters file name such as 987654 into cell B3, they then click a button, the button searches "F:\Invoices" folder for the file name and opens it for them.......it is a .pdf file btw.

Thank you ahead of time........and I am still searching.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try this, changing the path to Adobe Reader (AcroRd32.exe) to its path on your computer.

Code:
Public Sub Open_PDF_File()

    Const cAdobeReaderExe As String = "C:\path\to\AcroRd32.exe"

    Dim PDFfile As String
    
    PDFfile = "F:\Invoices\" & Range("B3").Value & ".pdf"
    If Dir(PDFfile) <> vbNullString Then
        Shell cAdobeReaderExe & " " & Chr(34) & PDFfile & Chr(34), vbNormal
    Else
        MsgBox PDFfile & " not found."
    End If

End Sub
 
Upvote 0
Great Thanks!
It found the file but would not open.
I bolded and made red the line that degug found something.
And...additionally, is there anyway to make it where it would find it in a subfolder as well? The subfolder may be unknown at this time and could be different. example
F:\Invoices\Hedge Trimming\
Where hedge trimming will be one of about 30 things later

Const cAdobeReaderExe As String = "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader"

Dim PDFfile As String

PDFfile = "F:\Invoices" & Range("B3").Value & ".PDF"
If Dir(PDFfile) <> vbNullString Then
Shell cAdobeReaderExe & " " & Chr(34) & PDFfile & Chr(34), vbNormal

Else
MsgBox PDFfile & " not found."
End If
 
Last edited:
Upvote 0
And...additionally, is there anyway to make it where it would find it in a subfolder as well? The subfolder may be unknown at this time and could be different. example
F:\Invoices\Hedge Trimming\
Where hedge trimming will be one of about 30 things later
Is it just 1 level of subfolder?

Const cAdobeReaderExe As String = "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader"
You need the .exe program as well, as shown in my example, maybe like this:

Code:
Const cAdobeReaderExe As String = "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe
 
Upvote 0
Is it just 1 level of subfolder?

You need the .exe program as well, as shown in my example, maybe like this:

Code:
Const cAdobeReaderExe As String = "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe

Yes....it is just one level subfolder......and thanks s much for spending the time on this.
It will be like;
F:\Invoices\hedge trimming\("b3"invoice number)
F:Invoices\pine straw\("b3"invoice number)
F:\Invoices\Cleanup\("b3"invoice number)

I plan to add more things in future so don't always know what they will be.
thanks
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,382
Members
448,889
Latest member
TS_711

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