Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-29-2017, 04:48 AM
bnyamin bnyamin is offline find colors then .... Windows 7 64bit find colors then .... Office 2010 64bit
Advanced Beginner
find colors then ....
 
Join Date: Oct 2014
Posts: 36
bnyamin is on a distinguished road
Question find colors then ....


excuse me
i cannot writ english good !
i need a code for :
find all colors in body text in word
then:
insert cood (for exampel ("code color is green/start/")) at start color
insert cood (for exampel ("code color is green/end/")) at end color

Sample
Attached Files
File Type: docx befor run vb .docx (15.8 KB, 9 views)
File Type: docx after run vb .docx (16.4 KB, 8 views)
Reply With Quote
  #2  
Old 04-29-2017, 06:17 AM
macropod's Avatar
macropod macropod is offline find colors then .... Windows 7 64bit find colors then .... Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Word supports 2^24 (i.e. 16,777,216) colours, of which only 2^4 (i.e. 16) have named constants. How do you want the other 16,777,200 colours reported?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 04-29-2017, 10:25 AM
bnyamin bnyamin is offline find colors then .... Windows 7 64bit find colors then .... Office 2010 64bit
Advanced Beginner
find colors then ....
 
Join Date: Oct 2014
Posts: 36
bnyamin is on a distinguished road
Default

yes
Insert the code instead of the color name
is This a good way?



You have proposed ؟ or Your suggestions?
Reply With Quote
  #4  
Old 04-29-2017, 04:47 PM
macropod's Avatar
macropod macropod is offline find colors then .... Windows 7 64bit find colors then .... Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try the following macro. It will generate output like:
< R: 0 G: 176 B: 80 - Green>the social sciences can be found at </ R: 0 G: 176 B: 80 - Green>
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim StrClr As String
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .Replacement.Text = ""
    .Format = True
    .Forward = True
    .Wrap = wdFindContinue
    .Font.ColorIndex = wdAuto
    .Replacement.Font.Hidden = True
    .Execute Replace:=wdReplaceAll
    .ClearFormatting
    .Replacement.ClearFormatting
    .Wrap = wdFindStop
    .Font.Hidden = False
    .Execute
  End With
  Do While .Find.Found
    StrClr = GetClr(.Characters.First.Font.Color, .Characters.First.Font.ColorIndex)
    If .Font.ColorIndex <> wdAuto Then
      With .Duplicate
        .Collapse wdCollapseStart
        .Text = "<" & StrClr & ">"
        .Font.ColorIndex = wdAuto
      End With
      With .Duplicate
        .Collapse wdCollapseEnd
        If .Characters.Last.Previous = vbCr Then .End = .End - 1
        .Text = "</" & StrClr & ">"
        .Font.ColorIndex = wdAuto
      End With
    End If
    .Collapse wdCollapseEnd
    If .End >= ActiveDocument.Range.End - 1 Then Exit Do
    .Find.Execute
  Loop
End With
ActiveDocument.Range.Font.Hidden = False
Application.ScreenUpdating = True
End Sub

