We help IT Professionals succeed at work.

Control document backgrounds in MS Word with VBA

JohnRobinAllen asked
I can change the background colors and patterns of a document in MS Word (2010) by clicking on Page Layout, then on Page Color, and then I can choose all sorts of delicious backgrounds in different colors. If I use the VBA editor to record those changes, I get a script like this:
    ActiveDocument.Background.Fill.ForeColor.RGB = RGB(102, 153, 255)
    ActiveDocument.Background.Fill.Transparency = 0#
    ActiveDocument.Background.Fill.PresetTextured msoTextureDenim

(That is a very simple background, but it can easily get more complicated.) If I run such a macro on a blank document, nothing happens.
What should I do to get VBA to give me different backgrounds?

     --John Robin
Watch Question

I'm not familiar at all with Office 2010, so I will leave VBA suggestions for experts who are, but I have to wonder whether it would be easier just saving some documents as suitably named templates after creating your attractive layouts.  That would make it easy enough just to open whichever template you wanted, add content, and Save As a standard Word document, without having to start messing around with Macros.  After al, you're still going to have to save the document with the Macro, so why not just have a ready formatted template.  Of course, this does not answer your question, but it is what I would do personally.

Open a new Word document and save as a suitable name. Let's use a file name of "BlueDenimBackground".

Do as follows:
Alt + F11 to show the Visual Basic Editor.
It should open to:

"Project (BlueDenimBackground)
          Microsoft Word Objects

Click the View menu and select "Code" to see the code editor:
Try pasting the attached Macro code into the code pane.

Sub Format_Background()
' Format_Background Macro
' Macro recorded 02/02/2011 by JohnRobinAllen
    ActiveDocument.Background.Fill.Visible = msoTrue
    ActiveDocument.Background.Fill.ForeColor.RGB = RGB(102, 153, 255)
    ActiveDocument.Background.Fill.Transparency = 0#
    ActiveDocument.Background.Fill.PresetTextured msoTextureDenim
End Sub

Open in new window

You should see the drop-down list now showing your Macro name of "Format_Background".

File > Save BlueDenimBackground.

Close the VBA Editing Window then Word.
Reopen the saved document and, when prompted, "Enable Macros".
Tools > Macro > Macros > select "Format_Background" > Run.

That should work for you, but that's the code that should have been generated anyway (obviously as a different Macro and document name) when you "recorded" the Macro.

Hope this helps.

JohnRobinAllenRetired professor of French


Thanks very much for the suggestion. Actually what you describe was to be my fall-back solution, but I had hoped to make my program have as few templates as possible. I have to have some, for they contain other VBA macros, so it would not be too much to ask that I include a few more that have no code but just have pretty backgrounds.
     I'll leave the question open for a few more days to see if there is a VBA solution to my problem. If not, then your template solution will be the de facto solution.
     One regrets that MS appears to have stopped supporting VBA, which still has many bugs and deficiencies.

Hi John

On the contrary, VBA is still alive and well in Office 2010, and should be perfectly usable for relatively simple Macros like you were trying to get working:

Did you try pasting the VBA code I provided into the VB Editor in Word 2010?

JohnRobinAllenRetired professor of French


No, I did not try out your code, because I solved my problem with your solution, to use a template that already has the background I want. I will try your VBA code out soon, but in the meantime, I am awarding you the points for your solution. I may then post more comments after checking out your code and the links you give in the above message.
JohnRobinAllenRetired professor of French


My query was to get VBA code for controlling the background.

The solution is much simpler, and I am grateful for the suggestion.
Thank you JRA.  Glad you found the best method for your needs.