Results 1 to 13 of 13
  1. #1
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: 'If Selection.Find.Execute' failure (xp)

    Could you live with searching for ^p(^d ? You'd need a wildcard search to include bits after the field, but you cannot search for fields if wildcards are on.

    Note: field codes must be visible when you run the macro.

  2. #2
    Star Lounger
    Join Date
    Jun 2004
    Location
    Gtr London, England
    Posts
    90
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 'If Selection.Find.Execute' failure (xp)

    I'm not too sure whether this is going to be helpful but it seems like you may be getting confused with Selection and Ranges. If you are cycling through paragraphs to find a word, then set the paragraph as your range and interrogate the paragraph. If you set the range you will not need to then use select. You could count the paragraphs in the document and then set the range on every paragraph.

    I am a little unsure of what you are searching for to replace - are you looking for paragraph marks?

    Regards
    Jack

  3. #3
    Star Lounger
    Join Date
    Jun 2004
    Location
    Gtr London, England
    Posts
    90
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 'If Selection.Find.Execute' failure (xp)

    Chris .... after taking a look at Hans reply, I realise that I was talking rubbish - apologies.

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 'If Selection.Find.Execute' failure (xp)

    > I was talking rubbish - apologies

    No need for apologies - I've been coding rubbish, apparently (grin!) so a rubbish response is appropriate (huge grin)

    Thanks for the response. I'm about to respond to Hans, and you'll learn a bit more there .....

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 'If Selection.Find.Execute' failure (xp)

    > Could you live with searching for ^p(^d ?

    I could indeed. (more below). I'm puzzled, though, as to why I can't have any text after the lexical unit ^d . I know that field codes have to be visible. I assume that with field codes visible the text search mechanism gets bogged down in the various switches that are now visible, so that search text placed after the ^d won't match, ever.

    ("below", and for Jack).

    I'w writing a generic text replacement routine. I want to match against constructs such as "a parenthesised field at the start of a paragraph", typically I might want to replace the parenthesised field with an empty string, and then apply a numbered style. Thus in this case I'm looking for "^p(^d)" and want to replace it with "^p". Or I might be looking for "^p^t(^d)^t - " and want to replace it with "^p". Equally I could be looking for fields within a paragraph, such as {tc} and the like. The leading ^p is a particular case of a general search-and-replace mechanism.

    At 7:00 a.m. this morning I was labouring through my childlike way of locating strings; I'm not very good with the Range object, and ended up with pointers:lngStartOfAll (locates the leading ^p)
    lngFieldStart (locates the leading edge of the ^d
    lngFieldEnd (locates the trailing edge of the ^d
    lngEndOfAll (locates the traling edge of the )

    That is, I'd carefully crafted values for .End and .Start of the three sub-components of my search string - head, field, tail, and figured I'd just reset the rng variable to the lngStartOfAll and lngEndOfAll , and drop in the replacement text.

    Then I realised that the replacement text might well be a meta-character such as ^p, so why don't I make use of Word/VBA's existing technology to effect the replace for me. Gives the user the entire range of meta characters for replacement.

    Thus I thought, "set the range to embrace everything from the head to the tail, I already know that the search string is there - it's how I came to determine those four pointers anyway - the search-and-replace should work instantly and I'm done. Very clever.

    But then it didn't work.

    I think that I now see why.

    It may well be that I'm trying to solve two problems at once here, so thanks for reading this far. I'm going to mull over this a bit more, to make sure that I'm doing what I really want to do.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: 'If Selection.Find.Execute' failure (xp)

    If you search for <code>^d</code>, Word will select the entire field. You can then check what is to the right of the selected field.
    If you want to search for a specific field, you can add the field code: <code>^d REF</code>, for example, or <code>^d REF MyBookmark</code>, or <code>^d REF MyBookmark * MERGEFORMAT</code>.

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 'If Selection.Find.Execute' failure (xp)

    > If you want to search for a specific field, you can add the field code:

    Thanks, Hans. Originally I was looping through the fields collection from .Fields.Count to 1, step -1, because the code doesn't know whether it will be modifying or deleting the field. I learned two weeks ago that stepping through Paragraphs is painfully slow, so maybe stepping through fileds isn't such a good idea.

    This morning's code stepped through fields, and I thought that searching for the field once I'd found it made sense so that I could use meta-characters to effect the user's replacement.

    Your tip about searching for a specific field leads me to believe that i should drop the idea of stepping through the Fields collection and revert to the old-fashioned method of using just a straight search-and-replace.

    I'll look at this and post back with my findings.

    Please let me be the 3,718th lounger to ask you where you find these snippets. All of a sudden my cat Jupiter doesn't look as smart as I had thought he was!

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: 'If Selection.Find.Execute' failure (xp)

    I find them by reading the help files, by searching the internet and newsgroups, by experimenting, and last but not least, from the Lounge. See Phil's very useful <!post=Find/Replace Special Characters,421558>Find/Replace Special Characters<!/post>.

  9. #9
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 'If Selection.Find.Execute' failure (xp)

    Sorry for the delay; paid work reared its ugly head .....

    The attached is a loosely-tested function to locate a field based on an end-user's specified mask; the end-user specifies search strings in a text manner, thus looking for a sequenced paragraph mark, the number enclosed in parentheses and followed by a Tab: ^p({SEQ})^t

    rngLocateFieldMask lets me know that I have found a sequence field embedded within a regular Edit-Find search string.

    At this point, having found my field (I'm primarily interested in {SEQ}), my second chunk of code (not presented here) can kick in to analyse the switches within that field.

    (I've never been happy with setting up spare copies of Range objects, so my code relies heavily on the lngStart=rng.Start technique. I'd welcome a non-FORTRAN way of doing this .....)

  10. #10
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 'If Selection.Find.Execute' failure (xp)

    Based on all that's said below, I think the end result is this:
    Find and replace with fields has limitations; iterating through the Fields collection gives us more control.

    I need to let my end-user specify find-and-replace strings in a familiar manner; this includes the use of meta-characters from the "Special" list. Thus my end-user might want to replace parenthesised SEQuence fields with an empty string and apply a paragraph style in place of the {SEQ}. The end-user would write
    (find)^p({SEQ})^t(replace)^p

    Or the end user might want to un-isolate table of contents fields, that sit by themselves in their own paragraph and write this as:
    (find)^p{TC}^p(replace){TC}^p

    In both cases I want to let the user think that they are effecting a series of find-and-replace, but when it comes to fields, find and replace has limitations.

    So I'm going to iterate through the fields collection and test each field to see if its code matches the one specified by the user.
    I will convert the user-specified meta-characters for replacement, as shown in the attached example ("SO LET'S TRANSLATE THEM").
    Although I'll iterate for the field itself ({SEQ} and {TC} in my examples), I'll use .Find to check that I have located such a field with an appropriate header and trailer of text, such as ^p( and )^t.

  11. #11
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    'If Selection.Find.Execute' failure (xp)

    I need help resolving the failure of a simple rng. or selection.find.execute. For the life of me I can't work out why what ought to be a straightforward edited version of a recorded find/replace action should fail.

    My only clue is the weird "new paragraph" evident in the body of the text document, which I have attached as a Word Document, in which is embedded (Module1)
    the VBA code pasted below.
    <pre>Sub test()
    Dim rng As Range
    Set rng = Selection.Range
    rng.Start = 44
    rng.End = 76
    rng.Select
    With Selection.Find
    .Text = "^p(^d)^t"
    .Replacement.Text = "replace"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    If Selection.Find.Execute Then ' we found an occurrence of the tail
    MsgBox "success"
    Else ' catastrophic failure
    Debug.Print rng.Start & " " & rng.End
    MsgBox "catastrophic failure"
    End If
    End Sub
    </pre>

    I'm fairly sure this is going to be a slap-my-forehead solution, and I'm prepared for that .....

    Running the test (note the hard-coded range values) pops up my "catastrophic failure" message box.

  12. #12
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 'If Selection.Find.Execute' failure (xp)

    FWIW here's the second phase of my three-phase solution; the attached code is a skeleton of a device to extract an identifying string from a {SEQ}, the idea being that we can fabricate a style name based on this string.

    Phase 1: first find your mask&field (accomplished)
    Phase 2: identify your field (this post)
    Phase 3: replace the mask&field with a string holding meta-characters (still to come)

  13. #13
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 'If Selection.Find.Execute' failure (xp)

    ... and here's how I replace the found field with a mask containing meta-characters.
    <pre>Public Function strReplaceWithMask(rng As Range, strMask As String) As String
    ''' We would like to effect replacement of one mask with another.
    ' For example, having found "^p({SEQ})^t" we would like to replace it with "^p^t"
    rng.Text = "$" ' first obliterate the original contents
    rng.Start = rng.Start + 1
    With Selection.Find
    .Text = "$"
    .Replacement.Text = "^p^t"
    .Forward = False
    .Wrap = wdFindStop
    End With
    Selection.Find.Execute
    Selection.Find.Execute Replace:=wdReplaceAll
    'Sub TESTstrReplaceWithMask()
    ' Call strReplaceWithMask(Selection.Range, "^p^t")
    'End Sub
    End Function
    </pre>


Posting Permissions

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