Can I change the background color of the page in Microsoft Word 2003 (2007, 2010, 2013) through a macro?
I am attempting to create a Word macro (Microsoft Word 2013) to change the page background color. I began by recording the keystrokes — this is the recorded macro:
Sub WritingLayout()
ActiveDocument.Background.Fill.ForeColor.RGB = RGB(255, 255, 204)
ActiveDocument.Background.Fill.Transparency = 0#
ActiveDocument.Background.Fill.PresetTextured msoTextureParchment
End Sub
This macro does not work on new documents.
Dirk Vollmar
171k53 gold badges256 silver badges313 bronze badges
asked Dec 11, 2015 at 11:39
5
I found how to do it. Just add this line before the vba code:
ActiveDocument.ActiveWindow.View.DisplayBackgrounds = True
Here is the entire macro:
Sub WritingLayout()
'
' WritingLayout Macro
'
ActiveDocument.ActiveWindow.View.DisplayBackgrounds = True
ActiveDocument.Background.Fill.ForeColor.RGB = RGB(255, 255, 204)
ActiveDocument.Background.Fill.Transparency = 0#
ActiveDocument.Background.Fill.PresetTextured msoTextureParchment
End Sub
Dirk Vollmar
171k53 gold badges256 silver badges313 bronze badges
answered Dec 12, 2015 at 14:21
0
I am attempting to create a Word macro (Microsoft Word Professional 10) to change the page background color. I began by recording the keystrokes — this is the recorded macro:
---
Sub WritingLayout()
'
' WritingLayout Macro
'
ActiveDocument.Background.Fill.Visible = msoTrue
ActiveDocument.Background.Fill.ForeColor.RGB = RGB(0, 43, 56)
ActiveDocument.Background.Fill.Solid
End Sub
---
This macro does not work on new documents. Once the page background color has been set, the macro works. Looking at the xml files, the only difference I can find is that after the page background has been set, there is an xml element <w:displayBackgroundShape/>
in the settings.xml file. Could this be the cause of the macro failure? If so, how do I set this in a macro?
asked Dec 20, 2014 at 1:02
2
Adding this line and running the macro with the document open in Print View inserts the settings.xml Element:
activedocument.ActiveWindow.View.DisplayBackgrounds = True
(Not easily deduced from the recorded code!)
answered Dec 20, 2014 at 15:12
0
04-05-2012, 10:59 AM
#1
Change Background Color on opening Word File
I’m hoping that someone can help me… I’m trying to develop a Word Document to assist a colleague with Dyslexia. She finds it easier to read an on-screen document if the background is pale pink and the text is dark blue. I have created a Word Doc in Word 2003 (the version at work) which has the following code:
Private Sub clickMe_Click()
Dim MyA, MyB, MyC, MyD, MyE, MyF As Integer
Dim dlgOpen As FileDialog‘Open the File Dialog Box to allow a file to be selected
Set dlgOpen = Application.FileDialog(FileDialogType:=msoFileDialogOpen)
With dlgOpen
.AllowMultiSelect = False
.Show
.ExecuteEnd With
‘Collect colour values from the master version
‘MyA to MyC are the background colours
MyA = txtA.Value
MyB = txtB.Value
MyC = txtC.Value‘MyD to MyF are the Font colours
MyD = txtD.Value
MyE = txtE.Value
MyF = txtF.Value‘Select the whole text from the document
Selection.WholeStory
‘Set the font color to the master version
Selection.Font.Color = RGB(MyD, MyE, MyF)
Selection.Font.Size = 14
Selection.Font.Name = «Arial»
Selection.HomeKey Unit:=wdStory‘Set the Active Document Background Color to the master version
ActiveDocument.Background.Fill.ForeColor.RGB = RGB(MyA, MyB, MyC)
ActiveDocument.Background.Fill.Visible = msoTrue
ActiveDocument.Background.Fill.SolidEnd Sub
This allows the user to click on a button, select the file she wants to open from her drives, and theoretically it opens the document with the altered standard colored text and background color.
Except it doesn’t always do it… Sometimes the text changes and the background stays the same.
In those cases (the background always changes in Web Layout, but we want it in Print Layout) to get it to work, you have to alter the background color manually and then press Ctrl+Z to «undo» and then the chosen salmon pink color appears as if by magic.
Can anybody help me please. She is already finding the assist of great help, but it could be better if it worked as required every time.
Regards,
Nick
09-27-2020, 09:58 PM |
|||
|
|||
Changing background colour in table based on value. Hi there, I am trying to change the color of some cells in a dropdown menu in a table (word document) I am only a fairly average skilled person when it comes to computers and would really appreciate some help. I would like the following to have a green shaded background Independent I would like the following to have a orange shaded background Assisted I would like the following to have a red shaded background
Marginal I have attached a clip of the table I have in excel to show you what I mean.. It is perfect but i can not get it into a word document. Thank you so much in advance |
09-27-2020, 11:06 PM |
__________________ |
09-27-2020, 11:46 PM |
|||
|
|||
Thank you so much, I did try that and it certainly works. How do I go in to change the words and also the yellow colour to orange ? |
09-28-2020, 12:05 AM |
You need to look closely at the code and adapt it to your requirements. I’m not sure exactly which code you chose. Colours can be explicitly defined in RGB terms for more flexibility in defining a colour. An example which would give you orange is Code: Case "Assisted": .Range.Cells(1).Shading.BackgroundPatternColorIndex = RGB(255,192,0)
__________________ |
09-28-2020, 04:53 PM |
|||
|
|||
ok thanks… what section do I go into to locate the formula to be able to modify? The code that seemed to work so far is the ‘content controls — dropdown conditional formatting.’ Thanks again |
09-28-2020, 05:55 PM |
It appears you are coming from ground zero in terms of using macros. How about you start with this for the basics I would also recommend you watch some youtube videos to get an understanding of what is involved in recording, editing and using VBA with Word. https://www.youtube.com/results?sear…Microsoft+Word Once you are comfortable with the VBA Editor and basic coding, the code you are looking for will be in the ThisDocument module of the template.
__________________ |
09-28-2020, 07:15 PM |
Quote:
Originally Posted by amandalee Thank you so much, I did try that and it certainly works. How do I go in to change the words and also the yellow colour to orange ? Further down the same thread is more code for changing the font colour: https://www.msofficeforums.com/103931-post21.html For orange, you could use code like: Code: Const wdOrange As Long = 41215 '... .Cells(1).Shading.BackgroundPatternColor = wdOrange .Font.Color = wdOrange or simply: Code: .Cells(1).Shading.BackgroundPatternColor = wdColorOrange .Font.Color = wdColorOrange
__________________ |
09-28-2020, 11:27 PM |
|||
|
|||
Thanks Thank you |
09-28-2020, 11:36 PM |
You haven’t declared the wdOrange constant: Code: Const wdOrange As Long = 41215 which should go on a new line after: Code: Const StrPwd As String = "abc"
__________________ |
09-29-2020, 04:57 PM |
|||
|
|||
Thank you, I have put in the line as you had suggested. The issue I have is that when I use the dropdown menu none of them change the background colour? ie it stays white it also comes up with the attached error? |
09-29-2020, 05:29 PM |
That End If line needs to move up two lines (after the End With line). The Title property on your CCs doesn’t match the code. Change this line Also note that if you are setting the fill and the text colour to exactly the same colour, you won’t be able to see the text. Your original request showed the background as a paler tint of the text colour. Once you have the code working, if you want the text to be visible we may need to ensure the background colour is not on 100% fill — but lets get the colour working first.
__________________ |
09-29-2020, 07:42 PM |
|||
|
|||
I have made the changes.. still not successful I’m afraid |
09-29-2020, 07:43 PM |
|||
|
|||
I would be really just as happy to have the font black and just change the background color to green orange and red if that makes it easier? |
09-29-2020, 08:43 PM |
Try this code instead. If you want subtle changes in colour to show text on a pastel background you need to use RGB values rather than sticking with limits of the built-in Word variables. Code: Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) Const StrPwd As String = "abc" With ContentControl Select Case .Title Case "Bondy Scale" If ActiveDocument.ProtectionType <> wdNoProtection Then ActiveDocument.Unprotect Password:=StrPwd With .Range .Cells(1).Shading.Texture = wdTextureNone Select Case .Text Case "Dependent", "Marginal" .Cells(1).Shading.BackgroundPatternColor = RGB(255, 109, 109) .Font.Color = wdColorRed Case "Assisted" .Cells(1).Shading.BackgroundPatternColor = RGB(255, 183, 67) .Font.Color = RGB(255, 160, 0) Case "Supervised", "Independent" .Cells(1).Shading.BackgroundPatternColor = wdColorLightGreen .Font.Color = wdColorBrightGreen Case Else .Cells(1).Shading.BackgroundPatternColor = wdColorWhite .Font.ColorIndex = wdAuto End Select End With ActiveDocument.Protect wdAllowOnlyFormFields, True, StrPwd End Select End With End Sub
__________________ |
09-29-2020, 08:56 PM |
|||
|
|||
Thank you Thank you — that works!! you are amazing!!! |
Hi Shauna
Many thanks for the code you sent me. Looks like a life saver!
Just to expand on this, could I pre-save the styles into my Word templates
and just call them when I need them?
Just thinking if this would save processor time as I have around 20 styles
to create.
Do I also need to format the text in these styles as the font sizes and the
like where a bit manic!
Hi All
Thanks for your feedback.
Been trying things out before you kindly sent me the below and went with the
highlight colour option, but what I can’t understand is that even though I’m
selecting the 2nd paragraph to initiate my highlighting it still covers all
paragraphs!!!
To explain I have a cell in my table consisting of the following:
BilboBaggins <vbCrLf>
Freddie Boggs<vbCfLf>
When I firstly format BilboBaggins it works perfectly I get the colour bg
that I want, but when I format Freddie Boggs the bg colour for both is
changed to what it should be for Freddie Boggs even though my code quite
clearly calls the 2nd paragraph, eg:
Set oDoc = oNewDoc.Tables(1).Cell(intRow, intCol).Range.Paragraphs(2)
oDoc.Range.Text = arrPupilData(0, intCurrentRec)
With oDoc.Range
.Font.Color = wdBlack
.HighlightColorIndex = wdRed
.Font.Italic = False
.Font.Bold = False
.Font.Underline = False
End With
Surely the above should only format the Freddie Boggs text??
Another thing is that I use the following line at the end:
oDoc.Range.Text = oDoc.Range.Text & vbCrLf
but instead of this just putting one paragraph/carriage return marker after
the name, eg Freddie Boggs, it puts 2 markers!
If I use .Doc.Range.InsertParagraphAfter instead of the above it puts the
right markers in, but crashes on any cells with more than 1 name in it — how
frustrating is this!!!!!
Any ideas?
Hi Blackberry
I suggest using character styles that have been defined to have the shading
you want. If you apply the styles to a .Range, then the shading will be
applied only to the text, and not to the whole paragraph.
Here’s some code to get you going. To test it out, create a document with a
table and put some text in each cell of the table.
Sub FormatBloggs()
Dim oNewDoc As Word.Document
Dim sBlueStyle As String
Dim sWhiteStyle As String
Dim sRedStyle As String
Dim styBlue As Word.Style
Dim styWhite As Word.Style
Dim styRed As Word.Style
Dim oTable As Word.Table
Dim nRowCounter As Long
Dim nColCounter As Long
‘Get a reference to our document
Set oNewDoc = ActiveDocument
‘Establish the names of our styles
sBlueStyle = «Blue»
sWhiteStyle = «White»
sRedStyle = «Red»
On Error Resume Next
‘If necessary, create new styles in our document
With oNewDoc.Styles
.Add Name:=sBlueStyle, Type:=wdStyleTypeCharacter ‘
wdStyleTypeParagraph
.Add Name:=sWhiteStyle, Type:=wdStyleTypeCharacter ‘
wdStyleTypeParagraph
.Add Name:=sRedStyle, Type:=wdStyleTypeCharacter
‘wdStyleTypeParagraph
End With
On Error GoTo 0
‘Get a reference to our styles
With oNewDoc.Styles
Set styBlue = .Item(sBlueStyle)
Set styWhite = .Item(sWhiteStyle)
Set styRed = .Item(sRedStyle)
End With
‘Identify the shading we want for our styles
With styBlue.Font.Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorBlue
End With
With styWhite.Font.Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorWhite
End With
With styRed.Font.Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorRed
End With
On Error Resume Next
‘Apply the character styles to text in our table
Set oTable = oNewDoc.Tables(1)
For nRowCounter = 1 To oTable.Rows.Count
For nColCounter = 1 To oTable.Columns.Count
oTable.Cell(nRowCounter,
nColCounter).Range.Paragraphs(1).Range.Style = styBlue
oTable.Cell(nRowCounter,
nColCounter).Range.Paragraphs(2).Range.Style = styWhite
oTable.Cell(nRowCounter,
nColCounter).Range.Paragraphs(3).Range.Style = styRed
Next nColCounter
Next nRowCounter
End Sub
I’d suggest that (a) you’ll need to add appropriate error checking and (b)
use names for the styles that reflect function, not format. Blue, White and
Red are not good names!
Hope this helps.
Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word