Macro for opening a pdf

bamaisgreat

Well-known Member
Is it possible to have a macro in my personal workbook that will open a pdf named job list thats in a certain folder.
 

Leith Ross

Well-known Member
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
 

bamaisgreat

Well-known Member
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
 

John_w

MrExcel MVP
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
 

Some videos you may like

This Week's Hot Topics

  • Get External Data (long shot question!)
    This is likely a long shot but I am wondering if it is at all possible for Excel to somehow 'change' the contents of a URL that is being linked to...
  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • Cell Formatting
    Good Morning, I need to format a few different cells in the following manners: A1 has to always add a colon (:) after whatever is typed in by a...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • Workbook_Change stopped working !
    I am working on an app to speed up & automate processing of Credit Cards statements. After data is input from a CSV file, it is presented to the...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
Top