Change the background color of a Word file using PowerShell
How to cn change the background color of a Word file using PowerShell?
$wd = New-Object -COM 'Word.Application'
$wd.Visible = $true # set to $false for production
Get-ChildItem 'C:\1\*.doc' | % {
$doc = $wd.Documents.Open($_.FullName)
# Here the problem
$doc.Background.Fill.ForeColor.RGB = RGB(192, 192, 192)
# Switch doc view to Online Layout view
$doc.ActiveWindow.View.Type = 6
$doc.Save($true)
$doc.Close()
}
$wd.Quit()
[Runtime.InteropServices.Marshal]::ReleaseComObject($wd)
[GC]::Collect()
[GC]::WaitForPendingFinalizers()
I am getting 2 errors:
* RGB : The term 'RGB' is not recognized as the name of a cmdlet...
* Cannot find an overload for "Save" and the argument count: "1".
source to share
So let's take a look at a couple of problems that I know of ...
RGB: The term "RGB" is not recognized as a cmdlet name ...
Of course, you know why this doesn't work, since PowerShell doesn't have an RGB cmdlet. However, if you look at the input for $doc.Background.Fill.ForeColor.RGB
, it looks for an integer. By referring to the article , you can see how we can make the transition.
$doc.Background.Fill.ForeColor.RGB = [long](192 + (192* 256) + (192 * 65536))
There is a caveat that needs to be addressed. While the above code does not throw an error, you will notice that the color does not appear in the document. This is because the background color is not displayed by default. You will need to enable it.
$doc.Background.Fill.Visible = $true
Ok, now it's done like oh ....
Unable to find overload for "Save" and number of arguments: "1".
We use Get-Member
to understand exactly why
PS C:\users\Cameron\Downloads> $doc | gm
TypeName: Microsoft.Office.Interop.Word.DocumentClass
...
RunLetterWizard Method void RunLetterWizard([ref] System.Object LetterContent, [ref] System.Object WizardMode), void _Document.Ru...
Save Method void Save(), void _Document.Save()
SaveAs Method void SaveAs([ref] System.Object FileName, [ref] System.Object FileFormat, [ref] System.Object LockComments...
...
It takes no parameters, so we just need to delete $true
. The rest of the entries just show that some of the other methods take parameters.
$doc.Save()
.... so .... for the latter
True: The term "True" is not recognized as a cmdlet name ...
I haven't seen this error, and I'm guessing it was a typo that didn't make it into the code of your question.
Skip RGB?
A raw PowerShell function that replicates functionality with little data validation
Function Get-RGB
{
Param(
[Parameter(Mandatory=$false)]
[ValidateRange(0,255)]
[Int]
$Red = 0,
[Parameter(Mandatory=$false)]
[ValidateRange(0,255)]
[Int]
$Green = 0,
[Parameter(Mandatory=$false)]
[ValidateRange(0,255)]
[Int]
$Blue = 0
)
Process
{
[long]($Red + ($Green * 256) + ($Blue * 65536))
}
}
Example
PS C:\users\Cameron\Downloads> Get-RGB 129 0 54
3539073
source to share
Just to clarify, I'm posting the final script here.
This script will loop through ALL .doc files in the selected path. I ran into the problem even though the doc files were read-only, so I changed save()
to SaveAs([ref]$name)
in a different folder and fixed the problem.
$wd = New-Object -COM 'Word.Application'
$wd.Visible = $true # set to $false for production
Get-ChildItem 'C:\songs\*.doc' | % {
$doc = $wd.Documents.Open($_.FullName)
$doc.Background.Fill.ForeColor.RGB = [long](249 + (232* 256) + (163 * 65536))
$doc.Background.Fill.Visible = $true
# Switch doc view to Online Layout view, otherwise the changes won't appear in normal view
$doc.ActiveWindow.View.Type = 6
# Replace folder path to solve "read-only" problem
$Name=($doc.Fullname).replace("songs","songs_edited")
$doc.SaveAs([ref]$Name)
$doc.Close()
}
$wd.Quit()
[Runtime.InteropServices.Marshal]::ReleaseComObject($wd)
[GC]::Collect()
[GC]::WaitForPendingFinalizers()
source to share