Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-26-2019, 07:19 PM
guyglk guyglk is offline Check if there is more than one font color used in a document Windows 10 Check if there is more than one font color used in a document Office 2007
Novice
Check if there is more than one font color used in a document
 
Join Date: Dec 2017
Posts: 3
guyglk is on a distinguished road
Default Check if there is more than one font color used in a document


Hi. I'm trying to figure out how to find if a document uses more than one font color.

(Using Word 2007 and 2013)

I also needed to find if the document uses any highlighted text. This way fairly easy. I used the 'find' command for that. I can't seem to replicate the same principle for font color though.

I tried looping through every character, which works - but incredibly slowly - I had to deem it inapplicable. I don't know how the find command manages to find highlighted text at light speed.. almost regardless of document's character count.

Does anyone have any idea how I can achieve this (check if there is more than one font color used throughout) just as quickly? The colors are unknown/nonimportant - the only important thing for me here is to make sure that the document uses only one color for font, throughout.

Thank you!
Reply With Quote
  #2  
Old 01-26-2019, 10:23 PM
gmayor's Avatar
gmayor gmayor is offline Check if there is more than one font color used in a document Windows 10 Check if there is more than one font color used in a document Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,138
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Try the following
Code:
Sub MakeBlack()
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Font.Color = wdColorBlack
    With Selection.Find
        .Text = "*"
        .Replacement.Text = "^&"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchWildcards = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
 End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 01-26-2019, 10:49 PM
macropod's Avatar
macropod macropod is offline Check if there is more than one font color used in a document Windows 7 64bit Check if there is more than one font color used in a document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,382
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

Testing whether a document uses more than one font colour or highlight is as simple as:
Code:
Sub Demo()
With ActiveDocument.Range
  MsgBox "Font Colour: " & .Font.ColorIndex
  MsgBox "Highlight Colour: " & .HighlightColorIndex
End With
End Sub
If the returned value is 9999999 for either, there is more than one of that property in use (no highlight counts as one).
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 01-26-2019, 11:53 PM
guyglk guyglk is offline Check if there is more than one font color used in a document Windows 10 Check if there is more than one font color used in a document Office 2007
Novice
Check if there is more than one font color used in a document
 
Join Date: Dec 2017
Posts: 3
guyglk is on a distinguished road
Default

Thank you both!
I will try this..

Greatly appreciated, thanks!
Reply With Quote
  #5  
Old 01-28-2019, 03:56 PM
guyglk guyglk is offline Check if there is more than one font color used in a document Windows 10 Check if there is more than one font color used in a document Office 2007
Novice
Check if there is more than one font color used in a document
 
Join Date: Dec 2017
Posts: 3
guyglk is on a distinguished road
Default

Hi again,

Well, it turns out I need to exclude spaces and other characters when looking for any different font colors... so the two-liner bit, as excellent as it is - is too "document-generic" for me.

I thought about going at it this way, using the find command (the code below is a crude version of what I'm after):

Outline (just to be clear of what I was trying to achieve in plain English, in case I missed something code-wise):
1. Go to the first character in the document
2. Search for that color font (grab and use the first character's font color as criteria) using find while ignoring spaces
3. Check if find bumps into any different color other than that of which is being searched (if find.found=false I think...)
Code:
Selection.HomeKey Unit:=wdStory

Selection.Find.ClearFormatting
Selection.Find.Font.Color = Selection.Font.Color
With Selection.Find
    .Text = "*[! ]"
    .Font.Color = Selection.Font.Color
    .MatchWildcards = True
    .Wrap = wdFindContinue
    .Format = True
    .Forward = True
    .Execute
End With

If Selection.Find.Found = False Then MsgBox "Found another color." _
Else MsgBox "Did not find any other color."
The problem: it always gives "Did not find any other color." whether the document has only one font color used or more.

I'm not sure what to do next. Any ideas on how to improve this?

Thanks,
Reply With Quote
  #6  
Old 01-28-2019, 04:11 PM
macropod's Avatar
macropod macropod is offline Check if there is more than one font color used in a document Windows 7 64bit Check if there is more than one font color used in a document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,382
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

Fairly obviously, I'd have thought, you could test a document via code like:
If .Font.ColorIndex = 9999999 Then
from where you might take an approach like that demonstrated in https://social.msdn.microsoft.com/Fo...-word-document to compile a list of the colours in use (the code in the link tests for Styles, but the principle is the same). The main difference you'll need to consider is whether you need to return only colorindex values or whether you need to return values from the full RGB spectrum.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Check if there is more than one font color used in a document VBA change font color to background color donaldadams1951 Word VBA 6 05-31-2018 04:36 PM
Advanced Font Color Manipulation In Word: Inserting another color than the surrounding text stuartg Word 1 02-20-2017 12:38 AM
Check if there is more than one font color used in a document Remove white text background (keeping the font color and page color intact cc3125 Word 1 10-26-2015 06:44 PM
Font Color Question//.Replacement.Font.Color = 12611584 rsrasc Word VBA 3 09-05-2015 09:03 PM
Make a certain font face and font color as default in a document. Singh_Edm Word VBA 2 09-13-2014 08:47 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:46 PM.


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