VBA to open a PDF file

JST013

Board Regular
Joined
Mar 11, 2015
Messages
74
So...I was going to try to do this with hyperlinks within my spreadsheet...but I was wondering first if there was an easier method..

I will know the location and names of the file....

I will probably have some userform that will take a name and add & ".pdf" to it and open that file name...

Is this something that can work or should I just stick with what I know...
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
In case anyone else cares...haha...

I set up a condition where if the file isn't found, give an error message. so...this is how i'm using it...I will possibly have it try to look in a different spot if it doesn't find it but I haven't gotten that far...for now it's just going to be an error message


Code:
Private Sub btnView_Click()
    Dim x As String
    With Me.ListBox1
        For i = 0 To .ListCount - 1
            If .Selected(i) Then
'I'm using some variation in the column order, but its not important for this so i took that out...thats why this looks a little strange
            x = Me.ListBox1.Column(1)
            End If
           Next i  
        Dim pdfPath As String
'x for variable file name
        pdfPath = "S:\JACM Adjustment Records\JACM Die Maintenance\Assembly Die Maintenance\" & x & ".pdf"
        Call OpenAnyFile(pdfPath)   
    End With
End Sub

Function OpenAnyFile(strPath As String)
  Set objShell = CreateObject("Shell.Application")
    If FileThere(strPath) Then
        objShell.Open (strPath)
    Else
        MsgBox ("File not found")
    End If  
End Function

Function FileThere(FileName As String) As Boolean
     If (Dir(FileName) = "") Then
        FileThere = False
     Else:
        FileThere = True
     End If
End Function



thanks again RatExcel
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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