1

I am using the version of Excel in Microsoft Office Professional Plus 2013.

Using Excel VBA, I would like to launch Adobe Reader XI and open a pdf file located in another folder.

I can successfully open the Adobe Reader file if the Excel file and the Adobe Reader file are in the same folder. Here’s the code that works:

Dim ABCfilename As String
Dim returnAcrobatfile As Variant
ABCfilename = "ABC.pdf"
acrobatFile = ThisWorkbook.Path & Application.PathSeparator & ABCfilename
returnAcrobatfile = Shell("C:\Program Files\Adobe\Reader _
11.0\Reader\AcroRd32.exe " & acrobatFile, vbNormalFocus)

However, I would like to launch Adobe Reader and program it to open a pdf file that is located in a different folder.

My Excel file is in a folder named C:\Customers\Pricing\

My Adobe file is in a folder named Z:\XYZ Company\

How should I modify the line of code that starts with returnAcrobatfile so that it opens a pdf file located in Z:\XYZ Company ?

Paul
  • 11
  • 1
  • 3

1 Answers1

1

see code below.

Sub PDF_Picker()

Dim Acrobatfile         As String

Acrobatfile = GetFile
Runit (Acrobatfile)

End Sub

Sub Runit(FileName As String)
   Dim Shex As Object
   Dim tgtfile As String
   Set Shex = CreateObject("Shell.Application")
   tgtfile = FileName
   Shex.Open (tgtfile)
End Sub

Function GetFile()

    With Application.FileDialog(msoFileDialogFilePicker)
        .InitialFileName = ActiveWorkbook.Path & Application.PathSeparator
        .AllowMultiSelect = False
        .Title = "Select PDF Files"
        .Filters.Clear
        .Filters.Add "Adobe PDF Files", "*.pdf"
        .InitialView = msoFileDialogViewDetails
        If .Show = True Then
            GetFile = .SelectedItems(1)
        Else
            GetFile = False
        End If
    End With

End Function
  • If your client has Acrobat installed in a different location from the path you hardcoded your solution will fail. I adapted it to make it more robust and use default application choice for PDF. BTW if you drop/delete the filter in the function GetFile: (.Filters.Add "Adobe PDF Files", "*.pdf") you can USE THIS TO OPEN ANY FILE even mp3s as I've just tested.
  • Credit to tigeravatar, I adapted his function for your purposes from this post: Importing folder to Excel (FileDialogFolderPicker) using VBA
  • Credit to user1302114 adapted RunIt Sub from this post: How can Excel VBA open file using default application
Community
  • 1
  • 1
gokool108
  • 56
  • 3