Opening PDF from VBA

smpatty08

Board Regular
Joined
May 16, 2014
Messages
155
I am trying to use the following code to open a pdf file from a folder, but I keep getting the "File not found" error.

Code:
Shell "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Adobe Acrobat DC.exe " & FullPath & """, vbMinimizedFocus"

Is there something I am missing? Below if the whole code if needed.

Code:
Sub OpenPDFs()
Dim C As Collection, C1 As Collection, C2 As Collection, C3 As Collection, C4 As Collection, subFolder As Variant, subFolder1 As Variant, _
subFolder2 As Variant, subFolder3 As Variant, sFolder As String, File As Variant, WB1 As Workbook, FullPath As String


Application.ScreenUpdating = False
'Sets Open workbook as WB1
Set WB1 = ActiveWorkbook
'Allows user to select Directory folder
sFolder = GetFolder()
'Sets all subfolders in Directory to Collection
Set C = GetFoldersIn(sFolder)
'Sets all subfolders of subfolder in Directory to Collection
For Each subFolder In C
    If subFolder = "." Or subFolder = ".." Then
    Else
'        Debug.Print "Folder Path " & sFolder & "\" & subFolder
        Set C1 = GetFoldersIn(sFolder & "\" & subFolder)
        For Each subFolder1 In C1
            If subFolder1 = "." Or subFolder1 = ".." Then
            Else
'                Debug.Print "Folder Path " & sFolder & "\" & subFolder & "\" & subFolder1
                Set C2 = GetFoldersIn(sFolder & "\" & subFolder & "\" & subFolder1)
                For Each subFolder2 In C2
                    If subFolder2 = "ParticleData" Then
'                        Debug.Print "Folder Path " & sFolder & "\" & subFolder & "\" & subFolder1 & "\" & subFolder2
                        Set C3 = GetFoldersIn(sFolder & "\" & subFolder & "\" & subFolder1 & "\" & subFolder2)
                        For Each subFolder3 In C3
                            If subFolder3 = "ParticleDataReports" Then
'                                Debug.Print "Folder Path " & sFolder & "\" & subFolder & "\" & subFolder1 & "\" & subFolder2 & "\" & subFolder3
                                Set C4 = GetFilesIn(sFolder & "\" & subFolder & "\" & subFolder1 & "\" & subFolder2 & "\" & subFolder3)
                                For Each File In C4
'                                    Debug.Print File
                                    FullPath = sFolder & "\" & subFolder & "\" & subFolder1 & "\" & subFolder2 & "\" & subFolder3 & "\" & File
                                    Debug.Print FullPath
'                                    Shell "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Adobe Acrobat DC.exe " & FullPath & """, vbMinimizedFocus"
                                Next File
                            End If
                        Next subFolder3
                    End If
                Next subFolder2
            End If
        Next subFolder1
    End If
Next subFolder


Application.ScreenUpdating = True
End Sub
Function GetFilesIn(Folder As String) As Collection
  Dim F As String
  Set GetFilesIn = New Collection
  F = Dir(Folder & "\*")
  Do While F <> ""
    GetFilesIn.Add F
    F = Dir
  Loop
End Function
Function GetFoldersIn(Folder As String) As Collection
  Dim F As String
  Set GetFoldersIn = New Collection
  F = Dir(Folder & "\*", vbDirectory)
  Do While F <> ""
    If GetAttr(Folder & "\" & F) And vbDirectory Then GetFoldersIn.Add F
    F = Dir
  Loop
End Function
Function GetFolder() As String
    Dim fldr As FileDialog
    Dim sItem As String
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With
NextCode:
    GetFolder = sItem
    Set fldr = Nothing
End Function
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I am trying to use the following code to open a pdf file from a folder, but I keep getting the "File not found" error.

Code:
Shell "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Adobe Acrobat DC.exe " & FullPath & """, vbMinimizedFocus"
You have some quotes in the wrong place and you need to put quotes around FullPath, like this:
Code:
Shell "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Adobe Acrobat DC.exe " & Chr(34) & FullPath & Chr(34), vbMinimizedFocus
Also, is that the correct path and exe? It's usually C:\Program Files (x86)\Adobe\.... and AcroRd32.exe or Acrobat.exe
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,692
Members
448,979
Latest member
DET4492

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