Consulting

Results 1 to 9 of 9

Thread: Finding Text and Exclude Fields

  1. #1
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location

    Finding Text and Exclude Fields

    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.

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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').
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by macropod
    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

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by macropod
    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.

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,339
    Location
    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
    Last edited by Aussiebear; 04-21-2023 at 08:11 PM. Reason: Adjusted code tags
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  8. #8
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by gmaxey
    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.

    Quote Originally Posted by fmaxey
    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.

  9. #9
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by macropod
    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.

Posting Permissions

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