Open a pdf file based on partial text of a cell value in Excel VBA

Cakz Primz

Board Regular
Joined
Dec 4, 2016
Messages
102
Office Version
  1. 365
Platform
  1. Windows
Dear all,

I have cell value in cell K3 "TEP-TGH-MRR-CI-00460" based on this cell value, I would like to open a pdf file in certain folder only if the pdf file name start with K3 cell value.
The code below was found in internet, and it works ONLY if the pdf file name is exactly the same with K3 cell value.

How can I open the pdf file if the pdf file name started with cell K3 value?


VBA Code:
Sub OpenPDF()
    ' Declare variables
    Dim filePath As String
    Dim cellValue As String

    ' Get the value of the cell
    cellValue = Range("K3").Value
    
    ' Specify the folder path
    filePath = "C:\"
    'filePath = "P:\7.LOG&MATERIAL\MATERIAL CONTROL\06-Warehousing\00 -Document Register\02 FINAL DOCUMENT\03-MRR\"

    ' Construct the full file path
    filePath = filePath & cellValue & ".pdf"

    ' Check if the file exists
    If Dir(filePath) = "" Then
        MsgBox "The file " & cellValue & ".pdf" & " does not exist in the folder."
    Else
        ' Open the file
        ActiveWorkbook.FollowHyperlink filePath
    End If
End Sub

Thank you very much in advance.
prima - Indonesia
 

Attachments

  • K3 cell value.png
    K3 cell value.png
    4.9 KB · Views: 18

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Code:
Sub Open_PDF()
Dim pth As String, fName As String
pth = "C:\Test"    '<---- Change as required
fName = Dir(pth & "\*" & [K3] & "*.pdf")
ActiveWorkbook.FollowHyperlink pth & "\" & fName
End Sub
 
Upvote 0
Solution
Code:
Sub Open_PDF()
Dim pth As String, fName As String
pth = "C:\Test"    '<---- Change as required
fName = Dir(pth & "\*" & [K3] & "*.pdf")
ActiveWorkbook.FollowHyperlink pth & "\" & fName
End Sub
Dear jolivanes,

Your code is working well.
Problem solved.
Thanks for everything.

Best regards,
prima - Indonesia

note:
How is the code to open a pdf file based on ActiveCell.Value (selected cell) ?
 
Upvote 0
Dear jolivanes,

Your code is working well.
Problem solved.
Thanks for everything.

Best regards,
prima - Indonesia

note:
How is the code to open a pdf file based on ActiveCell.Value (selected cell) ?
Dear jolivanes,

Problem solved, based on your code, I modified as below. And it works.
Again, thank you very much for your kind assistance and help.
VBA Code:
Sub Open_PDFx()
Dim pth As String, fName As String
curVal = ActiveCell.Value

pth = "C:\"    '<---- Change as required
fName = Dir(pth & "\*" & curVal & "*.pdf")
ActiveWorkbook.FollowHyperlink pth & "\" & fName
End Sub

Best regards,
Prima - Indonesia
 
Upvote 0
Terima kasih

Are you sure this works?
Code:
pth = "C:\"    '<---- Change as required
Looks like the backslash is not needed as you have it later in the code.
 
Upvote 0
Terima kasih

Are you sure this works?
Code:
pth = "C:\"    '<---- Change as required
Looks like the backslash is not needed as you have it later in the code.
Dear jolivanes,

Sure, it worked. I applied and ran the code, already.
The first time, I've tried based on C: as trial
But the actual drive is in share drive as below:


Code:
Sub Open_PDF2()

Dim pth As String, fName As String
curVal = ActiveCell.Value

pth = "P:\7.LOG&MATERIAL\MATERIAL CONTROL\06-Warehousing\00 -Document Register\02 FINAL DOCUMENT\03-MRR\"
'fName = Dir(pth & "\*" & [K3] & "*.pdf")
fName = Dir(pth & "\*" & curVal & "*.pdf")
ActiveWorkbook.FollowHyperlink pth & "\" & fName

End Sub

Thank you,
prima - Indonesia
 
Upvote 0
Hi @jolivanes

I am quite new to vba and syntax is very difficult to understand (no offence !) compared to my experience in R and Python
below are my file details :

> I want to open file from the same folder where xlsm file is saved
> I have file names in Column A, and actual file names have additional characters and .pdf extension
such as xx-yy-nn-xxx-nnnnn will be cell value and related file name will be xx-yy-nn-xxx-nnnnn_0001_2-a.pdf
> the content of cell in column A becomes hyperlink if it matches the file available in the folder OR
a text like "Open File" becomes visible with hyperlink to file if the cell content matches with file available in the folder

I tried Cakz_Primz's code by inserting in the module but cant understand how it works and where the link will be.
also, is there any setting need to be changed to enable refreshing the xlsm file ? I have already enabled some settings to allow contents but still something dont seem working . . .
I have modified code and declared pth as below
VBA Code:
Sub Open_PDFx()
Dim pth As String, fName As String
curVal = ActiveCell.Value

pth = Application.ActiveWorkbook.Path       '<---- Change as required
fName = Dir(pth & "\*" & curVal & "*.pdf")
ActiveWorkbook.FollowHyperlink pth & "\" & fName
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,939
Messages
6,175,532
Members
452,652
Latest member
eduedu

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