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

    Result of Find.Execute Replace ??? (word97/sr2/vba)

    Working with With

    I'm peservering in my use of Range objects (rather than selection) and with With/EndWith.

    I need help in understanding the results of a fairly standard Find/replace/Execute; I say fairly standard, because the code below is cloned from a recording of Words Edit-replace commands (which original appears at the very foot).

    The function blnRuleText works fine in that it finds the formatted text and replaces it with formatted text.

    What's missing (commented with 4 quotes) is the means of assigning the result to the returning function name.

    If I try blnRuleText = .Find.Found I get the result FALSE, even 'though the replacement took effect.

    If I try blnRuleText = .Find.Execute Replace:=wdReplaceAll I get a syntax error.


    The Help files tell me that the .Execute method returns true/false; I believe that's what I'd like to obtain.

    The help files tell me that the .Find returns a Find object

    I figure I should be trapping the result of the .Execute, but "blnruletext = .Find.Execute Replace:=wdReplaceAll" is a syntax error.

    (signed) "stuck" in Toronto


    <pre>Public Function blnRuleText(doc As Document, strAr() As String, iAr As Integer) As Boolean
    ' Given an open document, and a row of a rules array, effect the text find-and-replace

    blnRuleText = False ' default result is failure - "did not find"

    With Documents(doc).Range

    .Find.ClearFormatting
    If strAr(intcFindStyle, iAr) <> "" Then
    .Find.Style = ActiveDocument.Styles(strAr(intcFindStyle, iAr))
    Else
    End If
    If strAr(intcReplaceStyle, iAr) <> "" Then
    .Find.Replacement.Style = ActiveDocument.Styles(strAr(intcReplaceStyle, iAr))
    Else
    End If

    With .Find
    .Text = strAr(intcFindText, iAr)
    .Replacement.Text = strAr(intcReplaceText, iAr)
    .Forward = True
    .Wrap = wdFindContinue
    If strAr(intcFindStyle, iAr) <> "" Or strAr(intcReplaceStyle, iAr) <> "" Then
    .Format = True
    Else
    End If
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With

    .Find.Execute Replace:=wdReplaceAll

    '''' blnRuletext = ??????????
    End With
    End Function
    '
    '
    ' edited macro appears above
    ' recorded macro appears below
    '
    '
    Sub Macro1()
    '
    ' Macro1 Macro
    ' Macro recorded 02/28/02 by Christopher Greaves
    '
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("SmallCaps")
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Style = ActiveDocument.Styles("Hyperlink")
    With Selection.Find
    .Text = "to"
    .Replacement.Text = "aleph"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    End Sub
    </pre>


  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Result of Find.Execute Replace ??? (word97/sr2/vba)

    > f I try blnRuleText = .Find.Execute Replace:=wdReplaceAll I get a syntax error.

    The reason you get a syntax error is that the parameters for a function call need to be in parantheses.

    Try

    <pre> blnRuleText = .Find.Execute(Replace:=wdReplaceAll)
    </pre>


    StuartR

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

    Re: Result of Find.Execute Replace ??? (word97/sr2/vba)

    > parameters for a function call need to be in parantheses.

    Stuart, thanks for this.

    <pre> If strAr(intcReplaceText, iAr) <> "" Then
    blnRuleText = .Find.Execute(Replace:=wdReplaceAll)
    Else
    blnRuleText = .Find.Execute(Replace:=wdReplaceNone)
    End If
    </pre>


    I'll have to investigate further. I get a FALSE result no matter whether the .Find text is found or not. Something wrong with my logic ....

    I get execution now, so thanks for that.

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Result of Find.Execute Replace ??? (word97/sr2/vba)

    Chris,

    Does it make any difference if you move the "Replace:=wdReplaceAll" out of the .Execute method call? i.e. put that above along with the other "With .Find"s : .Replace = wdReplaceAll (?)

    Gary

  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: Result of Find.Execute Replace ??? (word97/sr2/vba)

    Thanks Gary, I'd tried it after seeing your post, but with no means of getting the "replace:=wdreplaceall" into my WITH.

    Below I have pasted a couple of essays that interest me. The first one ALWAYS returns false, even when it effects replacement. It's as if making use of the Replace object, which seems to be a sort-of-sub-object of the Find object, returns False regardless.

    The second essay, a simple Find, demonstrates that the Find does cause a TRUE to be returned.

    Odd.


    (You'll need a dummy document with a couple of occurrences of the two-letter word "to")


    <pre>Option Explicit
    '
    Sub Macro1()
    '
    ' Macro1 Macro
    ' Macro recorded 02/28/02 by Christopher Greaves
    '
    Selection.HomeKey Unit:=wdStory
    '
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = "to"
    .Replacement.Text = "too"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    MsgBox Selection.Find.Execute(Replace:=wdReplaceAll) ' always FALSE ???!!!!!
    End Sub
    Sub Macro2()
    '
    ' Macro2 Macro
    ' Macro recorded 02/28/02 by Christopher Greaves
    '
    ' Selection.HomeKey Unit:=wdStory
    '
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "to"
    .Replacement.Text = "too"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    MsgBox Selection.Find.Execute ' always TRUE !!!!
    End Sub
    </pre>


  6. #6
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Result of Find.Execute Replace ??? (word97/sr2/vba)

    Chris,

    I find that Macro2 does return an accurate result, but you must change .MatchWholeWord to True - I think what might have been happening in your test is that it was finding things like "into" - hence always returning True.

    I'm a little rusty on this, but I think the ".Wrap = wdFindContinue" may do the same thing as the "Replace:=wdReplaceAll" argument to the .Execute method, which is why you can do without the latter if you have the former (I think...)

    Gary

  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: Result of Find.Execute Replace ??? (word97/sr2/vba)

    I see the other suggestions and will investigate them too.

    Last night I think I stumbled on the answer, but went back to sleep:

    It's as if the Find works well, and the ReplaceAll, being a sub-of find, is implemented as a collection of Finds, thus the Find will always return a correct result, but the ReplaceAll will return (bug!) the result of the FINAL find:

    Find:
    look for it, return the result of the latest find


    Replace:
    keep on looking until the find fails.
    return the result of the latest find (always fails, right?)


    Just a theory ....


    I thought of this because I make several processes on a document, and each process may either succeed (I found something to do) or fail (there was no work for me). I have to record overall whether any thing was succesful, and act accordingly. hence my logic says
    FinalSuccess=FinalSuccess Or Local Success

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

    Re: Result of Find.Execute Replace ??? (word97/sr2/vba)

    > I find that Macro2 does return an accurate result, but you must change .MatchWholeWord to True - I think what might have been happening in your test is that it was finding things like "into" - hence always returning True.


    This is at odds with my Word97SR2 efforts. This morning I re-recorded Macro1 and Macro2 and re-ran the tests. Macro1 always] returns TRUE and Macro2 always returns FALSE.

    Changing parameters such as WholeWord ought to have no effect; after all, who is to say whether I want whole words or not? It happens that my test document contains both words "to" and "into", so either way, I should be getting a TRUE result on the Macro2.


    > I'm a little rusty on this, but I think the ".Wrap = wdFindContinue" may do the same thing as the "Replace:=wdReplaceAll" argument to the .Execute method, which is why you can do without the latter if you have the former (I think...)


    Hmmm. You will have seen my earlier theory on a bit of Harold-code. I was interested in replacing the wdreplaceAll for just that reason, so I did.

    Nix. I still get FALSE always from Macro2.

    Thanks for the input, though, and keep e'm rolling.

    I'm coming to the point of view that the Replace isn't going to work quite as one might expect, and there is probably a good MSoft reason for it. However, I think that Replace ought to return FALSE only if not one replacement takes effect. If one or more replacements is effected, I expect to hear TRUE from the Replace. A design feature it might yet be, but lousy design.

    OTOH maybe I just haven't understood the documentation ....

  9. #9
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Result of Find.Execute Replace ??? (word97/sr2/vba)

    (edited to fix url link)

    You could always try the technique suggested in http://www.mvps.org/word/FAQs/MacrosVBA/Ge...eplacements.htm

    So long as the replacement string is a different length to the string you are replacing then do the replacement and compare ActiveDocument.Characters.Count before and after the replacement.

    StuartR

  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: Result of Find.Execute Replace ??? (word97/sr2/vba)

    > compare ActiveDocument.Characters.Count

    Thanks for this. I read the article. Aaaaaaaaaaaaaaaaaaaaaaaaaaargh! It's like counting how many people are in a room by counting legs and dividing by two. Still it will work.

    As will your method.


    I also contemplated a test for ActiveDocument.IsDirty (not sure of the keyword), as long as I don't need that status. I may have to clear the status by doing a FileSave before hand, which would affect my execution speed.

    I'm amazed/aghast that there's no apparent status bit available.

  11. #11
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Result of Find.Execute Replace ??? (word97/sr2/vba)

    > ActiveDocument.IsDirty

    ActiveDocument.Saved = False

Posting Permissions

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