Find multiple files with options to open.

JasonRae

New Member
Joined
Feb 13, 2013
Messages
6
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.
 

Some videos you may like

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

JasonRae

New Member
Joined
Feb 13, 2013
Messages
6
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
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
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.
 

JasonRae

New Member
Joined
Feb 13, 2013
Messages
6
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
 

Watch MrExcel Video

Forum statistics

Threads
1,095,172
Messages
5,442,821
Members
405,198
Latest member
Florence Thomas

This Week's Hot Topics

  • Copy entire row if CountA <>0 to another sheet
    [B]I want to copy entire row if CountA <>0 for column J7:AM7 (headers on J6:AM6) and so on till the last used cell is column D and paste the...
  • Select last used Row in Table
    I have created a Table in a Worksheet which is locked to prevent user errors and protect formula. Some of the cells require freetext entries which...
  • excel workbook: do not allow certain file name
    Hello all, Don't think this has ever been asked before, but how do I restrict file save [Before_Save Event] if the name of the file being saved...
  • fixing problem autofilter
    hello i need help about my code when i search by code in textbox it doesn't show anything this is my data [ATTACH type="full"...
  • “Weight”
    Hi, i’ve got a long sheet filled with weights such as kg,g,L & ml. i can build a formula to convert kg into g and liter into ml. How ever, my...
  • How to capitalize everything before a certain character?
    In column A, I have some text: Hello good day.mp3 Hello good day.flac etc. I'd like to capitalize everything before the period. I don't need the...
Top