PDA

View Full Version : [SOLVED:] Finding Text and Exclude Fields



Opv
07-09-2012, 09:15 AM
When using With.Selection.Find... = True, what is the method for excluding words found in fields? My script to mark words for an Index is creating an endless loop because the script is finding the word not only in the document text but in each newly created index entry field.

macropod
07-09-2012, 08:20 PM
There is no 'Find' method for excluding fields, per se. It seems your issue is that you haven't told the code where to stop the Find operation (ie by defining the Find range and testing whether the found string is within it). Code that does that kind of checking has been posted here before (eg do a search for 'InRange').

Opv
07-09-2012, 08:28 PM
There is no 'Find' method for excluding fields, per se. It seems your issue is that you haven't told the code where to stop the Find operation (ie by defining the Find range and testing whether the found string is within it). Code that does that kind of checking has been posted here before (eg do a search for 'InRange').

The search range is the entire wdStory, finding all instances, for example, of a particular person, place or thing, and then automatically marking the terms/phrases as index entries. As the index fields are created, the search automatically finds the same string in the newly created field. I don't see how the search range can be defined for anything less than the full wdStory and still find all the index terms to be marked. I tried the inRange thing earlier; however, I don't find any range or inRange method that applies to a field. Notwithstanding, I think I've come up with a work-around, i.e., testing for the existence of the paragraph character immediately adjacent to the selection.find range. That seems to be working in the testing done so far.

Thanks

macropod
07-09-2012, 08:37 PM
Obviously, if it's the main story, you don't want to search the whole range. You need to define a range that doesn't include the Index and search only that range. That's where the InRange test might be useful. As you haven't posted any code, that's the most help that can be given at this stage. The issue isn't whether InRange applies to a field. the field is irrelevant. What matters is keeping the search out of the Index range.

Opv
07-09-2012, 08:54 PM
Obviously, if it's the main story, you don't want to search the whole range. You need to define a range that doesn't include the Index and search only that range. That's where the InRange test might be useful. As you haven't posted any code, that's the most help that can be given at this stage. The issue isn't whether InRange applies to a field. the field is irrelevant. What matters is keeping the search out of the Index range.

Thanks. I'll pursue InRange further, as you suggest. I'm sure I'll figure it out eventually. I like the struggle, as in the end, after the little light comes on, I have the advantage of understanding why something works rather than just having someone supply me with a block of code.

Thanks again.

gmaxey
07-09-2012, 09:23 PM
Unless it is a state secret, it would help to see your code. From reading the dialog above, it sounds like you are searching for terms and marking them as index entries. As you do then each entry is also marked and on and on and on.

What you need to do is move the start of the search range after the newly created index entry. Index enteries are fields and a field doesn't have a range. However, its code does so ...

I type the words "Test, test, test" and run this code:


Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Dim oRng As Word.Range
Dim oFld As Field
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "Test"
While .Execute
Set oFld = ActiveDocument.Indexes.MarkEntry(Range:=oRng, Entry:=oRng.Text, _
EntryAutoText:=oRng.Text, CrossReference:="", CrossReferenceAutoText:="", _
BookmarkName:="", Bold:=False, Italic:=False)
oRng.Start = oFld.Code.End + 1
Wend
End With
lbl_Exit:
Exit Sub
End Sub

macropod
07-10-2012, 12:27 AM
Hi Greg,

Thanks for the input. Maybe I had the wrong end of the stick - I had the impression the OP was also erroneously indexing the index in the main story, which is where the infinite loop originated.

Opv
07-10-2012, 08:07 AM
From reading the dialog above, it sounds like you are searching for terms and marking them as index entries. As you do then each entry is also marked and on and on and on.
Indeed, that is precisely what was happening.


What you need to do is move the start of the search range after the newly created index entry. Index enteries are fields and a field doesn't have a range. However, its code does so ...
Thanks. That is helpful. I think, after some experimentation, that I am beginning to grasp what I was doing wrong.

Opv
07-10-2012, 08:09 AM
Hi Greg,

Thanks for the input. Maybe I had the wrong end of the stick - I had the impression the OP was also erroneously indexing the index in the main story, which is where the infinite loop originated.

Thanks for your help.