Results 1 to 7 of 7
  1. #1
    Lounger
    Join Date
    Jul 2008
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts

    How can I show the color of second part of finding text

    Hi!
    How can I show the color of second part of finding text in a "msgbox"?
    Is there a way to Show the second part of finding text?

    Selection.Text(0).color
    Selection.Text(1).color

    Example:

    This is the 44 second part of the 33 selection.

    Code:
    Sub TheColor()
        Selection.Find.ClearFormatting
        With Selection.Find
            .Text = "([0-9]) ([a-zA-Z])"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .MatchWildcards = True
        End With
        Selection.Find.Execute
    MsgBox "Selection text color is" .....
    'For example "Selection text color is red" or "Selection text color is green"
    
    End Sub

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    There are 16,777,216 font colours available. A miniscule number (16 - or less than 1/1,000,000) is defined as red, green, etc. in Word.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    Lounger
    Join Date
    Jul 2008
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Sory, I couldn't understand.
    Just run macro and find the text.
    And tell me "second part of finding text" color.
    Or what is wrong this code?
    Selection.Text(0).color
    Selection.Text(1).color

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    As I said, there are 16,777,216 font colours available. Every one of these is returned by VBA as a number (from 0 to 16777215), not as a word. Of these 16,777,216 colours, only 16 have an equivalent 'word' description. But getting that description requires coding your own conversion table. For the rest, the best one can do is convert the raw number into RGB values (which, in turn, could be converted to CMYK values and, in some cases, Pantone colour values).

    The following macro & function returns the RGB values and, for the 16 predefined colours, their names. As you can see, this is far more complicated than a mere Selection.Text(1).color.
    Code:
    Sub TheColor()
    With Selection
      With .Find
        .ClearFormatting
        .Text = "([0-9]) ([a-zA-Z])"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .MatchWildcards = True
        .Execute
      End With
      If .Find.Found Then
        With .Characters
          MsgBox "The color of the first character is: " & _
            GetClr(.First.Font.ColorIndex, .First.Font.Color) _
            & vbCr & "The color of the last character is: " & _
            GetClr(.Last.Font.ColorIndex, .Last.Font.Color)
        End With
      End If
    End With
    End Sub
    
    Function GetClr(i As Long, RGB_Val As Long) As String
    Dim StrTmp As String
    If RGB_Val < 0 Or RGB_Val > 16777215 Then RGB_Val = 0
    StrTmp = StrTmp & " R: " & RGB_Val \ 256 ^ 0 Mod 256
    StrTmp = StrTmp & " G: " & RGB_Val \ 256 ^ 1 Mod 256
    StrTmp = StrTmp & " B: " & RGB_Val \ 256 ^ 2 Mod 256
    Select Case i
      Case 0: StrTmp = StrTmp & " - Auto (Default)"
      Case 1: StrTmp = StrTmp & " - Black"
      Case 2: StrTmp = StrTmp & " - Blue"
      Case 3: StrTmp = StrTmp & " - Turquoise"
      Case 4: StrTmp = StrTmp & " - Bright Green"
      Case 5: StrTmp = StrTmp & " - Pink"
      Case 6: StrTmp = StrTmp & " - Red"
      Case 7: StrTmp = StrTmp & " - Yellow"
      Case 8: StrTmp = StrTmp & " - White"
      Case 9: StrTmp = StrTmp & " - Dark Blue"
      Case 10: StrTmp = StrTmp & " - Teal"
      Case 11: StrTmp = StrTmp & " - Green"
      Case 12: StrTmp = StrTmp & " - Violet"
      Case 13: StrTmp = StrTmp & " - Dark Red"
      Case 14: StrTmp = StrTmp & " - Dark Yellow"
      Case 15: StrTmp = StrTmp & " - 50% Gray"
      Case 16: StrTmp = StrTmp & " - 25% Gray"
      Case Else:  StrTmp = StrTmp & " - User Defined"
    End Select
    GetClr = StrTmp
    End Function
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    Lounger
    Join Date
    Jul 2008
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks for your reply.
    I got to use "Selection.Characters.First" and "Selection.Characters.Last".

    How abaut find more than two texts?
    I mean let's find
    .Text = "([a-zA-Z]) ([0-9]) ([a-zA-Z])"

    No code like this:
    Selection.Characters.mid
    or
    Selection.Text(1)

  6. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Quote Originally Posted by yken View Post
    I got to use "Selection.Characters.First" and "Selection.Characters.Last".
    I don't know why you'd do that when I've already shown you how to do it without selecting the text...
    Quote Originally Posted by yken View Post
    How abaut find more than two texts?
    I mean let's find
    .Text = "([a-zA-Z]) ([0-9]) ([a-zA-Z])"
    Obviously, if you're going to change the Find parameters and you want the attributes of a different character, you're going to have to change the characters whose values you pass to the GetClr Function.

    In your "([a-zA-Z]) ([0-9]) ([a-zA-Z])" Find expression, ([0-9]) is the third character. So, if you want to get the results for the third character using the code I posted, instead of, say:
    GetClr(.Last.Font.ColorIndex, .Last.Font.Color)
    which returns the colour of the last character, you'd use:
    GetClr((3).Font.ColorIndex, (3).Font.Color)
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  7. #7
    Lounger
    Join Date
    Jul 2008
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks for your help.

Posting Permissions

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