繁体   English   中英

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

[英]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()

我收到 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".

所以让我们解决我所知道的几个问题......

RGB :术语“RGB”不被识别为 cmdlet 的名称...

当然,您知道为什么这不起作用,因为 PowerShell 没有 RGB cmdlet。 但是,如果您查看$doc.Background.Fill.ForeColor.RGB的输入,它正在寻找一个整数。 参考这篇相关文章,您可以了解我们如何进行转换。

$doc.Background.Fill.ForeColor.RGB = [long](192 + (192* 256) + (192 * 65536))

有一个警告需要解决。 虽然上面的代码不会导致错误,但您会注意到文档上没有显示颜色。 这是因为默认情况下不显示背景颜色。 您将需要启用它。

$doc.Background.Fill.Visible = $true

好的,那一个现在完成了如何......

找不到“保存”和参数计数的重载:“1”。

我们使用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...
...

它不需要任何参数,所以我们只需要删除$true 其他条目只是为了显示一些其他方法确实带参数。

$doc.Save()

.... 所以.... 最后一个

True : 术语“True”未被识别为 cmdlet 的名称...

我没有看到那个错误,我会认为这是一个错字,没有被带到你的问题代码中。

还是想念RGB?

一个粗略的 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))
    } 
}

例子

PS C:\users\Cameron\Downloads> Get-RGB 129 0 54
3539073

只是为了澄清我在这里发布最终脚本。

此脚本将遍历所选路径中的所有 .doc 文件 我遇到了一个问题,虽然 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