Script is not opening PDF files.

HECGroups

Board Regular
Joined
Jan 16, 2012
Messages
164
Hello Again,

I have adopted below code which I have found on the web but it is not working.

What I am doing here is trying to do here is searching specific or part of file name of a PDF and open.

Uptill searching is working but it is not opening the PDF file.

Below is the script. any help is appreciated.

Code:
Option Explicit
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
                    ByVal hwnd As Long, _
                    ByVal lpOperation As String, _
                    ByVal lpFile As String, _
                    ByVal lpParameters As String, _
                    ByVal lpDirectory As String, _
                    ByVal nShowCmd As Long) As Long
Private Const SW_HIDE As Long = 0
Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWMINIMIZED As Long = 2

'************************************************************************************************************************

Sub openfile()

Dim strpath As String
Dim openfile As Variant
Dim FileToDiag  As FileDialog
Dim fileSelected, RunBookCrtF

ChDrive "C:\"
ChDir Range("E1").Value '"C:\testing\"   'change to your own folder name
openfile = Application.GetOpenFilename(Title:="File to open", _
InitialFileName:=Range("E1").Value & Application.PathSeparator & "*" & Range("E3").Value & "*.PDF", _
FileFilter:="PDF Files *.pdf (*.pdf),")
Set openfile = Application.FileDialog(msoFileDialogOpen) 'Application.FileDialog(msoFileDialogFilePicker)
With openfile
    .AllowMultiSelect = False
    .InitialFileName = Range("E1").Value & Application.PathSeparator & "*" & Range("E3").Value & "*.PDF"
    .ButtonName = "Open Selection"
    .Title = "Select PDF File To Open"
    .Filters.Add "...", "*.pdf (*.pdf)", 1
    .Show
 
    For Each fileSelected In .SelectedItems
        RunBookCrtF = fileSelected
    Next
   
End With

If openfile = False Then
MsgBox "No file specified.", vbExclamation, "Bamm!"
Exit Sub
Else
End If
    ShellExecute Application.hwnd, "open", openfile, vbNullString, "C:\", SW_SHOWMAXIMIZED 'SW_SHOWNORMAL

End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Ok, please give an example of the file name the code would need to open as well as the variables in cells E1 and E3
 
Upvote 0
Ok I will prepare a sample file. But the above script is opening the folder, and suggesting the initial file name but not opening the PDF file.
 
Upvote 0
Code:
Ok, please give an example of the file name the code would need to open as well as the variables in cells E1 and E3

Filename as *Malaz*
E1 is the pat of the folder
E3 is the part of the PDF file name or full name of the PDF file
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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