Results 1 to 4 of 4

Thread: Storing Values From within find

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2005
    Posts
    24

    Storing Values From within find

    Hi,

    I was just wondering if there is a way to store a valur that turns up within
    Selection.Find.Text

    Consider the following code

    VB Code:
    1. Sub style_tag()
    2.   Selection.Find.ClearFormatting
    3.     Selection.Find.Style = ActiveDocument.Styles("H1")
    4.     Selection.Find.Replacement.ClearFormatting
    5.     With Selection.Find
    6.         .Text = ""
    7.         .Replacement.Text = "^&"
    8.         .Replacement.Font.Color = wdColorBlue
    9.         .Forward = True
    10.         .Wrap = wdFindContinue
    11.         .Format = True
    12.         .MatchCase = False
    13.         .MatchWholeWord = False
    14.         .MatchWildcards = False
    15.         .MatchSoundsLike = False
    16.         .MatchAllWordForms = False
    17.     End With
    18.     Selection.Find.Execute Replace:=wdReplaceAll
    19.     Selection.EscapeKey
    20. End Sub

    Now the above code finds where the style name H1 has been applied and highlights the selected text to blue.

    Is there a way in which i can store the resultant text in a variable?

    Example: if the style h1 is applied to a word "Chapter 1" then this code searches h1 style and highlights "Chapter 1" to blue. Is there a way in which i can store the highlighted text in a variable?

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,501

    Re: Storing Values From within find

    This is jsut an educated guess, but shouldnt be far wrong:
    VB Code:
    1. MyVariable = Selection.Text

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2005
    Posts
    24

    Re: Storing Values From within find

    MyVariable = Selection.Text
    This just stores the text in which the cursor is positioned

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,501

    Re: Storing Values From within find

    Where in your code did you place it? I think it would work if placed just before/after "Selection.EscapeKey". (if not, you may need to find & store before replacing).

    Note that it will only find the last value changed, as you are replacing all instances in one go - to get all of them you would need to replace just one at a time.

Posting Permissions

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



Click Here to Expand Forum to Full Width