Results 1 to 4 of 4
  1. #1
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Hatsukaichi, Hiroshima, Japan
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello, I know how to change the colour of text that is currently highlighted through a macro. But is there any way to automatically find a unit of coloured text and change it to black. For example, here is the start of a famous passage:

    To be, or not to be, that is the question:

    Whether 'tis nobler in the mind to suffer
    The slings and arrows of outrageous fortune,
    Or to take arms against a sea of troubles,
    And by opposing end them?


    I don't want to change all the text just the text on either side of the cursor. So if the cursor was on the word arrows and the macro was run it would end up like this:

    To be, or not to be, that is the question:

    Whether 'tis nobler in the mind to suffer
    The slings and arrows of outrageous fortune,
    Or to take arms against a sea of troubles,
    And by opposing end them?

    I guess I'd need so way to go back word by word from the cursor and test the colour and also forwards, but no idea how to do that. Or perhaps there is an easier approach (he said hopefully). Does anyone have any ideas?

    Thanks,

    Chris (Hunt)

    PS I'm still using Word 97

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Selection.SelectCurrentColor extends the current selection forwards until the text color changes.
    Unfortunately, there is no equivalent method for extending the selection backwards until the color changes, so you'd have to use another method for that, such as moving backwards character by character (safer but slower than word by word) until the color changes.

    See Select Word Range Depending on Color for ideas.

  3. #3
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Hatsukaichi, Hiroshima, Japan
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='HansV' post='789306' date='17-Aug-2009 13:42']Selection.SelectCurrentColor extends the current selection forwards until the text color changes.
    Unfortunately, there is no equivalent method for extending the selection backwards until the color changes, so you'd have to use another method for that, such as moving backwards character by character (safer but slower than word by word) until the color changes.

    See Select Word Range Depending on Color for ideas.[/quote]

    Thanks Hans, Selection.SelectCurrentColor is very useful to know. I had a look at the link you gave me but couldn't figure out how to go backwards character by character to check for a change of colour. Thinking about it character by character is essential as one can't guarantee where the cursor might be.
    Thanks again,

    Chris

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Here is a macro that will do what you want, but it is s l o o o o o w !

    Code:
    Sub SelectColor()
      Application.ScreenUpdating = False
      Selection.SelectCurrentColor
      Do While Not Selection.Font.Color = 9999999
    	' Move backwards by character
    	Selection.MoveStart Count:=-1
      Loop
      ' Undo last move
      Selection.MoveStart Count:=1
      Application.ScreenUpdating = True
    End Sub
    The following version is more elaborate but should be faster if the text usually consists of multiple sentences.

    Code:
    Sub SelectColor()
      Application.ScreenUpdating = False
      Selection.SelectCurrentColor
      Do While Not Selection.Font.Color = 9999999
    	' Move backwards by sentence
    	Selection.MoveStart unit:=wdSentence, Count:=-1
      Loop
      ' Undo last move
      Selection.MoveStart unit:=wdSentence, Count:=1
      Do While Not Selection.Font.Color = 9999999
    	' Move backwards by word
    	Selection.MoveStart unit:=wdWord, Count:=-1
      Loop
      ' Undo last move
      Selection.MoveStart unit:=wdWord, Count:=1
      Do While Not Selection.Font.Color = 9999999
    	' Move backwards by character
    	Selection.MoveStart unit:=wdCharacter, Count:=-1
      Loop
      ' Undo last move
      Selection.MoveStart unit:=wdCharacter, Count:=1
      Application.ScreenUpdating = True
    End Sub

Posting Permissions

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