Results 1 to 6 of 6
  1. #1
    2 Star Lounger
    Join Date
    Sep 2005
    Location
    Poughkeepsie, New York, USA
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Deselecting text after running a macro (Word 2003 SP3)

    I am running a macro where I first select text then run the macro to replace the selected text with the same text but highlighted. When done, the macro returns me to where I started but the selected text remains selected. I would like to deselect the text and move the cursor to the immediate position following the selected text. For example, if I select AACT in my document and then run the macro, the macro searches for each instance of AACT, replaces each instance with AACT in highlight, and when no more instances are found returns me to the original starting point but with AACT still highlighted. Instead, I would like to be returned to the original AACT, have the the text unselected and the cursor placed immediately following the T. The text of the macro is as follows:

    Sub MarkSelectedTextwithHighlight()

    ' Marks selected text with highlight
    ' Macro created 11/16/2005

    ' Establishes selected text as the F&R text

    Dim strText As String
    strText = Selection.Text

    ' Clears formatting and turns highlighting on

    With Selection.Find
    .ClearFormatting
    .Text = Selection.Text
    .Replacement.Text = Selection.Text
    With .Replacement
    .ClearFormatting
    .Highlight = True
    End With

    ' Finds the selected text and replaces it with
    ' the same text but highlighted

    .Execute Replace:=wdReplaceAll
    .Text = Selection.Text
    .Replacement.Text = Selection.Text
    .Wrap = wdFindStop
    .Format = True
    .Forward = True
    .MatchCase = True
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = False

    End With

    End Sub

    I haven't been able to figure out how to do it. Any suggestions will be appreciated. <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15>

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

    Re: Deselecting text after running a macro (Word 2003 SP3)

    Try
    <code>Selection.Collapse Direction:=wdCollapseEnd</code>

    StuartR

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

    Re: Deselecting text after running a macro (Word 2003 SP3)

    Try

    Selection.Collapse Direction:=wdCollapseEnd

  4. #4
    2 Star Lounger
    Join Date
    Sep 2005
    Location
    Poughkeepsie, New York, USA
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Deselecting text after running a macro (Word 2003 SP3)

    Thank you, Stuart and Hans. I spent hours trying to figure this one out. It works perfectly.

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

    Re: Deselecting text after running a macro (Word 2003 SP3)

    If I say snap before you, do I get both posts?

    StuartR

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

    Re: Deselecting text after running a macro (Word 2003 SP3)

    <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

Posting Permissions

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