Open folder with search value

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Open folder with search value

Post by adam »

Following is the actual folder I want to open which is located in a server.
\\myserver\My Folder Documents\

I need to open the the documents in this folder.
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Open folder with search value

Post by HansV »

Not in its subfolders?
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Open folder with search value

Post by adam »

Yeah
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Open folder with search value

Post by HansV »

I'm sorry - what exactly do you mean by "yeah"?
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Open folder with search value

Post by adam »

by "yeah" I meant I want the code to open the documents with any extension such as .pdf, .xls, xlsm, .doc that is located in the folder named "My Folder Documents" which is located in a server named as "myserver"

the path for the documents in the folder is as follows
\\myserver\My Folder Documents\

I hope I've made my question clear.

Thanks in advance.
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Open folder with search value

Post by HansV »

In that case, your code is overkill. See if the attached version works for you.

Search Folder.xlsm
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Open folder with search value

Post by adam »

Thanks for the help Hans. Where can I place a message in the existing code where ehen the user searches a document or text that is not available in the folder it gives a message saying the document is not available.

Any help would be kindly appreciated.
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Open folder with search value

Post by HansV »

Change CommandButton3_Click as follows:

Code: Select all

Private Sub CommandButton3_Click()
    Dim FileFilter As String
    Dim MyFile As String
    FileFilter = Me.SearchTextBox
    If FileFilter = "" Then
        Me.SearchTextBox.SetFocus
        MsgBox "Please enter a search text, then try again.", vbInformation
        Exit Sub
    End If
    Me.ListBox1.Clear
    FileFilter = "*" & FileFilter & "*"
    MyFile = Dir(MyPath & FileFilter)
    Do While MyFile <> ""
        Me.ListBox1.AddItem MyFile
        MyFile = Dir
    Loop
    If Me.ListBox1.ListCount = 0 Then
        MsgBox "There are no files matching the search text!", vbInformation
    End If
End Sub
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Open folder with search value

Post by adam »

Thankyou so much Hans. It worked really well.
Best Regards,
Adam

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Open folder with search value

Post by adam »

I've added a second textbox txt.date.value to my userform.

The purpose of this is to allow the user to search folder items by date and name.

For example if the user types date 20-06-2022 in textbox txt.date.value and file name apples in Me.SearchTextBox, I want the code to filter the file with that particular name and date.

How may I achieve this?
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Open folder with search value

Post by HansV »

I am away from my computer at the moment. If nobody else replies, I will look at it later.
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Open folder with search value

Post by adam »

Thanks in advance.
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Open folder with search value

Post by HansV »

I'll use txtDate as name of the text box, for txt.date is not a valid name for a text box: point is not allowed.

Try this:

Code: Select all

Private Sub CommandButton3_Click()
    Dim FileFilter As String
    Dim MyFile As String
    FileFilter = Me.SearchTextBox
    If FileFilter = "" Then
        Me.SearchTextBox.SetFocus
        MsgBox "Please enter a search text, then try again.", vbInformation
        Exit Sub
    End If
    Me.ListBox1.Clear
    FileFilter = "*" & FileFilter & "*"
    MyFile = Dir(MyPath & FileFilter)
    Do While MyFile <> ""
        If Int(FileDateTime(MyPath & FileFilter)) = CLng(Me.txtDate) Then
            Me.ListBox1.AddItem MyFile
        End If
        MyFile = Dir
    Loop
    If Me.ListBox1.ListCount = 0 Then
        MsgBox "There are no files matching the search text!", vbInformation
    End If
End Sub
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Open folder with search value

Post by adam »

Thanks for the help Hans.
Best Regards,
Adam