![]() |
#1
|
|||
|
|||
![]()
Hi.
Would like to change background color, and font color with a macro. But I want them all to be in a order on the same shortcut. For each time I press the shortcut, It will change the color. Is there a way to do that? Color combinations I want on "F9": | B:White F:Black | B: Black F:White | B:Black F:Yellow | I also got a problem when recording. I record changing the font color from white to red/yellow/blue/green etc. When I run the macro, it will change the color from white to black when. What is wrong? |
#2
|
||||
|
||||
![]()
What you're asking for isn't feasible, since there's no way for the macro to store the details of the last change. Setting up some logic to change white to black, black to yellow and yellow to white, in sequence, is easy enough. But not going from white to black, black to either yellow or white. Indeed, it's not at all clear what the criteria are for the final black to yellow to be triggered as an alternative to the black to white.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
It don't need to remember my last choice, as long as I could use the same button to swith between them.
1 push: Black on white. 2 pushes: White on black. 3 pushes: Yellow on black. If i push to times quickly, I will get white on black. If I press 3 times, it will be Yellow on black. If I got White on black, and want black on white, It's okey to press one time. Is this possible? |
#4
|
||||
|
||||
![]()
Try:
Code:
Sub Colorize() With Selection.Range Select Case .Font.ColorIndex Case wdAuto, wdBlack .Font.ColorIndex = wdWhite .HighlightColorIndex = wdBlack Case wdWhite .Font.ColorIndex = wdYellow .HighlightColorIndex = wdBlack Case wdYellow .Font.ColorIndex = wdAuto .HighlightColorIndex = wdNoHighlight End Select End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]()
Thanks Macropod! That worked just fine.
when I said background color, I ment the whole paper, not just the line behind the text, and is it possible for the macro to mark the whole text and change like (CTRL+A), and then re-mark the text, so I don't delete it all when I start to write :-) Again, thank you so much ![]() |
#6
|
||||
|
||||
![]()
Simply change 'Selection' to 'ActiveDocument'.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
![]()
Thanks again :-)
If I understand the macro right, I would like to change the "HighlightColorIndex" into something like "Background(...)", so the background would change color, and not the Highlight-color. You have a formula for that one? |
#8
|
||||
|
||||
![]()
You could try changing '.HighlightColorIndex' to '.Shading.BackgroundPatternColorIndex'. I'm not sure how well that will work with coloured fonts, though.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
![]()
Thanks again. Really appreciate your help
![]() That one made a black box around the text. I think it's easier to use two macros. One for font color, and one for page/background color. Manually I can change the background color. My Office is not in english, so I don't know the english button label, but maybe something like "page color / fill color". This button changes the color for the whole page, as if i could print a yellow page. Is there a macro for changing between Yellow, White and Black fill color on same short key, just like the font color? |
#10
|
||||
|
||||
![]()
It wasn't apparent from what you had previously posted that you wanted the entire page to change colour, and not just the text. For that, you could use:
Code:
Sub Colorize() With ActiveDocument .Background.Fill.Visible = msoTrue Select Case .Range.Font.ColorIndex Case wdAuto, wdBlack .Range.Font.ColorIndex = wdWhite .Background.Fill.BackColor.TintAndShade = wdBlack Case wdWhite .Range.Font.ColorIndex = wdYellow .Background.Fill.BackColor.TintAndShade = wdBlack Case wdYellow .Range.Font.ColorIndex = wdAuto .Background.Fill.BackColor.TintAndShade = wdNoHighlight .Background.Fill.Visible = msoFalse End Select End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
![]()
This was really not easy. Programming in VBA is clearly not my strength. I tried to use your latest code, but it don't seem to be stable. After 10-20 executions, it stops working. Now I have made four macros, which I want into one "Select Case" (?). If you could do that for me, I would really much appreciate that! Have used 4 hour at work with this, but are about to give up.
Code:
Sub HvitBlatt() ActiveDocument.Range.Font.ColorIndex = wdWhite ActiveDocument.Background.Fill.ForeColor.RGB = RGB(0, 0, 255) ActiveDocument.Background.Fill.ForeColor.TintAndShade = 0 ActiveDocument.Background.Fill.Visible = msoTrue ActiveDocument.Background.Fill.Solid End Sub ' ' ' ' Sub SvartGul() ActiveDocument.Range.Font.ColorIndex = wdBlack ActiveDocument.Background.Fill.ForeColor.RGB = RGB(255, 255, 0) ActiveDocument.Background.Fill.ForeColor.TintAndShade = 0 ActiveDocument.Background.Fill.Visible = msoTrue ActiveDocument.Background.Fill.Solid End Sub ' ' ' ' Sub HvitSvart() ActiveDocument.Range.Font.ColorIndex = wdBlack ActiveDocument.Background.Fill.ForeColor.RGB = RGB(255, 255, 255) ActiveDocument.Background.Fill.ForeColor.TintAndShade = 0 ActiveDocument.Background.Fill.Visible = msoTrue ActiveDocument.Background.Fill.Solid End Sub ' ' ' ' Sub SvartHvitt() ActiveDocument.Range.Font.ColorIndex = wdWhite ActiveDocument.Background.Fill.ForeColor.RGB = RGB(0, 0, 0) ActiveDocument.Background.Fill.ForeColor.TintAndShade = 0 ActiveDocument.Background.Fill.Visible = msoTrue ActiveDocument.Background.Fill.Solid End Sub ' |
#12
|
||||
|
||||
![]()
Your four macros are equivalent to:
Code:
Sub Colorize() With ActiveDocument With .Background.Fill .ForeColor.TintAndShade = 0 .Visible = msoTrue .Solid End With Select Case .Range.Font.ColorIndex Case wdWhite Select Case .Background.Fill.ForeColor.RGB Case RGB(0, 0, 255) 'Blue .Background.Fill.ForeColor.RGB = RGB(0, 0, 0) 'Black Case RGB(0, 0, 0) 'Black .Range.Font.ColorIndex = wdAuto .Background.Fill.ForeColor.RGB = RGB(255, 255, 255) 'White End Select Case wdAuto, wdBlack Select Case .Background.Fill.ForeColor.RGB Case RGB(255, 255, 255) 'White .Background.Fill.ForeColor.RGB = RGB(255, 255, 0) 'Yellow Case RGB(255, 255, 0) 'Yellow .Range.Font.ColorIndex = wdWhite .Background.Fill.ForeColor.RGB = RGB(0, 0, 255) 'Blue End Select End Select End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#13
|
|||
|
|||
![]()
Thanks! This is the code I ended up with. There is one issue thou! Some times it will not execute if the documents start with yellow background fill. And some times it will not execute if the document starts with black background fill.
Is there a reason for that? There is no problem to start with a black background, but it don's seem consequent. Code:
Sub BytteFargekombinasjon() With ActiveDocument With .Background.Fill .ForeColor.TintAndShade = 0 .Visible = msoTrue .Solid End With Select Case .Range.Font.ColorIndex Case wdWhite 'HvitSkrift Select Case .Background.Fill.ForeColor.RGB Case RGB(0, 0, 255) 'BlåBakgrunn .Background.Fill.ForeColor.RGB = RGB(0, 0, 0) 'SvartBakgrunn Case RGB(0, 0, 0) 'Svart .Range.Font.ColorIndex = wdAuto .Background.Fill.ForeColor.RGB = RGB(255, 255, 255) 'HvitBakgrunn End Select Case wdAuto, wdBlack 'SvartSkrift Select Case .Background.Fill.ForeColor.RGB Case RGB(255, 255, 255) 'HvitBakgrunn .Background.Fill.ForeColor.RGB = RGB(255, 255, 0) 'GulBakgrunn Case RGB(255, 255, 0) .Range.Font.ColorIndex = wdWhite .Background.Fill.ForeColor.RGB = RGB(0, 0, 255) 'BlåBakgrunn End Select End Select End With End Sub Last edited by macropod; 06-30-2014 at 12:02 AM. Reason: Added code formatting |
#14
|
||||
|
||||
![]()
The way the code works, it looks first at the font colour, to see whether it's white or black. If it's not either of those colours, it does nothing.
If the font is white, the code looks at the background colour, to see whether it's blue or black. If it's not either of those colours, it does nothing. If the font is 'auto' (black), the code looks at the background colour, to see whether it's white or yellow. If it's not either of those colours, it does nothing. So, if you start off with a document that has red text, the macro does nothing. It also does nothing if the document has white text and a yellow background or black text and a pink background, for example. One of the problems with checking against RGB colours is that what might look white, for example, might have RGB(255, 255, 254) or RGB(255, 254, 255) or RGB(254, 255, 255) or RGB(254, 254, 254), amongst others, instead of RGB(255, 255, 255). So you might want a 'Case Else' result for both the font colour and the background. For example: Code:
Sub BytteFargekombinasjon() With ActiveDocument With .Background.Fill .ForeColor.TintAndShade = 0 .Visible = msoTrue .Solid End With Select Case .Range.Font.ColorIndex Case wdWhite 'HvitSkrift Select Case .Background.Fill.ForeColor.RGB Case RGB(0, 0, 255) 'BlåBakgrunn .Background.Fill.ForeColor.RGB = RGB(0, 0, 0) 'Svart Case Else .Range.Font.ColorIndex = wdAuto .Background.Fill.ForeColor.RGB = RGB(255, 255, 255) 'HvitSkrift End Select Case wdAuto, wdBlack Select Case .Background.Fill.ForeColor.RGB Case RGB(255, 255, 255) 'HvitSkrift .Background.Fill.ForeColor.RGB = RGB(255, 255, 0) 'GulBakgrunn Case Else .Range.Font.ColorIndex = wdWhite 'HvitSkrift .Background.Fill.ForeColor.RGB = RGB(0, 0, 255) 'BlåBakgrunn End Select Case Else .Range.Font.ColorIndex = wdAuto .Background.Fill.ForeColor.RGB = RGB(255, 255, 255) 'HvitSkrift End Select End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
gsrikanth | Excel | 8 | 11-05-2022 04:10 AM |
![]() |
sparkync | Office | 3 | 08-04-2012 03:26 PM |
![]() |
nja | Word VBA | 1 | 10-24-2009 05:07 AM |
Where is the MS Office Shortcut Bar? | vwm12345 | Office | 0 | 11-14-2008 04:30 AM |
Style shortcut | billzant | Word | 0 | 01-06-2007 02:13 AM |