MS Office Forum / Word / Programming / December 2006
Search not working as wanted
|
|
Thread rating:  |
Ken McLennan - 02 Dec 2006 13:18 GMT G'day there One & All,
I'm currently working on my first Word VB project (well, the first one I've not abandoned and forgotten about) and I've obviously got something wrong.
I have some experience with VB for Excel, so most of the concepts ring a bell or two, but the Word object model escapes me and I'm still trying to get my head around it.
I have 3 years worth of occurrence logs that I need to search for a specific string. The logs consist of a single table with some consistent entries. Unfortunately this string appears in all logs in one of those entries which I describe as a "heading area". I need to find it in the "narrative" section, not one of the heading areas. The logs are organised in Year Folders, with Month folders inside. The log names are the date for each daily log.
I'm trying to perform the following tasks:
1. Use dialog to obtain enclosing folder for all documents - done. 2. Iterate through all subfolders to find log documents - done. 3. Search through each log in turn and search for one of the consistant headings, then commence searching from the next line down for the target word. If found, then store filename in base docuement and ... 4. Stop at end of document, and got to next one - done.
Courtesy of the Word MVP pages, and this group's archives, I think I've got 1, 2 & 4 working OK.
However 3 is giving me a few problems. I'm getting false hits, and missing occurrences of the target. Ergo, I've got something out of whack. I hope that someone here may be able to point me in the right direction.
Here's my searching code:
Application.Documents.Open FileName:=strPATH & strFILENAME (Do I need to "open"?) Application.Documents(strFILENAME).Activate With Selection.Find .Text = "Objectives C" ' Heading where search begins from next line .MatchWholeWord = True .Font.Underline = wdUnderlineSingle Do While .Execute Selection.GoTo what:=wdGoToLine, which:=wdGoToNext With Selection.Find .Text = "hotel" .Forward = True .Wrap = wdFindStop .MatchCase = False .MatchWholeWord = True If .Found = True Then strFULLNAME = Mid(strPATH & strFILENAME, 78, Len(strPATH & strFILENAME) - 77) ' Leave just parent folder and filename If Len(objSTOREDOC.Content) = 0 Then _ objSTARTRANGE.InsertAfter strFULLNAME & vbCr Else objSTOREDOC.Content.Select Selection.Collapse direction:=wdCollapseEnd Selection.InsertAfter strFULLNAME & vbCr End If End If End With Loop End With Application.Documents(strFILENAME).Close strFILENAME = Dir
(This is repeated in a loop)
Hopefully this will make sense and someone can point out a better way to do it.
Thanks for listening,
 Signature See ya, Ken McLennan Qld, Australia
Helmut Weber - 02 Dec 2006 14:08 GMT Hi Ken,
apart from the usual advices, to avoid the selection-object and to use a range-object instead,
there seems to be an ".execute" missing in:
With Selection.Find .Text = "hotel" [snip] .execute ' missing ! if .found [snip]
HTH
 Signature Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
Helmut Weber - 02 Dec 2006 14:14 GMT Please disregard,
I was on the wrong track.
Helmut weber
Tony Jollans - 02 Dec 2006 20:45 GMT Actually, Helmut, I think you are on the right track.
After finding "Objectives C" (on the code line Do While .Execute), various things are done to the Find object, but it is not executed again so, first time through, .Found is always True. Subject to the (True) .Found check the Selection is altered and then the loop is iterated, but by then it's already gone wrong.
What I'm not quite sure about is exactly what is being searched for and where, but I would echo your comments about using Selection.Find particularly as the Selection is being used for another purpose.
 Signature Enjoy, Tony
> Please disregard, > > I was on the wrong track. > > Helmut weber Ken McLennan - 03 Dec 2006 01:24 GMT G'day there Helmut & Tony,
> Actually, Helmut, I think you are on the right track. > [quoted text clipped - 3 lines] > Selection is altered and then the loop is iterated, but by then it's already > gone wrong. I understand that I've gone wrong somewhere, but I've never worked with Word before and the differences between it & Excel are really throwing me off track. I'd appreciate any assistance I can get.
> What I'm not quite sure about is exactly what is being searched for and > where, but I would echo your comments about using Selection.Find > particularly as the Selection is being used for another purpose. I have a table in each sheet, thusly:
----------------------------------- | | | | | | | | ----------------------------------- | | | ----------------------------------- | | | | | | | | -----------------------------------
This top section is consistent, with worker's shifts, date, location and other information included.
It's followed by a couple of table rows with addresses, objectives and a few bits and pieces but the addresses include the name of a Hotel which incorporates the word "Hotel". That word appears a couple of times in this section.
The words "Objective C" is a heading for the last defined objective and the word "Hotel" is not included here or after in any pre entered text. Hence any further use of this term refers to an incident at a hotel and it's these incidents recorded in the lower section of the table that I'm surching for.
I therefore tried to find "Objective C" (which is underlined on my documents) and then search between there and the end of the document for the word "hotel" written in any case. As you've obviously determined, I wasn't really successful after the "finding Objective C' bit.
Thanks for the above explanation, also.
Helmut, I was able to find your messages on Google, but they didn't download into my Gravity newsreader so something may be amiss. Or it might not, I dunno how it all works but I thank you for your assistance. It's much appreciated.
 Signature See ya, Ken McLennan Qld, Australia
