Hi,
I rescently had a similar problem solved for me in this forumn, all the files I wished to open with the button were saved in a folder on my desktop 'ProcesSpecifications' this folder contains loads of other folders containing .doc/.pdf/.xls etc etc. please see code below:
Private Sub CommandButton1_Click()
'Process Specifications
Dim varFNamePath As Variant
Dim colFoundFiles As New Collection
Dim strFilter As String
Dim strPath As String
Dim myRow As Long
strPath = "C:\Documents and Settings\user\Desktop\ProcessSpecifications"
strFilter = "*" & Sheets("Sheet1").Range("E17").Value & "*.*"
Call FileSearch(colFoundFiles, strPath, strFilter, True)
If colFoundFiles.Count = 0 Then
MsgBox "No file was found!"
Exit Sub
End If
' Opens first file found
'ActiveWorkbook.FollowHyperlink Address:=colFoundFiles(1)
'Application.CommandBars("Web").Visible = False
' Opens all files found
'For Each varFNamePath In colFoundFiles
' ActiveWorkbook.FollowHyperlink Address:=varFNamePath
' Application.CommandBars("Web").Visible = False
'Next varFNamePath
' Creates hyperlinks to all files found in Column A
myRow = 1
Range("A:A").Clear
For Each varFNamePath In colFoundFiles
Sheets("Sheet1").Hyperlinks.Add Anchor:=Range("A" & myRow), Address:=varFNamePath _
, TextToDisplay:=varFNamePath
myRow = myRow + 1
Next varFNamePath
End Sub
Private Sub FileSearch(colFoundFiles As Collection, strPath As String, _
strMask As String, blnIncSubDir As Boolean)
Dim strDirFile As String
Dim varColItem As Variant
Dim colSubDir As New Collection
strPath = Trim(strPath)
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
strDirFile = Dir(strPath & strMask)
Do While strDirFile <> ""
colFoundFiles.Add strPath & strDirFile
strDirFile = Dir
Loop
If Not blnIncSubDir Then Exit Sub
strDirFile = Dir(strPath & "*", vbDirectory)
Do While strDirFile <> ""
If strDirFile <> "." And strDirFile <> ".." Then If ((GetAttr(strPath & strDirFile) And _
vbDirectory) = 16) Then colSubDir.Add strPath & strDirFile
strDirFile = Dir
Loop
For Each varColItem In colSubDir
Call FileSearch(colFoundFiles, CStr(varColItem), strMask, blnIncSubDir)
Next
End Sub
You would have to change the line that appertains to where you want to search
strPath = "C:\Documents and Settings\user\Desktop\ProcessSpecifications"
you will also see the below reference:-
strFilter = "*" & Sheets("Sheet1").Range("E17").Value & "*.*"
the ("E17") referes to the cell number in which I type the file name or part file name of the file I wish to search for befor clicking the button to search, works brilliantly for me. I'm not a VBA enabled person and if there is any problems with the coding for you I will probably not be able to help sort. As I said, works perfectly for me,
Good Luck,
Andy.
Bookmarks