none
Finding and Listing Documents if thy Contain a String.

    Question

  • Morning Guys.

    I have put together a small app to make organic gardening tips easily exessable to me. Here is the complete code so far.

    Imports System.IO
    Public Class Form1
        Dim CompanionPlantingFolder As String = "F:\My Documents\Gardening\My Companion Planting Guide"
        Dim Files() As String = Directory.GetFiles(CompanionPlantingFolder)
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            CodeListBox.Items.Clear()
            For Each file As String In Files
                CodeListBox.Items.Add(Path.GetFileName(file))
            Next
        End Sub
        Private Sub CodeListBox_DoubleClick(sender As System.Object, e As System.EventArgs) Handles CodeListBox.DoubleClick
            Process.Start(Path.Combine(CompanionPlantingFolder, CodeListBox.SelectedItem.ToString))
            'Me.WindowState = FormWindowState.Minimized
        End Sub
        Private Sub StringSearchTextBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles StringSearchTextBox.TextChanged
            CodeListBox.Items.Clear()
            'This line of code removes case sensitivity.
            Dim SearchStr As String = StringSearchTextBox.Text.ToUpper
            If StringSearchTextBox.Text <> "" Then
                For Each file As String In Files
                    If file.ToUpper.Contains(SearchStr) Then
                        CodeListBox.Items.Add(Path.GetFileName(file))
                    End If
                Next
            Else
                MsgBox("No String Enterd")
            End If
        End Sub
        Private Sub ResetButton_Click(sender As System.Object, e As System.EventArgs) Handles ResetButton.Click
            CodeListBox.Items.Clear()
            For Each file As String In Files
                CodeListBox.Items.Add(Path.GetFileName(file))
            Next
        End Sub
    End Class

    As you can see, entering a string into the SearchTextbox results in the ListBox items being filterd acordingly.  And clicking one of the items will open the document.  

    What I need help with is as follow.

    As soon as an item is clicked and the document is opend I need the Listbox to be populated with all the documents that contains eny teference inside the same as the string in the SearchTextbox.


    Hendri Bissolati

    Sunday, December 01, 2013 5:39 AM

Answers

  • Since you are using Word documents then you probably need to use automating Microsoft Office application functionality to search your Word documents for strings. And of course if one is found then provide the appropriate information for that document to your ListBox.

    See this link How to: Search for Text in Documents and any links on the left side of the page or anything under Word Solutions that could help you out.

    And of course exclude the Word Document that you already have shown in your display.

    And maybe this code can assist you. And I'm not hacking a Wireless USB Antenna (as the text in the image below says) I'm adding an external antenna to a Linksys Wi-Fi USB dongle I have to increase its range significantly.

    For Each File In My.Computer.FileSystem.GetFiles("C:\Users\John\Desktop")
          Me.Text = File ' Do your code here I suppose by getting the file and performing whatever actions you are trying to do.
    Next

    Of maybe this code if you don't have only Word documents in your folder. Although the file extension .Doc may not be correct for Word documents as I didn't look up what it should be.

    For Each File In My.Computer.FileSystem.GetFiles("C:\Users\John\Desktop")
                If File.Contains(".Doc") Then
                    Me.Text = File ' Do your code here I suppose by getting the file and performing whatever actions you are trying to do.
                End If
    Next


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.



    Saturday, December 07, 2013 5:09 PM

