locked
how to search for file name (*.doc) and text into (*.doc) and show result in ListBox ? RRS feed

  • Question

  • how to :

    1. search for file name (*.doc) .
    2. search for text into (*.doc) .
    3. show result in ListBox .
    4. open File after Double click into ListBox .
    Monday, January 27, 2014 4:34 PM

Answers

  • Hi,

    Welcome to MSDN.

    What do you mean by "search for text into (*.doc) ."? Do you mean that search .doc file from the path which stored in that text?

    I created a simple sample, you could have a look.

    Select the folder to search:

     Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
            Dim dlgFolderBrwsr As New FolderBrowserDialog
            With dlgFolderBrwsr
                .ShowNewFolderButton = False
                .Description = "Choose the folder to search"
            End With
            If dlgFolderBrwsr.ShowDialog() = Windows.Forms.DialogResult.OK Then
                txtFolder.Text = dlgFolderBrwsr.SelectedPath
            End If
        End Sub

    Search the .doc files form that folder :

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
            If txtFolder.Text.Length > 0 Then
                Dim allfiles() As String = Directory.GetFiles(txtFolder.Text, "*.doc", SearchOption.AllDirectories)
                ListBox1.Items.Clear()
                For Each strFile As String In allfiles
                    ListBox1.Items.Add(strFile)
                Next
            Else
                MsgBox("please choose the folder to search")
            End If
        End Sub

    Open the file by double clicking ListBox:

     Private Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick
            Dim lbList As ListBox = CType(sender, ListBox)
            If File.Exists(lbList.SelectedItem) Then
                Try
                    Dim p As New Process
                    Dim pi As New ProcessStartInfo
                    pi.FileName = lbList.SelectedItem
                    p.StartInfo = pi
                    p.Start()
                Catch ex As Exception
                    MsgBox(ex.ToString())
                End Try
               
            End If
        End Sub

    You could download it form : http://sdrv.ms/M8af2c

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Venkat786 Tuesday, January 28, 2014 2:34 PM
    • Marked as answer by srajmuneer Friday, January 31, 2014 5:47 PM
    Tuesday, January 28, 2014 6:50 AM
  • Hi,

    Glad to receive your reply.

    1. >>I mean (search for text into (*.doc)) search any part into (*.doc)  :<<

    As Cor suggested, that forum is more dedicated for this issue:

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vsto

    Since the way how to get the text of that doc file is one of these issues that forum supports.

    2. >>when choose (c:\) show error :<<

    According to that error, that folder need Administrator permissions, you could run VS as admin then test your project.

    3.>> I want search for a specified file , example : (tutorial.doc)<<

    What I suggested is just a simple sample, you could change it to the one fit your requirements.

    You could refer to Directory.GetFiles Method (String, String, SearchOption):

    change "*.doc" to "tutorial.doc" to test.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by srajmuneer Friday, January 31, 2014 5:46 PM
    Friday, January 31, 2014 7:12 AM

All replies

  • Windows search does this for you. But if you want to write VB for this, then you can easily search for each of your questions.

    Chuck

    Monday, January 27, 2014 10:31 PM
  • Hi,

    Welcome to MSDN.

    What do you mean by "search for text into (*.doc) ."? Do you mean that search .doc file from the path which stored in that text?

    I created a simple sample, you could have a look.

    Select the folder to search:

     Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
            Dim dlgFolderBrwsr As New FolderBrowserDialog
            With dlgFolderBrwsr
                .ShowNewFolderButton = False
                .Description = "Choose the folder to search"
            End With
            If dlgFolderBrwsr.ShowDialog() = Windows.Forms.DialogResult.OK Then
                txtFolder.Text = dlgFolderBrwsr.SelectedPath
            End If
        End Sub

    Search the .doc files form that folder :

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
            If txtFolder.Text.Length > 0 Then
                Dim allfiles() As String = Directory.GetFiles(txtFolder.Text, "*.doc", SearchOption.AllDirectories)
                ListBox1.Items.Clear()
                For Each strFile As String In allfiles
                    ListBox1.Items.Add(strFile)
                Next
            Else
                MsgBox("please choose the folder to search")
            End If
        End Sub

    Open the file by double clicking ListBox:

     Private Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick
            Dim lbList As ListBox = CType(sender, ListBox)
            If File.Exists(lbList.SelectedItem) Then
                Try
                    Dim p As New Process
                    Dim pi As New ProcessStartInfo
                    pi.FileName = lbList.SelectedItem
                    p.StartInfo = pi
                    p.Start()
                Catch ex As Exception
                    MsgBox(ex.ToString())
                End Try
               
            End If
        End Sub

    You could download it form : http://sdrv.ms/M8af2c

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Venkat786 Tuesday, January 28, 2014 2:34 PM
    • Marked as answer by srajmuneer Friday, January 31, 2014 5:47 PM
    Tuesday, January 28, 2014 6:50 AM
  • Getting the files is easy as others show, however, searching in the text is a not that easy. 

    Try for your question the Visual Studio For Office forum.

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vsto


    Success
    Cor

    Tuesday, January 28, 2014 8:03 AM
  • Thank you Carl Cai - MSFT ,

    1. I mean (search for text into (*.doc)) search any part into (*.doc)  :

    2. when choose (c:\) show error :

    3. I want search for a specified file , example : (tutorial.doc)

    Thursday, January 30, 2014 4:21 PM
  • Yes that is what I thought also did you see my link?

    You know a doc file is not a plain text file. It is a special kind of file with all kind of commands included in the format of the file.


    Success
    Cor


    Thursday, January 30, 2014 4:41 PM
  • Thank you Cor Ligthert , but your link is search for forums and The lack of a suitable answer to my problem
    • Edited by srajmuneer Thursday, January 30, 2014 9:26 PM
    Thursday, January 30, 2014 9:25 PM
  • Hi,

    Glad to receive your reply.

    1. >>I mean (search for text into (*.doc)) search any part into (*.doc)  :<<

    As Cor suggested, that forum is more dedicated for this issue:

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vsto

    Since the way how to get the text of that doc file is one of these issues that forum supports.

    2. >>when choose (c:\) show error :<<

    According to that error, that folder need Administrator permissions, you could run VS as admin then test your project.

    3.>> I want search for a specified file , example : (tutorial.doc)<<

    What I suggested is just a simple sample, you could change it to the one fit your requirements.

    You could refer to Directory.GetFiles Method (String, String, SearchOption):

    change "*.doc" to "tutorial.doc" to test.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by srajmuneer Friday, January 31, 2014 5:46 PM
    Friday, January 31, 2014 7:12 AM
  • Thank you very very much Carl Cai - MSFT
    Friday, January 31, 2014 5:47 PM