VBA command to open PDF

Adnan_razaq

New Member
Joined
Nov 14, 2019
Messages
11
Hi All,

my first post here :)

I am trying to create an interface to select and open PDF reports from Excel.

Currently I have got 4 drop down boxes where users can select which report they would like to see.

Currently the options are

drop down box 1 : Europe, America and Pacific
drop down box 2 : Red, Blue and Green
drop down box 3: months of year and
drop down box 4: years from 2015-19

once a users selects the above options, I would like to create a button based on the user choices to open a PDF report in a shared folder

for instance, if a user selects Europe, Red, and Jun 2019 the appropriate report will open

Many Thanks in advance
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
5,997
Welcome to MrExcel forums.

You haven't said exactly how the PDF file name should be constructed from the 4 drop down values, but see if this macro works for you. The 4 drop downs are Form Control Combo Boxes placed on a worksheet and with their default names: "Drop Down 1", "Drop Down 2", etc. Edit the code to change the PDFsfolder string, which is the folder containing the PDF files. Assign the following macro to the command button on the worksheet:

Code:
Option Explicit

[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  VBA7 Then
    Private Declare PtrSafe Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" _
       (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
    Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" _
       (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If


Public Sub Open_PDF()

    Dim DropDowns(1 To 4) As DropDown
    Dim PDFsfolder As String
    Dim PDFfileName As String
    Dim PDFexe As String
    
    PDFsfolder = "C:\path\to\PDFs"               'folder containing PDF files
    If Right(PDFsfolder, 1) <> "" Then PDFsfolder = PDFsfolder & ""
    
    Set DropDowns(1) = ActiveSheet.Shapes("Drop Down 1").OLEFormat.Object
    Set DropDowns(2) = ActiveSheet.Shapes("Drop Down 2").OLEFormat.Object
    Set DropDowns(3) = ActiveSheet.Shapes("Drop Down 3").OLEFormat.Object
    Set DropDowns(4) = ActiveSheet.Shapes("Drop Down 4").OLEFormat.Object
    
    If DropDowns(1).ListIndex + DropDowns(2).ListIndex + DropDowns(3).ListIndex + DropDowns(4).ListIndex >= 4 Then

        PDFfileName = Range(DropDowns(1).ListFillRange).Item(DropDowns(1).ListIndex).Value & " " & _
                      Range(DropDowns(2).ListFillRange).Item(DropDowns(2).ListIndex).Value & " " & _
                      Range(DropDowns(3).ListFillRange).Item(DropDowns(3).ListIndex).Value & " " & _
                      Range(DropDowns(4).ListFillRange).Item(DropDowns(4).ListIndex).Value & ".pdf"
                      
        If Dir(PDFsfolder & PDFfileName) <> vbNullString Then
            PDFexe = Get_ExePath(PDFsfolder & PDFfileName)
            Shell PDFexe & " " & Chr(34) & PDFsfolder & PDFfileName & Chr(34), vbNormalFocus
        Else
            MsgBox PDFsfolder & PDFfileName & " not found", vbExclamation
        End If
    End If
    
End Sub


Private Function Get_ExePath(lpFile As String) As String
    Dim lpDirectory As String, sExePath As String, rc As Long
    lpDirectory = ""
    sExePath = Space(255)
    rc = FindExecutable(lpFile, lpDirectory, sExePath)
    Get_ExePath = Left$(sExePath, InStr(sExePath, Chr$(0)) - 1)
End Function
 

Forum statistics

Threads
1,081,845
Messages
5,361,663
Members
400,643
Latest member
RockStar89

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top