Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #5  
Old 10-30-2013, 07:20 AM
gmaxey gmaxey is offline Converting color codes in VBA Windows 7 32bit Converting color codes in VBA Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,636
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

That number doesn't have to be 7 or 8 digits. It can be three and it represents a long data type.

Experiment with this in a new document:

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
ActiveDocument.Range.Text = "ABC...XYZ"
'Assign color using constants.
ActiveDocument.Range.Characters(1).Font.Color = wdColorRed
ActiveDocument.Range.Characters(2).Font.Color = wdColorGreen
ActiveDocument.Range.Characters(3).Font.Color = wdColorBlue
'Color property returns a long data type
Debug.Print ActiveDocument.Range.Characters(1).Font.Color
Debug.Print ActiveDocument.Range.Characters(2).Font.Color
Debug.Print ActiveDocument.Range.Characters(3).Font.Color
'Assign color using long values.
ActiveDocument.Range.Characters(4).Font.Color = 255
ActiveDocument.Range.Characters(5).Font.Color = 32768
ActiveDocument.Range.Characters(6).Font.Color = 16711680
'What are the RGB values?
Debug.Print fcnLongToRGB(255) 'Red
'Green has a long value 32768
Debug.Print fcnLongToRGB(32768) 'Green
'Blue as a long value 16711680
Debug.Print fcnLongToRGB(16711680) 'Blue
'Assign color using RGB values
ActiveDocument.Range.Characters(7).Font.Color = RGB(255, 0, 0)
ActiveDocument.Range.Characters(8).Font.Color = RGB(0, 128, 0)
ActiveDocument.Range.Characters(9).Font.Color = RGB(0, 0, 255)
End Sub
Code:
Function fcnLongToRGB(ByRef lngColor As Long) As String
Dim lngRed As Long, lngGreen As Long, lngBlue As Long
lngRed = lngColor Mod 256
lngGreen = (lngColor \ 256) Mod 256
lngBlue = (lngColor \ 256 \ 256) Mod 256
fcnLongToRGB = "RGB(" & lngRed & "," & lngGreen & "," & lngBlue & ")"
End Function
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
 

Tags
color, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting color codes in VBA Codes Popping Up In My Text? Faedrie Word 1 11-08-2012 01:00 AM
Converting color codes in VBA Codes for Word Textbox Font Color? tinfanide Word VBA 7 10-23-2012 03:13 PM
Converting color codes in VBA Losing VBA codes in .dotm tinfanide Word VBA 15 04-12-2012 12:39 AM
Converting color codes in VBA Outline Codes eliz.bell Word 4 03-28-2012 07:27 PM
Why run-time is always different from stepping into the codes? tinfanide Excel Programming 1 03-04-2012 12:04 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:30 AM.


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