Find multiple files with options to open.

JasonRae

New Member
Joined
Feb 13, 2013
Messages
7
Hi,

First time poster, many time reader. I have a macro to find me a document from a certain details, but in some circumstances their may be multiple applicable documents, I know nothing of user forms, but how to I find all the documents, show their "modified date", "Name","File type" and select one or which several to open.

My current code:
Code:
[/COLOR]Private Sub OpenPDF()
Dim Ans
Dim TheFile As String
Dim ThePath As String, LocationD
Dim R
  R = ActiveCell.Row
ThePath = "H:folders"


Ans = MsgBox("Do you want the PDF? (no gives excel)", vbYesNo, "PDF or Excel")


If Ans = vbYes Then
    TheFile = Dir(ThePath & "*" & customersname & "*" & ".pdf")
    LocationD = ThePath & TheFile
    ElseIf Ans = vbNo Then
    TheFile = Dir(ThePath & "*" & customername & "*" & ".xlsm")
    LocationD = ThePath & TheFile
End If
ActiveWorkbook.FollowHyperlink LocationD


End Sub
[COLOR=#333333]

To have something more like a form popup showing for a search within a folder for files containing 'Brians Store':
Open? Name Date
O "Quote Brians Store - ProductsA.xlsm" 1/2/12
O "Quote Brians Store - ProductsB.xlsm" 1/1/12
O "Quote Brians Store - ProductsA.xlsm" 1/5/11
O "Quote Brians Store - ProductsC.xlsm" 1/4/11

And I may want to open the 1st, 2nd and last files.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
You can use the FileDialog object. Check it out in VBA Help.
Ok so this is what I came up with. But because there is a huge list, I want to filter by a particular customer, but it isn't excepting that filter option. Any ideas?
Code:
 Sub MainTESTTEST()    'Declare a variable as a FileDialog object.
    Dim fd As FileDialog, Name As String, FType As String, R As Integer
    R = ActiveCell.Row
    Name = Range("D" & R).Value
    FType = ".pdf"
    'Create a FileDialog object as a File Picker dialog box.
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    
    'Declare a variable to contain the path
    'of each selected item. Even though the path is aString,
    'the variable must be a Variant because For Each...Next
    'routines only work with Variants and Objects.
    Dim vrtSelectedItem As Variant
    
    fd.InitialFileName = "H:\Orders\Quotes\"
    
    'Use a With...End With block to reference the FileDialog object.
    With fd
        'Allow the selection of multiple files.
        fd.AllowMultiSelect = True
        'Add a filter that includes GIF and JPEG images and make it the first item in the list.
        .Filters.Add "PDF", "*.pdf", 3
        .Filters.Add "EXCEL", "*.xlsm; *.xls", 2
        .Filters.Add "Name", "*" & Name & "*.pdf", 1


        'Use the Show method to display the File Picker dialog box and return the user's action.
        'The user pressed the button.
        If .Show = -1 Then


            'Step through each string in the FileDialogSelectedItems collection.
            For Each vrtSelectedItem In .SelectedItems
                '.Title
                '.Item
                'vrtSelectedItem is aString that contains the path of each selected item.
                'You can use any file I/O functions that you want to work with this path.
                'This example displays the path in a message box.
                MsgBox "The path is: " & vrtSelectedItem


            Next vrtSelectedItem
        'The user pressed Cancel.
        Else
        End If
    End With


    'Set the object variable to Nothing.
    Set fd = Nothing


End Sub
 
Upvote 0
A Filter is only for the extension (file type) not for the name. You can use the InitialFileName property to filter by name, but only for one file type.
 
Upvote 0
Got it! Just would like to be able to define which columns I see and it would be perfect. Thanks so much. Very elegant solution!
Code:
Sub OpenPDF()    'Declare a variable as a FileDialog object.
    Dim fd As FileDialog, Name As String, FType As String, R As Integer, vrtSelectedItem As Variant
    R = ActiveCell.Row
    Name = Range("D" & R).Value
    FType = ".pdf"
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    
    fd.InitialFileName = "H:\Orders\Quotes\*" & Name & "*"
    fd.Title = "Please choose which Quotes(s) to open"
    fd.InitialView = msoFileDialogViewDetails
    'Use a With...End With block to reference the FileDialog object.
    With fd
        'Allow the selection of multiple files.
        fd.AllowMultiSelect = True
        .Filters.Add "PDF", "*.pdf", 1
        .Filters.Add "Excel", "*.xlsm; *.xls", 1
        .Filters.Add "All", "*.*", 1


        'Use the Show method to display the File Picker dialog box and return the user's action.
        'The user pressed the button.
        If .Show = -1 Then


            'Step through each string in the FileDialogSelectedItems collection.
            For Each vrtSelectedItem In .SelectedItems
                'MsgBox "The path is: " & vrtSelectedItem
                ActiveWorkbook.FollowHyperlink vrtSelectedItem
            Next vrtSelectedItem
        Else
            'The user pressed Cancel.
        End If
    End With
    'Set the object variable to Nothing.
    Set fd = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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