|
-
Mar 20th, 2002, 04:08 PM
#1
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:
Option Explicit
Private objWord As Word.Application
Private wd As Word.Document
Private Sub Command1_Click()
If objWord Is Nothing Then
Set objWord = CreateObject("Word.Application")
Else
Set objWord = GetObject(, "Word.Application")
End If
DoEvents
Set wd = objWord.Documents.Open("c:\Test.doc")' Here i want it to get the file from the listbox.
With wd.Content.Find
.Text = "test"
Wrap:=wdFindContinue
End With
objWord.ActiveDocument.Close False
'objWord.Visible = True
If Not (wd Is Nothing) Then Set wd = Nothing
If Not (objWord Is Nothing) Then objWord.Application.Quit
If Not (objWord Is Nothing) Then Set objWord = Nothing
End Sub
-
Mar 20th, 2002, 04:33 PM
#2
-= B u g S l a y e r =-
-
Mar 20th, 2002, 04:45 PM
#3
Your code is working fine... but I just modified it a little...can u help me?
-
Mar 20th, 2002, 04:52 PM
#4
-= B u g S l a y e r =-
are all the documents in the same folder?
-
Mar 20th, 2002, 04:56 PM
#5
No, but the path to the document is listed in the listbox. Ex: C:\Word\Test\Testfile.doc
-
Mar 20th, 2002, 05:13 PM
#6
-= B u g S l a y e r =-
one way
VB Code:
Option Explicit
Private objWord As Word.Application
Private wd As Word.Document
Private Sub Form_Load()
List1.Clear
List2.Clear
List1.AddItem "C:\Docs\test1.doc"
List1.AddItem "C:\Docs\test2.doc"
List1.AddItem "C:\Docs\test3.doc"
List1.AddItem "C:\Docs\test4.doc"
End Sub
Private Sub Command1_Click()
Dim i As Integer
If objWord Is Nothing Then
Set objWord = CreateObject("Word.Application")
Else
Set objWord = GetObject(, "Word.Application")
End If
DoEvents
For i = 0 To List1.ListCount - 1
If DocContainSearchString(List1.List(i), "test") Then List2.AddItem List1.List(i)
Next i
If Not (wd Is Nothing) Then Set wd = Nothing
If Not (objWord Is Nothing) Then objWord.Application.Quit
If Not (objWord Is Nothing) Then Set objWord = Nothing
End Sub
Private Function DocContainSearchString(sDocName As String, sSearchString As String) As Boolean
Dim myRange As Range
Dim sSearchfor As String
sSearchfor = sSearchString
If sSearchfor = "" Then Exit Function
Set wd = objWord.Documents.Open(sDocName)
Set myRange = wd.Content
myRange.Find.Execute FindText:=sSearchfor, Forward:=True
If myRange.Find.Found Then
DocContainSearchString = True
Else
DocContainSearchString = False
End If
End Function
-
Mar 20th, 2002, 05:24 PM
#7
Thanks, this code works...
Do you know how to serach in pdf document at the same way?
-
Mar 20th, 2002, 05:34 PM
#8
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*
-
Mar 21st, 2002, 02:27 AM
#9
-= B u g S l a y e r =-
uh.. u want pdf files aswell?
I have to do some digging then...
later
-
Mar 21st, 2002, 01:47 PM
#10
-
Mar 23rd, 2002, 06:00 AM
#11
Peet, do u solved this? ...
-
Mar 25th, 2002, 06:37 AM
#12
Peet, heeeelp me...
-
Mar 25th, 2002, 06:45 AM
#13
-= B u g S l a y e r =-
Sorry Pirre, I'll get working on it later today
-
Mar 25th, 2002, 07:39 AM
#14
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?
-
Mar 25th, 2002, 07:45 AM
#15
to get around the documents not being found:
VB Code:
Private Function DocContainSearchString(sDocName As String, sSearchString As String) As Boolean
Dim myRange As Range
Dim sSearchfor As String
DocContainSearchString = False
sSearchfor = sSearchString
If sSearchfor = "" Then Exit Function
On Error Resume Next
Set wd = objWord.Documents.Open(sDocName)
If Err = 0 Then
Set myRange = wd.Content
myRange.Find.Execute FindText:=sSearchfor, Forward:=True
If myRange.Find.Found Then
DocContainSearchString = True
End If
End If
End Function
-
Mar 26th, 2002, 12:44 PM
#16
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.
-
Mar 26th, 2002, 05:27 PM
#17
-= B u g S l a y e r =-
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.
-
Mar 26th, 2002, 05:34 PM
#18
Ohhh, Peet....you were my last hope...
Anyone else who can help me?
-
Mar 26th, 2002, 07:35 PM
#19
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.
-
Apr 17th, 2002, 03:08 PM
#20
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:
Option Explicit
Private objWord As Word.Application
Private wd As Word.Document
Private Sub Form_Load()
List1.Clear
List2.Clear
List1.AddItem "C:\Docs\test1.doc"
List1.AddItem "C:\Docs\test2.doc"
List1.AddItem "C:\Docs\test3.doc"
List1.AddItem "C:\Docs\test4.doc"
End Sub
Private Sub Command1_Click()
Dim i As Integer
If objWord Is Nothing Then
Set objWord = CreateObject("Word.Application")
Else
Set objWord = GetObject(, "Word.Application")
End If
DoEvents
For i = 0 To List1.ListCount - 1
If DocContainSearchString(List1.List(i), "test") Then List2.AddItem List1.List(i)
Next i
If Not (wd Is Nothing) Then Set wd = Nothing
If Not (objWord Is Nothing) Then objWord.Application.Quit
If Not (objWord Is Nothing) Then Set objWord = Nothing
End Sub
Private Function DocContainSearchString(sDocName As String, sSearchString As String) As Boolean
Dim myRange As Range
Dim sSearchfor As String
sSearchfor = sSearchString
If sSearchfor = "" Then Exit Function
Set wd = objWord.Documents.Open(sDocName)
Set myRange = wd.Content
myRange.Find.Execute FindText:=sSearchfor, Forward:=True
If myRange.Find.Found Then
DocContainSearchString = True
Else
DocContainSearchString = False
End If
End Function
-
Apr 17th, 2002, 03:13 PM
#21
Member
Have you thought about or is it possible to use Index Server/Indexing Services?
-
Apr 17th, 2002, 03:16 PM
#22
BorDeWolf ,
What do you mean? Can you help me?
-
Apr 17th, 2002, 03:32 PM
#23
Member
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.
-
Apr 17th, 2002, 03:41 PM
#24
Thanks BorDeWolf,
I'm using NT4... Do you think you could help me to complete my code with Index Server?
-
Apr 17th, 2002, 03:49 PM
#25
Member
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.
-
Apr 18th, 2002, 03:35 AM
#26
Frenzied Member
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:
Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation _
As String, ByVal lpFile As String, ByVal lpParameters _
As String, ByVal lpDirectory As String, ByVal nShowCmd _
As Long) As Long
Public Sub ShowFindDialog(Optional InitialDirectory As String)
ShellExecute 0, "find", _
IIf(InitialDirectory = "", "", InitialDirectory), _
vbNullString, vbNullString, SW_SHOW
End Sub
And it is then called with:
ShowFindDialog (“C:\FolderToSearch”)
-
Apr 18th, 2002, 03:20 PM
#27
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|