Macro for opening a pdf

bamaisgreat

Well-known Member
Joined
Jan 23, 2012
Messages
831
Office Version
  1. 365
Platform
  1. Windows
Is it possible to have a macro in my personal workbook that will open a pdf named job list thats in a certain folder.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hello bamaisgreat,

Change the folder and file names in the macro below to what you will be using.

Copy the code and paste it into a new VBA Module in your Workbook.

Code:
Private Declare PtrSafe Function ShellExecute _
    Lib "Shell32.dll" Alias "ShellExecuteA" _
        (ByVal hwnd As LongPtr, _
         ByVal lpOperation As String, _
         ByVal lpFile As String, _
         ByVal lpParameters As String, _
         ByVal lpDirectory As String, _
         ByVal nShowCmd As Long) _
    As LongPtr


Sub OpenPDF()


    Dim args    As String
    Dim File    As String
    Dim Folder  As String
    Dim ret     As Long
    
        File = "Test.pdf"
        Folder = "C:\Users\Owner\Documents"
        args = "/A ""pagemode=none&toolbar=0&statusbar=0"""
        
        ret = ShellExecute(0&, "open", File, args, Folder, 1&)
        
End Sub
 
Upvote 0
Thanks for the help. I had been using the following.
Is it possible to have it open the only pdf file in the folder without knowing its name?

Code:
Sub openJOBLIST()
ThisWorkbook.FollowHyperlink "\\hq\dfsdata\Data\UserHomes\jamey.eerson\My Documents\pdf-NEW\Latest Job List\Job List.pdf"
End Sub
 
Upvote 0
Try this:
Code:
Public Sub openJOBLIST()

    Dim path As String
    Dim PDFfile As String
    
    path = "\\hq\dfsdata\Data\UserHomes\jamey.eerson\My Documents\pdf-NEW\Latest Job List\"
    If Right(path, 1) <> "\" Then path = path & "\"
    
    PDFfile = Dir(path & "*.pdf")
    If PDFfile <> vbNullString Then
        ThisWorkbook.FollowHyperlink path & PDFfile
    End If
    
End Sub
 
Upvote 0
Worked great.
Could you possibly edit the code where it opens the only pdf with GGS in the file name?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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