Results 1 to 27 of 27

Thread: Search in Word document?

  1. #1
    Pirre001
    Guest

    Question Search in Word document?

    I have several Word .doc files listed in a listbox and I need to search all of them to see if a certain string of text is found. I don't want any of the documents to be opened, I just want the name(s) of the documents that the text is found in, to be returned to the listbox. It should only show the founded documents in the listbox.
    This far have I come with the code, as I think peet wrote. But this code are far away from complete and not okej for my purpose...
    VB Code:
    1. Option Explicit
    2. Private objWord As Word.Application
    3. Private wd As Word.Document
    4.  
    5. Private Sub Command1_Click()
    6.    
    7.     If objWord Is Nothing Then
    8.         Set objWord = CreateObject("Word.Application")
    9.     Else
    10.         Set objWord = GetObject(, "Word.Application")
    11.     End If
    12.     DoEvents
    13.     Set wd = objWord.Documents.Open("c:\Test.doc")' Here i want it to get the file from the listbox.
    14.  
    15.     With wd.Content.Find
    16.         .Text = "test"
    17. Wrap:=wdFindContinue
    18.     End With
    19.     objWord.ActiveDocument.Close False
    20.    
    21.     'objWord.Visible = True
    22.     If Not (wd Is Nothing) Then Set wd = Nothing
    23.     If Not (objWord Is Nothing) Then objWord.Application.Quit
    24.     If Not (objWord Is Nothing) Then Set objWord = Nothing
    25.  
    26. End Sub

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    u saying my code is not working?? *sob* I don't beleve u !
    -= a peet post =-

  3. #3
    Pirre001
    Guest
    Your code is working fine... but I just modified it a little...can u help me?

  4. #4
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    are all the documents in the same folder?
    -= a peet post =-

  5. #5
    Pirre001
    Guest
    No, but the path to the document is listed in the listbox. Ex: C:\Word\Test\Testfile.doc

  6. #6
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    one way

    VB Code:
    1. Option Explicit
    2. Private objWord As Word.Application
    3. Private wd As Word.Document
    4.  
    5. Private Sub Form_Load()
    6.     List1.Clear
    7.     List2.Clear
    8.     List1.AddItem "C:\Docs\test1.doc"
    9.     List1.AddItem "C:\Docs\test2.doc"
    10.     List1.AddItem "C:\Docs\test3.doc"
    11.     List1.AddItem "C:\Docs\test4.doc"
    12. End Sub
    13. Private Sub Command1_Click()
    14.     Dim i As Integer
    15.    
    16.     If objWord Is Nothing Then
    17.         Set objWord = CreateObject("Word.Application")
    18.     Else
    19.         Set objWord = GetObject(, "Word.Application")
    20.     End If
    21.     DoEvents
    22.    
    23.     For i = 0 To List1.ListCount - 1
    24.         If DocContainSearchString(List1.List(i), "test") Then List2.AddItem List1.List(i)
    25.     Next i
    26.     If Not (wd Is Nothing) Then Set wd = Nothing
    27.     If Not (objWord Is Nothing) Then objWord.Application.Quit
    28.     If Not (objWord Is Nothing) Then Set objWord = Nothing
    29.    
    30. End Sub
    31.  
    32. Private Function DocContainSearchString(sDocName As String, sSearchString As String) As Boolean
    33.     Dim myRange As Range
    34.     Dim sSearchfor As String
    35.  
    36.     sSearchfor = sSearchString
    37.     If sSearchfor = "" Then Exit Function
    38.    
    39.     Set wd = objWord.Documents.Open(sDocName)
    40.    
    41.     Set myRange = wd.Content
    42.     myRange.Find.Execute FindText:=sSearchfor, Forward:=True
    43.     If myRange.Find.Found Then
    44.         DocContainSearchString = True
    45.     Else
    46.         DocContainSearchString = False
    47.     End If
    48.    
    49. End Function
    -= a peet post =-

  7. #7
    Pirre001
    Guest
    Thanks, this code works...

    Do you know how to serach in pdf document at the same way?

  8. #8
    Pirre001
    Guest
    If I want to search for a text it works, but when I use wildcards (*) in the text it doesen't work. Do you know how to complete the to use wildcards in my text?
    Ex: Test* or *Test*

  9. #9
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    uh.. u want pdf files aswell?

    I have to do some digging then...

    later
    -= a peet post =-

  10. #10
    Pirre001
    Guest
    I have a code for *.doc files.
    And want a code with same functions for pdf files also.
    I would be very, very happy if you could help me with that code... I'm gonne give you many carlsberg beers then.

  11. #11
    Pirre001
    Guest
    Peet, do u solved this? ...

  12. #12
    Pirre001
    Guest
    Peet, heeeelp me...

  13. #13
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Sorry Pirre, I'll get working on it later today
    -= a peet post =-

  14. #14
    Pirre001
    Guest
    Ok, Thanks Peet!

    Another question, when I search in *.doc documents with the code you made for me...it stops if the word-document is not found. How do I do to make the code continue the search with the next document in the listbox even if a document not found?

  15. #15
    Si_the_geek
    Guest
    to get around the documents not being found:

    VB Code:
    1. Private Function DocContainSearchString(sDocName As String, sSearchString As String) As Boolean
    2.     Dim myRange As Range
    3.     Dim sSearchfor As String
    4.  
    5.     DocContainSearchString = False
    6.  
    7.     sSearchfor = sSearchString
    8.     If sSearchfor = "" Then Exit Function
    9.    
    10.     On Error Resume Next
    11.     Set wd = objWord.Documents.Open(sDocName)
    12.  
    13.     If Err = 0 Then  
    14.       Set myRange = wd.Content
    15.       myRange.Find.Execute FindText:=sSearchfor, Forward:=True
    16.       If myRange.Find.Found Then
    17.           DocContainSearchString = True
    18.       End If
    19.    End If
    20.  
    21. End Function

  16. #16
    Pirre001
    Guest
    Heelllo again Peet and all of you,

    I have been looking around by myself. I have also downloaded the latest sdk for adobe pdf. But I haven't found out how to search in pdf documents with vb.
    I would be very greateful if someone, or you Peet, could help me with this.

  17. #17
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Hi Pirre,
    I'm sorry to say, but I have not come up with any solution to how to search for PDF's containing a search crit.
    -= a peet post =-

  18. #18
    Pirre001
    Guest
    Ohhh, Peet....you were my last hope...

    Anyone else who can help me?

  19. #19
    WorkHorse
    Guest
    I don't have Acrobat (just the free reader), so I'm just going by hearsay. You have to have something that translates the gobble-dee-gook in a PDF file into language. I believe when you actually buy Acrobat, you get a component, and maybe some macros, that will allow you to read the text from a PDF. It looks like there are some DDLs on the market that will do limted work with PDF files that you can buy and use.

    Unless you have something that can read the PDF file text, I think you would have to use some wacky work-around like shelling each file and sending keys to search for text and maybe send keys to copy the text then checking the clipboard? Maybe an API could get the text, but you would still need to open each file.

  20. #20
    Pirre001
    Guest
    Is this the best code to search in 5000 documents in a row after a text?
    When I use this code now, it stops working after about 2000 documents. Why?

    I want it to search trough the header also, how do I complete the code for that?
    VB Code:
    1. Option Explicit
    2. Private objWord As Word.Application
    3. Private wd As Word.Document
    4.  
    5. Private Sub Form_Load()
    6.     List1.Clear
    7.     List2.Clear
    8.     List1.AddItem "C:\Docs\test1.doc"
    9.     List1.AddItem "C:\Docs\test2.doc"
    10.     List1.AddItem "C:\Docs\test3.doc"
    11.     List1.AddItem "C:\Docs\test4.doc"
    12. End Sub
    13. Private Sub Command1_Click()
    14.     Dim i As Integer
    15.    
    16.     If objWord Is Nothing Then
    17.         Set objWord = CreateObject("Word.Application")
    18.     Else
    19.         Set objWord = GetObject(, "Word.Application")
    20.     End If
    21.     DoEvents
    22.    
    23.     For i = 0 To List1.ListCount - 1
    24.         If DocContainSearchString(List1.List(i), "test") Then List2.AddItem List1.List(i)
    25.     Next i
    26.     If Not (wd Is Nothing) Then Set wd = Nothing
    27.     If Not (objWord Is Nothing) Then objWord.Application.Quit
    28.     If Not (objWord Is Nothing) Then Set objWord = Nothing
    29.    
    30. End Sub
    31.  
    32. Private Function DocContainSearchString(sDocName As String, sSearchString As String) As Boolean
    33.     Dim myRange As Range
    34.     Dim sSearchfor As String
    35.  
    36.     sSearchfor = sSearchString
    37.     If sSearchfor = "" Then Exit Function
    38.    
    39.     Set wd = objWord.Documents.Open(sDocName)
    40.    
    41.     Set myRange = wd.Content
    42.     myRange.Find.Execute FindText:=sSearchfor, Forward:=True
    43.     If myRange.Find.Found Then
    44.         DocContainSearchString = True
    45.     Else
    46.         DocContainSearchString = False
    47.     End If
    48.    
    49. End Function

  21. #21
    Member
    Join Date
    Mar 2002
    Location
    Amsterdam
    Posts
    51
    Have you thought about or is it possible to use Index Server/Indexing Services?

  22. #22
    Pirre001
    Guest
    BorDeWolf ,

    What do you mean? Can you help me?

  23. #23
    Member
    Join Date
    Mar 2002
    Location
    Amsterdam
    Posts
    51
    The Index Server is available on NT4 Server and W2000. You can do a lot of things with it. You can enter directories and submit queries. In a VB program you can call the Index component and do all kind of things. Some sample code:

    Code:
      
      'creating the index object
      Set objQuery = CreateObject("ixsso.Query")
      objQuery.Query = strQuery
      objQuery.Columns = "filename, size, rank, hitcount"
    
      'define scope and submiting query
      Set objUtil = CreateObject("ixsso.util")
      objUtil.AddScopeToQuery objQuery, myDirectory, "deep"
    
      Set rsXML = New MSXML2.DOMDocument30
      
      'get searchresults and store them in xml
      Set rs = objQuery.CreateRecordset("nonsequential")
      rs.Save rsXML, adPersistXML
    
      'use a stylesheet to get the format you want 
      Set XSL = New MSXML2.DOMDocument30
      XSL.async = False
      If Not XSL.Load(m_XSL_PATH & "myXSL.xsl") Then
        Err.Raise 1, , "the expression is invalid XML"
      Else
        strResult = rsXML.transformNode(XSL)
      End If
      
      MakeQuery = strResult
    You can leave out the XML part. The query can also be stored as
    an ADODB RecordSet. What you get from the index object is:

    filename, size, rank, hitcount

    of any .doc, .ppt, .txt, .xls file - and with adding a little component - this can also be enabled for .pdf - in which the word in the strQuery occurs. You can work with Boolean operators, wildcards and do phrase searches. Indexing Services is extremely strong when you have to scan a large of documents.

    Search the MSDN - there is quite a lot of documentation about this service that ship with NT Server and W2000.

  24. #24
    Pirre001
    Guest
    Thanks BorDeWolf,

    I'm using NT4... Do you think you could help me to complete my code with Index Server?

  25. #25
    Member
    Join Date
    Mar 2002
    Location
    Amsterdam
    Posts
    51
    Well, first I have to have more details about the constellation you want to work in. I have only experience with calling Index Server on a server on which a lot files are stored. If I remember well, Index Server is not available on NT4 Workstation.

  26. #26
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    If you are not too interested in out-right speed, then the simple Windows Explorer "Find" facility will find text within documents.

    In order to call the Windows Explorer “Find Files” dialogue outside your program, use the following:
    VB Code:
    1. Declare Function ShellExecute Lib "shell32.dll" Alias _
    2.     "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation _
    3.     As String, ByVal lpFile As String, ByVal lpParameters _
    4.     As String, ByVal lpDirectory As String, ByVal nShowCmd _
    5.     As Long) As Long
    6.  
    7. Public Sub ShowFindDialog(Optional InitialDirectory As String)
    8.     ShellExecute 0, "find", _
    9.         IIf(InitialDirectory = "", "", InitialDirectory), _
    10.         vbNullString, vbNullString, SW_SHOW
    11. End Sub
    And it is then called with:

    ShowFindDialog (“C:\FolderToSearch”)

  27. #27
    Pirre001
    Guest
    Hi agan everybody,

    I hope you understand me right. But what I want is a code to search in 5000 word-documents in a row after a certain text, and list the results in a listbox.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width