Apply a background solid color to a Word Document

How to apply a solid background for the Word Document using osascript in MAC

document.Background.Fill.ForeColor.ObjectThemeColor = Word.WdThemeColorIndex.wdThemeColorMainDark1;
document.Background.Fill.ForeColor.TintAndShade = 0.5;
document.Background.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
app.ActiveWindow.View.DisplayBackgrounds = true;

Couldn’t able to fetch the background object and fill format for the same in MAC. In MAC background shape is used for a different purpose

I think it’s used for the same purpose.

If you get the properties of a document, it should return a laundry list that includes the following:

properties of document 1
--> background shape:background shape of document 1 

So then, we can get its properties and among them:

properties of background shape of document 1
--> fill format:fill format of background shape of document 1

Continuing down that path but this time we’re looking for two properties:

properties of fill format of background shape of document 1
--> fore color:{65535, 65535, 65535} -- aka white
--> visible:true

Now we have enough to set the background colour of the document, say to a nice but legibility-defeating blue:

tell application "Microsoft Word"
	set fore color of fill format of background shape of document 1 to {0, 0, 32767}
	set visible of fill format of background shape of active document to true
end tell

As far as I can tell, documents default to having this visibility set to true so that line may be redundant but I include it here to help with troubleshooting. The background shape also has a visible property so conceivably, either or both may require setting.

You say you want this to run this using osascript so here is the command to change background colour:

% osascript -e "tell application \"Microsoft Word\" to set 
fore color of fill format of background shape of document 
\"Document1\" to {64507, 54484, 46260}"

NB I broke it in three for readability so you’ll have to reform it into a single line. If you end up requiring the visible property, you’ll have to add that as well.

Updated: to fix visible command

1 Like

@Mockman

tell application "Microsoft Word"
    activate
    set fore color of fill format of background shape of active document to {0, 0, 32767}
    set visible of active document to true
end tell

This is the error that, I receive
page_color.scpt:51:136: execution error: Microsoft Word got an error: Can’t set active document to {0, 0, 32767}. (-10006)

I screwed up. The property should be made visible, not the document (which presumably already is).

set visible of fill format of background shape of active document to true

Note that both fill format and background shape have a visible property but fore color does not. So conceivably you may need to set both.

	set bs1 to background shape of active document
	set ff1 to fill format of bs1
	set fc1 to fore color of ff1

	visible of bs1
	--> true
	visible of ff1
	--> true

As to your -10006 error… that’s a strange one: The write error was denied

active document is read only and offhand, it implies that you’re trying to set it, which matches the error message.

Sorry mate that doesn’t work as well

Trying this out in MAC Mini M1 (Ventura - 13.3.1 (22E261))

tell application "Microsoft Word"
    activate
    tell its active document
    set fore color theme index of fill format of background shape to fifth accent theme color
    --set transparency of ff1 to 0.5
    end tell
end tell

page_color.scpt:113:124: execution error: Can’t get fill format of missing value. (-1728)

set fore color theme index of ff1 to fifth accent theme color

Remove the its from the tell active document line as it is redundant. It’s actually referring to Word here.

Then use it (or its, depending) to refer to the active document inside the tell block, for example:

set fore color theme index of fill format of background shape of it to fourth accent theme color

I shouldn’t think it will ever be it's.

I guess I got carried away there.

Apologies, the suggested things aren’t working