0

I´m trying to make excel open files other than xls (word files, powerpoint files and pdf files). I´m only succeding with excel files (for obvious reasons). Is there a way to make excel use the default program to open a file. For example, if i´m opening a .doc file it should use Word but if i´m opening a .pdf it should use Acrobat (or another pdf reader). I´m using hyperlinks. I will show you what I got so far:

I have a list (Listbox2 in this case) with a code to load all the files in a specific folder:

Private Sub UserForm_Initialize()
Dim MyFolder As String
Dim MyFile As String
Dim j As Integer
USER = TextBox99
MyFolder = "C:\Example"
MyFile = Dir(MyFolder & "\*.*")
Do While MyFile <> ""
    ListBox2.AddItem MyFile
    MyFile = Dir
Loop
End Sub

Let´s say that in the listbox2 I have the file word.doc, powerpoint.ppt, excel.xls and acrobat.pdf. If I choose the file from the listbox2 and press a button with the following code:

Private Sub CommandButton14_Click()
   Dim strFileandPath As String
   strFileandPath = "C:\Example\" & UserForm1.ListBox2.Column(0) 
   Workbooks.Open strFileandPath 
End Sub

It will only open the excel files. The other ones will get me an error. Is there a way to make Excel open any kind of file using the default program in the PC. Thanks for your attention!

Cheers

Dmitry Pavliv
  • 35,333
  • 13
  • 79
  • 80
RMAMDF
  • 39
  • 11
  • possible duplicate of [How can Excel VBA open file using default application](http://stackoverflow.com/questions/18921168/how-can-excel-vba-open-file-using-default-application) – Automate This Apr 06 '14 at 20:05

1 Answers1

0

I´ve made it to open pdf and word files but now I can´t open excel files. Credit goes to user1302114 (How can Excel VBA open file using default application).

The code I used:

    Dim Shex As Object
   Set Shex = CreateObject("Shell.Application")
   tgtfile = "C:\Example" & UserForm1.ListBox2.Column(0)
   Shex.Open (tgtfile)

I also enabled Acrobat and word libraries in Tools-Preferences

Community
  • 1
  • 1
RMAMDF
  • 39
  • 11