Powershell脚本更改单词docs的字体

时间:2017-08-02 13:32:55

标签: powershell ms-word

您好我正在尝试创建一个更改特定文件夹中所有word文档字体的脚本,我已设法创建一个更改1个文档的字体,但无法解决如何为我的脚本的所有文件执行此操作在下面

$Folder = Read-Host "Select Folder name"
$test = Test-Path C:\Users\andy.burton\Desktop\Layouts\$Folder\
$File = @
("0.bil","0.est","0.lbl","1.arm","1.bil","1.crd","1.env","1.est","1.frm","1.gp",
"1.hos","1.ins","1.lbl","1.lc","1.lmr","1.mls","1.NON","1.OP","1.pat","1.PRS
","1.rcl","1.rec","1.rmd","1.stm","10.pat","10.rec","11.pat","11.rec","12.pa
t","12.rec","13.pat","13.rec","14.PAT","14.rec","15.pat","16.pat","17.pat","18.pat","2.arm","2.bil","2.env","2.est","2.frm","2.gp","2.hos","2.ins","2.lbl","2.lc","2.lmr","2.mls","2.NON","2.pat","2.rcl","2.rec","2.rmd","2.stm","3.arm","3.bil","3.env","3.est","3.gp","3.hos","3.ins","3.lbl","3.lc","3.lmr","3.NON","3.pat","3.rec","3.rmd","3.STM","4.arm","4.bil","4.env","4.est","4.gp","4.hos","4.ins","4.lbl","4.lc","4.lmr","4.non","4.pat","4.rec","4.rmd","4.STM","5.arm","5.bil","5.env","5.est","5.gp","5.hos","5.ins","5.lbl","5.lc","5.lmr","5.non","5.pat","5.rec","5.rmd","6.bil","6.env","6.est","6.lbl","6.pat","6.rec","7.env","7.lbl","7.pat","7.rec","8.ENV","8.LBL","8.pat","8.rec","9.env","9.lbl","9.pat","9.rec","acchead.doc","address.lbl","apptreminder.email","apptreminder.sms","BMIBOOK.FRM","BUPA.OCR","clinicprint.rep","clinicprint2.rep","clinicprint2B.rep","clinicprint2C.rep","CLUB.BIL","Consent1.doc","consent2.doc","DEPOSIT.REC","ebs021.doc","ebs022.doc","EBS023.DOC","FConsent1.DOC","FEBS021.DOC","FEBS023.DOC","GP.OP","HCABOOK.FRM","InvCen.doc","InvFoot.doc","INVOICE.LBL","InvoiceGrid.doc","InvoiceGridNonVat.doc","InvoiceGridVAT.doc","InvoiceTotals.doc","InvoiceTotalsNonVAT.doc","InvoiceTotalsVAT.doc","J8160-95.LBL","J8160.LBL","J8162-95.LBL","J8162.LBL","J8163-95.LBL","J8163.LBL","J8165-95.LBL","J8165.LBL","J8360-95.LBL","J8360.LBL","J8362-95.LBL","J8362.LBL","J8363-95.LBL","J8363.LBL","J8365-95.LBL","J8365.LBL","J8560-95.LBL","J8560.LBL","J8562-95.LBL","J8562.LBL","J8563-95.LBL","J8563.LBL","J8565-95.LBL","J8565.LBL","L7160-95.LBL","L7160.LBL","L7161-95.LBL","L7161.LBL","L7162-95.LBL","L7162.LBL","L7163-95.LBL","L7163.LBL","L7164-95.LBL","L7164.LBL","L7165-95.LBL","L7165.LBL","L7166-95.LBL","L7166.LBL","L7167-95.LBL","L7167.LBL","L7168-95.LBL","L7168.LBL","L8162-95.LBL","letfoot.doc","lethead.doc","MC.BIL","NHS.PRS","NUFFBOOK.FRM","OPNOTE.OP","RecCen.doc","RECEIPT2.CC","ReceiptTotals.doc","recfoot.doc","remfoot.doc","shoulder1.jpg","shoulder2.jpg","TDL.FRM","TDL2.FRM","theatreprint.rep","VOUCHER2.CC")



if($test -eq $False) {

Copy-Item -Path 'C:\Users\andy.burton\desktop\Layouts\Arial 10' -Destination C:\Users\andy.burton\Desktop\Layouts\$Folder\ -Recurse


$file.ForEach({ 


$Word = New-Object -ComObject Word.Application

$Word.Visible = $False

$Doc = $word.Documents.Open()

$Selection = $word.Selection

$Doc.Select()

$Selection.Font.Name = "Calibri"

$Selection.Font.Size = 11

$Doc.Close()

$Word.Quit()

})


}


Else {Write-Warning "Folder Already Exists"}

2 个答案:

答案 0 :(得分:0)

您将需要使用Foreach循环并循环遍历$File数组。您可能还想将$File重命名为$Files

foreach($file in $Files)
{
     # peform font change here
}

答案 1 :(得分:0)

我需要共享更改一堆Word文档的字体,Internet似乎要回到这篇文章,以获取有关使用Powershell更改整个Word文档的字体的结果和参考。这段代码和答案没有很清楚地显示出来,因此我贡献了这段代码。此代码获取与$ Files部分中列出的关键字匹配的文件夹中的word文档,然后将字体更改为$ Selection.Font.Name =部分中输入的字体。我修改了这篇文章的原始代码以构建它,并且已经在本地文件夹和服务器共享上对其进行了测试。请让我知道是否可以批准。例如,最好输出到屏幕上正在更改的每个文件。

$Folder = "\\SERVER\SHARE\FOLDER\"
$Files = Get-ChildItem -Path $Folder -Recurse -Include *misc*.doc, *.misc*.docx, *Misc*.docx, *Misc*.doc, *MISC.docx, *MISC*.doc, *test*.doc, *test*.docx, whatever.doc

foreach($File in $Files)
{
    $Word = New-Object -ComObject Word.Application
    $Word.Visible = $false
    $Doc = $Word.Documents.Open($File.FullName)
    $Selection = $Word.Selection
    $Doc.Select()
    $Selection.Font.Name = "Verdana Pro SemiBold"
    $Doc.Close()
    $Word.Quit()
}

Write-Warning "Finshed With Font Change"