简体   繁体   English

通过 PowerShell 更改 Word 文件的背景颜色

[英]Change the background color of a Word file via PowerShell

How can I change the background color of a Word file via PowerShell?如何通过 PowerShell 更改 Word 文件的背景颜色?

$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's 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'm getting 2 errors :我收到 2 个错误:

* RGB : The term 'RGB' is not recognized as the name of a cmdlet...
* Cannot find an overload for "Save" and the argument count: "1".

So lets address the couple of issues I know about...所以让我们解决我所知道的几个问题......

RGB : The term 'RGB' is not recognized as the name of a cmdlet... RGB :术语“RGB”不被识别为 cmdlet 的名称...

Of course you know why that is not working since PowerShell does not have a RGB cmdlet.当然,您知道为什么这不起作用,因为 PowerShell 没有 RGB cmdlet。 However if you look at the input for $doc.Background.Fill.ForeColor.RGB it is looking for an integer.但是,如果您查看$doc.Background.Fill.ForeColor.RGB的输入,它正在寻找一个整数。 Referring to this related 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 cause an error you are going to notice that the colour is not displayed on the document.虽然上面的代码不会导致错误,但您会注意到文档上没有显示颜色。 That is because backround colour is not displayed by default.这是因为默认情况下不显示背景颜色。 You will need to enable it.您将需要启用它。

$doc.Background.Fill.Visible = $true

Ok, that one is done now how about....好的,那一个现在完成了如何......

Cannot find an overload for "Save" and the argument count: "1".找不到“保存”和参数计数的重载:“1”。

We use Get-Member to see exactly why我们使用Get-Member来确切了解原因

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 does not take any parameters so we simply need to remove $true .它不需要任何参数,所以我们只需要删除$true The other entries are there just to show some other methods do take parameters.其他条目只是为了显示一些其他方法确实带参数。

$doc.Save()

.... so .... for the last one .... 所以.... 最后一个

True : The term 'True' is not recognized as the name of a cmdlet... True : 术语“True”未被识别为 cmdlet 的名称...

I did not see that error and will assume it was a typo that didnt get carried over into the code of your question.我没有看到那个错误,我会认为这是一个错字,没有被带到你的问题代码中。

Still miss RGB?还是想念RGB?

A crude PowerShell function that replicates the functionality with a little data validation一个粗略的 PowerShell 函数,通过少量数据验证来复制功能

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

Just to clarify I'm posting the final script here.只是为了澄清我在这里发布最终脚本。

This script will loop through ALL .doc files in the selected path.此脚本将遍历所选路径中的所有 .doc 文件 I encountered a problem though where the doc files were read-only , So I changed save() to SaveAs([ref]$name) at another folder and that fixed the problem.我遇到了一个问题,虽然 doc 文件是只读的,所以我在另一个文件夹中将save()更改为SaveAs([ref]$name)并解决了问题。

$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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM