Results 1 to 7 of 7
  1. #1
    2 Star Lounger
    Join Date
    Jul 2003
    Location
    Park City, Utah
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Efficient Code (Word 2003)

    In the following code, what can be deleted from the WithSelection.Find entries to reduce the amount of code? I'm just doing a replace all...

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = "[Author ID]"
    .Replacement.Text = "strAuthorID"
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

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

    Re: Efficient Code (Word 2003)

    It is a good idea to specify all find/replace characteristics explicitly, otherwise you may be reusing previous settings with unexpected/undesired results. You can streamline the code a bit by putting everything between With ... End With:

    With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "[Author ID]"
    .Replacement.Text = "strAuthorID"
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
    End With

  3. #3
    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: Efficient Code (Word 2003)

    Adding on HansV's comment, I've also started "resetting" nonstandard settings, such as Use Wildcards, so that the next time I visit the Find/Replace dialog I don't curse myself for having to fix all the boxes. Oops, that makes it longer, not shorter. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  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: Efficient Code (Word 2003)

    > If you search using a range object, rather than the Selection object

    I love this! I love separating "me the user" from "me the programmer". As programmers we should be using the Range object except when communicating to/from the user, at which time we need to use the Selection object. [/two cents]

  5. #5
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Efficient Code (Word 2003)

    Just a minor side point here - do you actually want to have the text string "strAuthorID" in your document, or does this, as it seems to, refer to a variable of that name that will carry the author's name? Because if it's the latter, you need to take out the double quotes around "strAuthorID" ...
    Beryl M


  6. #6
    2 Star Lounger
    Join Date
    Jul 2003
    Location
    Park City, Utah
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Efficient Code (Word 2003)

    You are correct and I fixed that in the actual code, the "strAuthorID" refers to a variable and the quotes should be removed.

  7. #7
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Efficient Code (Word 2003)

    If you search using a range object, rather than the Selection object, you always get a "clean start". Not only don't you have to worry about any Selection settings that were in effect before you did your search, you also don't have to worry about resetting anything after your search if your search involves unusual settings that you wouldn't want to have to undo (or might not notice) when you did your next search. Here's your sample search using a range object:
    <pre> Dim rngX As Word.Range
    Set rngX = Selection.Range
    With rngX.Find
    .Text = "[Author ID]"
    .Replacement.Text = "strAuthorID"
    .Execute Replace:=wdReplaceAll
    End With
    Set rngX = Nothing</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
  •