2017-08-02 100 views
1

嗨我想創建一個腳本,更改特定文件夾中的所有單詞文檔的字體,我已經設法創建一個更改1文檔的字體,但無法工作如何爲所有的文件我的腳本如下Powershell腳本來更改字體文檔

$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"} 
+0

這是怎麼失敗的,並且你看到什麼證據的失敗? –

+0

本來我有$ Doc = $ word.Documents.Open(「C:\ Users \ andy.burton \桌面\佈局\ $文件夾\佈局\ 1.PAT」)工作正常,打開該特定文件,但我不能工作打開所有文檔打開所有文檔我試過$ Doc = $ word.Documents.Open()但是沒有做任何事 – Andy

回答

0

你會想通過$File數組使用foreach循環和循環做到這一點。您還可能需要重命名$File$Files

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

我試過並得到錯誤選擇文件夾名稱:Andy 調用Open時出錯。所需參數被省略。 在線:16 char:1 + $ Doc = $ word.Documents.Open() + ~~~~~~~~~~~~~~~~~~~~~~~~ ~~ + CategoryInfo:OperationStopped:(:) [],ArgumentException + FullyQualifiedErrorId:System.ArgumentException 您無法在空值表達式上調用方法。 在行:18字符:1 + $ Doc.Select() + ~~~~~~~~~~~~~ + CategoryInfo:InvalidOperation:(:) [],的RuntimeException + FullyQualifiedErrorId:InvokeMethodOnNull – Andy

+0

我認爲這一行是$ Doc = $ word.Documents.Open()的問題,但我不確定在()中放置什麼,以便它打開每個文件。 – Andy

+0

在循環期間,您將希望每次生成路徑新鮮。因此,使用數組您使用該信息來創建路徑,並把它放入'。開()'方法 –