Open a pdf file based on partial text of a cell value

kamranyd

Board Regular
Joined
Apr 24, 2018
Messages
152
Office Version
  1. 2021
Platform
  1. Windows
It open pdf based on cell value in cell F10. I would like to open a pdf file in certain folder only if the pdf file name start with F10 cell value. Below code work fine but works ONLY if the pdf file name is exactly the same in F10 cell value.

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


These codes open pdf file based on cell value, I need these codes open pdf who has numbers

VBA Code:
Sub inv_PDF_File()

   Dim FileNameRef As String, Prompt As String, myPath As String
   Dim ret As Variant
  
      FileNameRef = Application.WorksheetFunction.Text(Range("F10"), "0000")
      myPath = "C:\"
    
    On Error Resume Next
    ThisWorkbook.FollowHyperlink myPath & FileNameRef & ".pdf"
    
    If Err.Number <> 0 Then
    Prompt = "Invoice Number [-]> " & FileNameRef & " <[-]" & vbNewLine & _
    "NOT FOUND IN BELOW LOCATION OR LOCATION DOESN'T EXIT." & vbNewLine & _
    "Click to explore folder location C:\"
  
    Const TITLE = "PDF file Search"
    Const BUTTONS = vbExclamation
            
    Dim lRet As VbMsgBoxResult
    Dim sLinksArray(0 To 0, 1) As String
  
    sLinksArray(0, 0) = "C:\":            sLinksArray(0, 1) = "C:\"
    
    lRet = HyperlinkMsgBox(Prompt, sLinksArray, BUTTONS, TITLE)
    
End If
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I would like to open a pdf file in certain folder only if the pdf file name start with F10 cell value.

Incorporate this into your code:
VBA Code:
    Dim PDFfile As String
    
    PDFfile = Dir(myPath & Range("F10").Value & "*.pdf")
    If PDFfile <> vbNullString Then
        ThisWorkbook.FollowHyperlink myPath & PDFfile
    End If
 
Upvote 0
Incorporate this into your code:
VBA Code:
    Dim PDFfile As String
   
    PDFfile = Dir(myPath & Range("F10").Value & "*.pdf")
    If PDFfile <> vbNullString Then
        ThisWorkbook.FollowHyperlink myPath & PDFfile
    End If
Thanks for help, please if you show how to incorporate with my codes, I am trying but nothing happen. thanks once again
 
Upvote 0
Like this:
VBA Code:
Sub inv_PDF_File()

    Dim FileNameRef As String, Prompt As String, myPath As String
    Dim ret As Variant
    Dim PDFfile As String
    
    FileNameRef = Range("F10").Value
    myPath = "C:\"
    PDFfile = Dir(myPath & FileNameRef & "*.pdf")
    If PDFfile <> vbNullString Then
        ThisWorkbook.FollowHyperlink myPath & PDFfile
    Else
        Prompt = "Invoice Number starting with [-]> " & FileNameRef & " <[-]" & vbNewLine & _
        "NOT FOUND IN BELOW LOCATION OR LOCATION DOESN'T EXIT." & vbNewLine & _
        "Click to explore folder location C:\"
      
        Const TITLE = "PDF file Search"
        Const BUTTONS = vbExclamation
                
        Dim lRet As VbMsgBoxResult
        Dim sLinksArray(0 To 0, 1) As String
      
        sLinksArray(0, 0) = "C:\":            sLinksArray(0, 1) = "C:\"
        
        ret = HyperlinkMsgBox(Prompt, sLinksArray, BUTTONS, TITLE)
    End If
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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