All replies

  • As soon as an item is clicked and the document is opend I need the Listbox to be populated with all the documents that contains eny teference inside the same as the string in the SearchTextbox.

    This line is incorrect:
                    If file.ToUpper.Contains(SearchStr) Then

    It should be 
                    If file.ReadAllText.ToUpper.Contains(SearchStr) Then

    Sunday, December 01, 2013 6:06 AM
  • I have tryed yhis but get an error saing ReadAllText not a member of String.

     For Each file As String In Files
                If file.ReadAllText.ToUpper.Contains(SearchStr) Then
                    CodeListBox.Items.Add(Path.GetFileName(file))
                End If
            Next


    Hendri Bissolati

    Sunday, December 01, 2013 6:41 AM
  • Try

                    If File.ReadAllText(File).ToUpper.Contains(SearchStr) Then

    instead.  See: http://msdn.microsoft.com/en-us/library/ms143368(v=vs.110).aspx
    It is strongly recommended that you don't use class names as variable names

    Sunday, December 01, 2013 8:59 AM
  • Hendri,

    That is because you have created ambiguous names without telling in which namespace they are. No problem, that is basic of OOP not forbidden, not recommended to avoid, simply one of the elements.. 

    That is why there are namespaces. 

    Your file is inside your application.

    In VB namespaces are hidden and have defaults. However the IO.File is in the namespace 

    System.IO

    http://msdn.microsoft.com/en-us/library/system.io(v=vs.110).aspx

    And now you have used them twice you have to use the namespace as prefix.


    Success
    Cor

    Sunday, December 01, 2013 1:50 PM
  • Hendri,

    Looking at the screenshot there, the files appear to be MS Word files - if so, you won't ever return any "text" from them using your method.

    *****

    This really needs to be set up either as a datatable or a class and List(Of Class) with all of the information in it. Once you build that, you can hold the data in, for example, an XML file. Doing it that way, you won't need any of those document files; it's all inside the XML file and once loaded, it'll be a whole lot faster too.


    Please call me Frank :)

    Sunday, December 01, 2013 3:00 PM
  • Yes it is a collection of MS Word documents in an folder and I prefer to keep them as is.

    Below is the changed code block in question. 

    Private Sub CodeListBox_DoubleClick(sender As System.Object, e As System.EventArgs) Handles CodeListBox.DoubleClick
            Process.Start(Path.Combine(CompanionPlantingFolder, CodeListBox.SelectedItem.ToString))
            
           CodeListBox.Items.Clear()
           CodeListBox.Items.Add(Populate the Listbox with all the other files that contain any part of the opend file's name in there text)
    
        End Sub
    Please show me some code to do this.


    Hendri Bissolati

    Sunday, December 01, 2013 3:36 PM
  • Yes it is a collection of MS Word documents in an folder and I prefer to keep them as is.

    Below is the changed code block in question. 

    Private Sub CodeListBox_DoubleClick(sender As System.Object, e As System.EventArgs) Handles CodeListBox.DoubleClick
            Process.Start(Path.Combine(CompanionPlantingFolder, CodeListBox.SelectedItem.ToString))
            
           CodeListBox.Items.Clear()
           CodeListBox.Items.Add(Populate the Listbox with all the other files that contain any part of the opend file's name in there text)
    
        End Sub
    Please show me some code to do this.


    Hendri Bissolati

    I don't know anything about reading an MS document file - that involves Office Automation which I've never done.

    Perhaps someone else can help.


    Please call me Frank :)

    Sunday, December 01, 2013 3:39 PM
  • Hendri,

    The time I did some office automation is to far away to help you. 

    Kevin I does sometimes something around it here. If you get no help soon enough then try this forum.

    Visual Studio For Office

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


    Success
    Cor

    Sunday, December 01, 2013 3:58 PM
  • Hello,

    Two examples of search for a word and produce a count. Add a reference to Microsoft.Office.Interop.Word

    The first example does a loop thru files while the second shows one file which of course you can place into a loop.

    Imports Microsoft.Office.Interop
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Not String.IsNullOrWhiteSpace(txtSearchFor.Text) Then
                Dim Files = IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.docx")
                For Each file In Files
                    Dim app As Word.Application = New Word.Application
                    Dim FileName As Object = file
                    Dim doc As Word.Document = app.Documents.Open(FileName)
                    Dim count As Integer = doc.Words.Count
                    Dim Holder As New List(Of Int32) ' contains index of word in document
                    Dim SearchText As String = txtSearchFor.Text.ToLower
    
                    If count > 0 Then
                        For i As Integer = 1 To count
                            If doc.Words(i).Text.ToLower.Contains(SearchText) Then
                                Holder.Add(i)
                            End If
                        Next
                    End If
    
                    app.Quit()
    
                    If Holder.Count > 0 Then
                        Console.WriteLine("{0} has {1} matches", file, Holder.Count)
                    Else
                        Console.WriteLine("{0} has no matches", file)
                    End If
                Next
    
            End If
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            If Not String.IsNullOrWhiteSpace(txtSearchFor.Text) Then
                Dim app As Word.Application = New Word.Application
                Dim FileName As Object = IO.Path.Combine(Application.StartupPath, "SomeFile.docx")
                Dim doc As Word.Document = app.Documents.Open(FileName)
    
                Dim SearchText As String = txtSearchFor.Text.ToLower
                Dim intFound As Integer = 0
    
                Dim rng As Word.Range = doc.Content
                rng.Find.Text = SearchText
                rng.Find.Execute()
    
                Do While rng.Find.Found = True
                    intFound += 1
                    rng.Find.Execute()
                Loop
                app.Quit()
                Console.WriteLine(intFound)
            End If
    
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Monday, December 02, 2013 2:05 AM
    Moderator
  • Morning Guys.

    I have put together a small app to make organic gardening tips easily exessable to me. Here is the complete code so far.

    Imports System.IO
    Public Class Form1
        Dim CompanionPlantingFolder As String = "F:\My Documents\Gardening\My Companion Planting Guide"
        Dim Files() As String = Directory.GetFiles(CompanionPlantingFolder)
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            CodeListBox.Items.Clear()
            For Each file As String In Files
                CodeListBox.Items.Add(Path.GetFileName(file))
            Next
        End Sub
        Private Sub CodeListBox_DoubleClick(sender As System.Object, e As System.EventArgs) Handles CodeListBox.DoubleClick
            Process.Start(Path.Combine(CompanionPlantingFolder, CodeListBox.SelectedItem.ToString))
            'Me.WindowState = FormWindowState.Minimized
        End Sub
        Private Sub StringSearchTextBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles StringSearchTextBox.TextChanged
            CodeListBox.Items.Clear()
            'This line of code removes case sensitivity.
            Dim SearchStr As String = StringSearchTextBox.Text.ToUpper
            If StringSearchTextBox.Text <> "" Then
                For Each file As String In Files
                    If file.ToUpper.Contains(SearchStr) Then
                        CodeListBox.Items.Add(Path.GetFileName(file))
                    End If
                Next
            Else
                MsgBox("No String Enterd")
            End If
        End Sub
        Private Sub ResetButton_Click(sender As System.Object, e As System.EventArgs) Handles ResetButton.Click
            CodeListBox.Items.Clear()
            For Each file As String In Files
                CodeListBox.Items.Add(Path.GetFileName(file))
            Next
        End Sub
    End Class

    As you can see, entering a string into the SearchTextbox results in the ListBox items being filterd acordingly.  And clicking one of the items will open the document.  

    What I need help with is as follow.

    As soon as an item is clicked and the document is opend I need the Listbox to be populated with all the documents that contains eny teference inside the same as the string in the SearchTextbox.


    Hendri Bissolati

    I am responding to the bold, italicized, underlined area. What happens when you want the Listbox re-populated with the previous information rather than just coninuing "south" with this (which means basically going one way continuously without regard for going the other way even though going the other way would actually work)?

    I don't see much of an issue with populating the ListBox with the filenames of every document that contains the string in the search textbox. Although that would require reading every file to a string I suppose and then seeing if they contain the string in the search textbox. I've never tested something like that. And over a wide range of large files I could see that needing to run in a background worker with some kind of display showing for the length of all files combined the amount of data that had been searched so far due to the possible time concerns.

    And the type of documents you want to do this with may be an issue since your original document is apparently not a .txt file since it has an image in it.


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.




    Thursday, December 05, 2013 3:31 AM
  • Morning Guys.

    I have put together a small app to make organic gardening tips easily exessable to me. Here is the complete code so far.

    Imports System.IO
    Public Class Form1
        Dim CompanionPlantingFolder As String = "F:\My Documents\Gardening\My Companion Planting Guide"
        Dim Files() As String = Directory.GetFiles(CompanionPlantingFolder)
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            CodeListBox.Items.Clear()
            For Each file As String In Files
                CodeListBox.Items.Add(Path.GetFileName(file))
            Next
        End Sub
        Private Sub CodeListBox_DoubleClick(sender As System.Object, e As System.EventArgs) Handles CodeListBox.DoubleClick
            Process.Start(Path.Combine(CompanionPlantingFolder, CodeListBox.SelectedItem.ToString))
            'Me.WindowState = FormWindowState.Minimized
        End Sub
        Private Sub StringSearchTextBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles StringSearchTextBox.TextChanged
            CodeListBox.Items.Clear()
            'This line of code removes case sensitivity.
            Dim SearchStr As String = StringSearchTextBox.Text.ToUpper
            If StringSearchTextBox.Text <> "" Then
                For Each file As String In Files
                    If file.ToUpper.Contains(SearchStr) Then
                        CodeListBox.Items.Add(Path.GetFileName(file))
                    End If
                Next
            Else
                MsgBox("No String Enterd")
            End If
        End Sub
        Private Sub ResetButton_Click(sender As System.Object, e As System.EventArgs) Handles ResetButton.Click
            CodeListBox.Items.Clear()
            For Each file As String In Files
                CodeListBox.Items.Add(Path.GetFileName(file))
            Next
        End Sub
    End Class

    As you can see, entering a string into the SearchTextbox results in the ListBox items being filterd acordingly.  And clicking one of the items will open the document.  

    What I need help with is as follow.

    As soon as an item is clicked and the document is opend I need the Listbox to be populated with all the documents that contains eny teference inside the same as the string in the SearchTextbox.


    Hendri Bissolati

    I am responding to the bold, italicized, underlined area. What happens when you want the Listbox re-populated with the previous information rather than just coninuing "south" with this (which means basically going one way continuously without regard for going the other way even though going the other way would actually work)?

    I don't see much of an issue with populating the ListBox with the filenames of every document that contains the string in the search textbox. Although that would require reading every file to a string I suppose and then seeing if they contain the string in the search textbox. I've never tested something like that. And over a wide range of large files I could see that needing to run in a background worker with some kind of display showing for the length of all files combined the amount of data that had been searched so far due to the possible time concerns.

    And the type of documents you want to do this with may be an issue since your original document is apparently not a .txt file since it has an image in it.


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.




    I am planing to use the resetbutton to return North, the files used is MS.Word. The pic you see is not contained in the files.

    Hendri Bissolati

    Saturday, December 07, 2013 5:17 AM
  • the question contains some misspelling which makes it hard to understand: " eny teference " . what is it?

    Saturday, December 07, 2013 10:13 AM
  • Since you are using Word documents then you probably need to use automating Microsoft Office application functionality to search your Word documents for strings. And of course if one is found then provide the appropriate information for that document to your ListBox.

    See this link How to: Search for Text in Documents and any links on the left side of the page or anything under Word Solutions that could help you out.

    And of course exclude the Word Document that you already have shown in your display.

    And maybe this code can assist you. And I'm not hacking a Wireless USB Antenna (as the text in the image below says) I'm adding an external antenna to a Linksys Wi-Fi USB dongle I have to increase its range significantly.

    For Each File In My.Computer.FileSystem.GetFiles("C:\Users\John\Desktop")
          Me.Text = File ' Do your code here I suppose by getting the file and performing whatever actions you are trying to do.
    Next

    Of maybe this code if you don't have only Word documents in your folder. Although the file extension .Doc may not be correct for Word documents as I didn't look up what it should be.

    For Each File In My.Computer.FileSystem.GetFiles("C:\Users\John\Desktop")
                If File.Contains(".Doc") Then
                    Me.Text = File ' Do your code here I suppose by getting the file and performing whatever actions you are trying to do.
                End If
    Next


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.



    Saturday, December 07, 2013 5:09 PM