Function GetClr(RGB_Val As Long, Optional i 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
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 04-29-2017, 07:14 PM
bnyamin bnyamin is offline find colors then .... Windows 7 64bit find colors then .... Office 2010 64bit
Advanced Beginner
find colors then ....
 
Join Date: Oct 2014
Posts: 36
bnyamin is on a distinguished road
Default

Thank you
This method is very good
But after running this code
The product ....

After pasting the code

Two macro you've written?
But a macro is executed!!!
Attached Images
File Type: png Untitled.png (198.9 KB, 20 views)
Attached Files
File Type: docx after run vb2 .docx (16.1 KB, 7 views)
Reply With Quote
  #6  
Old 04-29-2017, 08:40 PM
macropod's Avatar
macropod macropod is offline find colors then .... Windows 7 64bit find colors then .... Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

I don't get an output anything like your latest attachment. See attached.

There is only one macro (named 'Demo') which calls a Function (named 'GetClr').
Attached Files
File Type: docm befor run vb (1).docm (26.2 KB, 10 views)
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 04-29-2017, 10:30 PM
bnyamin bnyamin is offline find colors then .... Windows 7 64bit find colors then .... Office 2010 64bit
Advanced Beginner
find colors then ....
 
Join Date: Oct 2014
Posts: 36
bnyamin is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
I don't get an output anything like your latest attachment. See attached.

There is only one macro (named 'Demo') which calls a Function (named 'GetClr').
This file is absolutely true!
But
When I run Yours macro
I see error !!
Attached Images
File Type: png Untitled.png (253.4 KB, 19 views)
Reply With Quote
  #8  
Old 04-29-2017, 11:00 PM
macropod's Avatar
macropod macropod is offline find colors then .... Windows 7 64bit find colors then .... Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Did you do what the message suggested?

Please don't keep posting large screenshots; either resize/crop them to fit; or, better still, post just the relevant messages, where applicable.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 04-30-2017, 06:44 AM
gmaxey gmaxey is offline find colors then .... Windows 7 32bit find colors then .... Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,428
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Paul,

I like your function! As you know, your use of "Find.Found" shivers my timbers so I try to avoid it. Tinkering with your code, I did notice that it can hiccup if there are color adjacent to each other. This can probably be refined but seems to work:

Code:
Sub Demo()
Application.ScreenUpdating = False
Dim StrClr As String
Dim i As Long, oRng As Range, oRngCompare As Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Format = True
    .Wrap = wdFindContinue
    .Font.ColorIndex = wdAuto
    .Replacement.Font.Hidden = True
    .Execute Replace:=wdReplaceAll
    .ClearFormatting
    .Wrap = wdFindStop
    .Font.Hidden = False
    Do While .Execute
      With oRng
        StrClr = GetClr(.Characters.First.Font.Color, .Characters.First.Font.ColorIndex)
        If .Font.ColorIndex <> wdAuto Then
          Set oRngCompare = oRng.Duplicate
          With oRngCompare
            .Collapse wdCollapseStart
            .Text = "<" & StrClr & ">"
            .Font.ColorIndex = wdAuto
          End With
          .Start = oRngCompare.End
          Set oRngCompare = .Duplicate
          oRngCompare.Collapse wdCollapseStart
          If Asc(.Characters.Last) = 13 Then
            Do While .Characters.Last.Font.Color = .Characters.Last.Next.Font.Color
              .End = .End + 1
            Loop
          End If
          With .Duplicate
            For i = 1 To .Characters.Count
            If .Characters(i).Font.Color = .Characters(1).Font.Color Then
              oRngCompare.End = oRngCompare.End + 1
              oRngCompare.Select
            Else
              Exit For
            End If
            Next i
          End With
          .End = oRngCompare.End
          .Select
          .Collapse wdCollapseEnd
        .Text = "</" & StrClr & ">"
        .Font.ColorIndex = wdAuto
        End If
        .Collapse wdCollapseEnd
        If .End >= ActiveDocument.Range.End - 1 Then Exit Do
      End With
    Loop
  End With
  ActiveDocument.Range.Font.Hidden = False
  Application.ScreenUpdating = True
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #10  
Old 04-30-2017, 01:37 PM
bnyamin bnyamin is offline find colors then .... Windows 7 64bit find colors then .... Office 2010 64bit
Advanced Beginner
find colors then ....
 
Join Date: Oct 2014
Posts: 36
bnyamin is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
Paul,

I like your function! As you know, your use of "Find.Found" shivers my timbers so I try to avoid it. Tinkering with your code, I did notice that it can hiccup if there are color adjacent to each other. This can probably be refined but seems to work:

Code:
Sub Demo()
Application.ScreenUpdating = False
Dim StrClr As String
Dim i As Long, oRng As Range, oRngCompare As Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Format = True
    .Wrap = wdFindContinue
    .Font.ColorIndex = wdAuto
    .Replacement.Font.Hidden = True
    .Execute Replace:=wdReplaceAll
    .ClearFormatting
    .Wrap = wdFindStop
    .Font.Hidden = False
    Do While .Execute
      With oRng
        StrClr = GetClr(.Characters.First.Font.Color, .Characters.First.Font.ColorIndex)
        If .Font.ColorIndex <> wdAuto Then
          Set oRngCompare = oRng.Duplicate
          With oRngCompare
            .Collapse wdCollapseStart
            .Text = "<" & StrClr & ">"
            .Font.ColorIndex = wdAuto
          End With
          .Start = oRngCompare.End
          Set oRngCompare = .Duplicate
          oRngCompare.Collapse wdCollapseStart
          If Asc(.Characters.Last) = 13 Then
            Do While .Characters.Last.Font.Color = .Characters.Last.Next.Font.Color
              .End = .End + 1
            Loop
          End If
          With .Duplicate
            For i = 1 To .Characters.Count
            If .Characters(i).Font.Color = .Characters(1).Font.Color Then
              oRngCompare.End = oRngCompare.End + 1
              oRngCompare.Select
            Else
              Exit For
            End If
            Next i
          End With
          .End = oRngCompare.End
          .Select
          .Collapse wdCollapseEnd
        .Text = "</" & StrClr & ">"
        .Font.ColorIndex = wdAuto
        End If
        .Collapse wdCollapseEnd
        If .End >= ActiveDocument.Range.End - 1 Then Exit Do
      End With
    Loop
  End With
  ActiveDocument.Range.Font.Hidden = False
  Application.ScreenUpdating = True
End Sub

I would like to thank all friends
After running the code ...
Error message comes ...»»»
Attached Files
File Type: zip 2.zip (198.2 KB, 6 views)
File Type: docm run vb (3).docm (23.6 KB, 6 views)

Last edited by bnyamin; 04-30-2017 at 07:06 PM.
Reply With Quote
  #11  
Old 04-30-2017, 03:16 PM
macropod's Avatar
macropod macropod is offline find colors then .... Windows 7 64bit find colors then .... Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by gmaxey View Post
I like your function! As you know, your use of "Find.Found" shivers my timbers so I try to avoid it.
Thanks Greg,
Yes, I know my .Find.Found loops trouble you, but they work for me!
Quote:
Originally Posted by gmaxey View Post
Tinkering with your code, I did notice that it can hiccup if there are color adjacent to each other. This can probably be refined
I was aware of the likely limitation re adjacent colours, but looking at the OP's sample there weren't any, so I wasn't going to further complicate the code unless it was an issue. ATM I'm more interested in why the code works for me but not, apparently, for the OP.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #12  
Old 04-30-2017, 07:04 PM
bnyamin bnyamin is offline find colors then .... Windows 7 64bit find colors then .... Office 2010 64bit
Advanced Beginner
find colors then ....
 
Join Date: Oct 2014
Posts: 36
bnyamin is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
I don't get an output anything like your latest attachment. See attached.

There is only one macro (named 'Demo') which calls a Function (named 'GetClr').
After the implementation and runing of the Code
we see
deferent error
Attached Files
File Type: docm run vb (2).docm (23.9 KB, 7 views)
File Type: zip 3.zip (180.9 KB, 8 views)
Reply With Quote
  #13  
Old 04-30-2017, 07:21 PM
macropod's Avatar
macropod macropod is offline find colors then .... Windows 7 64bit find colors then .... Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

I removed the colour tags in your document, then ran the macro that's already in it and it produced the correct results. See attached. I have no idea what you're doing...
Attached Files
File Type: docm run vb (2).docm (23.8 KB, 13 views)
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #14  
Old 05-01-2017, 01:18 AM
bnyamin bnyamin is offline find colors then .... Windows 7 64bit find colors then .... Office 2010 64bit
Advanced Beginner
find colors then ....
 
Join Date: Oct 2014
Posts: 36
bnyamin is on a distinguished road
Default

I did it
https://youtu.be/1qZIOX79JhA
Reply With Quote
  #15  
Old 05-01-2017, 01:28 AM
macropod's Avatar
macropod macropod is offline find colors then .... Windows 7 64bit find colors then .... Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

It might have helped had you said you were using an RTL language format for the basic document. Nowhere is that evident from anything you posted but it may account for the behaviour you're seeing. Try using it on a document using a LTR language format, then copying the annotated content into your other document.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
find colors then .... Customization of colors marif300 Project 3 02-23-2015 07:48 AM
find colors then .... Bar colors ketanco Project 1 03-30-2013 08:24 AM
find colors then .... Unable to change font colors from theme colors choy Word 3 08-01-2012 09:12 PM
find colors then .... Counting Colors g48dd Excel 2 03-13-2011 09:28 PM
Hyperlink colors pamm13 Word 2 02-18-2011 08:51 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:03 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft