Find and print document based on cell reference

Ben171

Board Regular
Joined
Jul 2, 2021
Messages
88
Hi,

Say I have a name of a PDF file stored in Cell A1 of a sheet called "Database". e.g. 12345.pdf

Lets say this PDF is stored in the location C:/TestPDF/

I would like to create a macro, to search in the directory C:/TestPDF/ to find the pdf name located in cell A1 of the sheet "Database", and then to print this PDF.

Any help would be greatly appreciated!
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Bump,

How do i search a specific folder location through code "C:/TestPDF/, then to find the correct PDF based on the text in a cell? e.g. the cell may contain Version1, so it checks this file location for Version1.pdf
 
Upvote 0
Hi,

Give the following a try. It makes use of the Dir functinon, which among other things can determine if a file exists or not by looking at its output.
VBA Code:
    Dim filePath As String
    filePath = "C:/Temp/" & Me.Range("A1").Value & ".txt"
    If Dir(filePath, vbDirectory) <> vbNullString Then
        MsgBox "File " & filePath & " exists"
        ActiveWorkbook.FollowHyperlink filePath 'Opens the file, from this answer https://stackoverflow.com/a/19185602
    Else
        MsgBox "File " & filePath & "  does not exist"
    End If

If you want to open the file, then you could just do something along the lines of this little work around, but if you wanted to print as well, you'll likely have to invoke some shell commands in VBA - looking around after a quick google search there's a few answers on this already.
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

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