Helmut Weber - 03 Dec 2006 10:43 GMT Hi Ken,
it might need some more postings, but I think we can do that. So You want to search for "Objectives C" in file B, and if found, search further down for "hotel" in file B, and as long as "hotel" is found, insert parts of the name of file B at the end of file A.
Pseudocode, beware, it's kind of hard without testing material.
Dim lngX as long ' just a counter Dim strF as string ' a file's fullname Dim lngF as long ' number of files to be processed Dim docA as document ' your main doc Dim docB as document ' the secondary docs Dim rngA as range ' the main doc's range Dim rngB as range ' the secondary doc's range
set docA = documents("c:\test\MyMain.Doc") set rngA = docA.range for lngX = 1 to lngF ' or whatever loop construction set docB = documents.open loopdoc(lngX) ' whatever set rngB = docB.range with docB.range.find .text = "Objectives C" if .execute then ' define the range to be searched rngB.collapse direction:=wdcollapseend rngB.end = docB.range.end with rngB.find .text = "hotel" while .execute strF = process(docB.fullname) docA.range.insertafter strF & vbcrlf wend end with endif end with docB.close next ' ------------------------ Test-doc:
hotel The quick brown fox jumps over the lazy dog. objectives C hotel The quick brown fox hotel hotel The quick brown fox jumps over hotel
Test-code finds all occurences of "hotel" after "Objectives C":
Sub Test90012() Dim rngA As Range Set rngA = ActiveDocument.Range With rngA.Find .Text = "Objectives C" If .Execute Then rngA.Select ' for testing stop rngA.Collapse Direction:=wdCollapseEnd rngA.End = ActiveDocument.Range.End With rngA.Find .Text = "hotel" While .Execute rngA.Select ' for testing stop Wend End With End If End With End Sub
If you don't do anything to the range, it collapses to the found spot and expands again to it's former settings. It ain't that easy, but it's worth trying.
HTH
 Signature Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
Ken McLennan - 03 Dec 2006 11:16 GMT G'day there Helmut,
> it might need some more postings, but I think we can do that. I think this one has it all pretty well covered =)
> So You want to search for "Objectives C" in file B, > and if found, search further down for "hotel" in file B, > and as long as "hotel" is found, insert parts of the name > of file B at the end of file A. That's pretty much it in a nutshell.
> Pseudocode, beware, it's kind of hard > without testing material. I can understand that, but I'm sure I can translate pseudocode to the real thing. It might take me a couple of attempts, but I'll get there in the end.
> Dim lngX as long ' just a counter > Dim strF as string ' a file's fullname -----[SNIP]-----
> docB.close > next [quoted text clipped - 7 lines] > hotel > hotel It's all so obvious when you lay it out like that =).
I think that we need MS to rewrite Word so that all the little strings are each in their own box. That way I could borrow the Excel code that I already understand =). Of course, such a project by MS might just screw a few thing up about the office <g>.
I think I might be off to the bookshop after Xmas to get myself a text or two.
Thank you very much for your assistance with this. I really do appreciate you taking the time to help me out.
Take care,
 Signature See ya, Ken McLennan Qld, Australia
aalaan - 03 Dec 2006 17:06 GMT Hi Ken
...
> I think that we need MS to rewrite Word... !!
No trouble. Just give 'em a ring. Ask for Bill. They'll do it <G>
Ken McLennan - 04 Dec 2006 06:51 GMT G'day there Aalan,
> ... > > I think that we need MS to rewrite Word... > > !! > > No trouble. Just give 'em a ring. Ask for Bill. They'll do it <G> Do you really think they'd consider it? If that's so, then I might just call to see what he says. Surely he'd not say rude words to me, would he? <g>
 Signature See ya, Ken McLennan Qld, Australia
aalaan - 04 Dec 2006 19:15 GMT No Ken, he's cool. Why, only the other day he sent me a short video saying he had two minutes for me (I think that's what the signal meant).
Aalaan (also in Australia)
> G'day there Aalan, > [quoted text clipped - 8 lines] > just call to see what he says. Surely he'd not say rude words to me, > would he? <g> kenrmcl_2006@operamail.com - 03 Dec 2006 04:02 GMT G'day there Helmut,
> apart from the usual advices, > to avoid the selection-object and > to use a range-object instead, That's sound advice. It's also preferable with XL, and I actually had my first version of the code with a range object. Of course, I had something else wrong and it didn't work so I tried another version using an example from the help files. Hence it referred to the selection object, but that didn't fix my errors (obviously).
> there seems to be an ".execute" missing in: > [quoted text clipped - 4 lines] > if .found > [snip] Aha!!! You're right!! I'll go back to it and change everything back to a range object and then slip in the missing ".execute" to see if that fixes things. (It's hard to type with my fingers crossed <g> )
> -- > Greetings from Bavaria, Germany From what I've seen, that's a beautiful part of the world. I hope you enjoy it. Thanks very much, See ya Ken McLennan Qld, Australia
|
|
|