Macro to open pdf file based on a cell value.

martin61

New Member
Joined
Dec 17, 2011
Messages
5
How can I code a Macro in Excel to search in a preset directory and open a certain Adobe Acrobat .pdf file ?

Example: If cell L1 has a date of 5-Jan, I want it to open a pdf file called 5-Jan.pdf, which i can do with the following:

Sub Payslip()
MyPath = "C:\Program Files (x86)\Adobe\Reader 9.0\Reader\AcroRd32.exe"
MyFile = "C:\Users\Dads\Documents\Payslips\2012\5 Jan.pdf"
Shell MyPath & " " & MyFile, vbNormalFocus
End Sub

But when the date in cell L1 changes to say 12-Jan the above is no good.
I want it to open pdf file based on the value of cell L1.
Any help appreciated Thanks.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
If you want it to automatically trigger on change, you will need to build a selection change macro in the worksheet object.

I suggest using a switch case statement to choose which PDF to open, based on the value it finds in the target cell.

HTH.
 
Upvote 0
Hi GopherUK, thank you for ur reply.
I dont want it to automatically trigger on change, I want to click on a button to activate macro.
So if cell L1 has a value of 5-Jan when i click on button it opens pdf file named 5-Jan.pdf, or if L1 has value of 12-Jan click on button to open pdf file named 12-Jan.pdf

Thanks Again..
 
Upvote 0
Code:
Sub Payslip()

    MyPath = "C:\Program Files (x86)\Adobe\Reader 9.0\Reader\AcroRd32.exe"
    MyFile = "C:\Users\Dads\Documents\Payslips\2012\" & Range("L1") & ".pdf"
    Shell MyPath & " " & MyFile, vbNormalFocus

End Sub

That should do it. Insert a shape or something into the sheet then attach that macro to it.
 
Upvote 0
GopherUK thanks again for ur reply.
It trying to work, it opens acrobat but i get the following error message:
"there was a error opening this document" this file cannot be found.
Cell L1 shows value of "5 Jan" formated as Custom "d mmm"
Pdf file is named 5 Jan.pdf
Im not sure why it cant find it.
 
Upvote 0
This should work.

Code:
Sub Payslip()

    Dim FileNameRef As String
    
    FileNameRef = Application.WorksheetFunction.Text(Range("L1"), "d mmm")

    MyPath = "C:\Program Files (x86)\Adobe\Reader 9.0\Reader\AcroRd32.exe"
    MyFile = "C:\Users\Dads\Documents\Payslips\2012\" & FileNameRef & ".pdf"
    Shell MyPath & " " & MyFile, vbNormalFocus

End Sub
 
Upvote 0

Forum statistics

Threads
1,221,866
Messages
6,162,511
Members
451,772
Latest member
MohanadElnashar

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