如何避免洗掉只讀,我需要在沒有只讀的情況下正常使用excel。我有以下代碼:
$excelfile="C:\Users\Administrator\Pictures\unprotect_org - Copy\unprotect - Copy (2).xlsx"
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$excel.DisplayAlerts = $false
$wb = $excel.Workbooks.Open($excelfile,$true,123)
$wb.SaveAs($excelfile,[Type]::Missing,$password)
$excel.Quit()
我收到以下錯誤:
Cannot save as that name. Document was opened as read-only.
At line:1 char:1
$wb.SaveAs($excelfile,[Type]::Missing,$password)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : OperationStopped: (:) [], COMException
FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

第二件事,為什么microsoft execel在任務管理器中打開,我怎樣才能關閉這個exe。

請幫我解決這個錯誤。
uj5u.com熱心網友回復:
您需要更改兩件事 - 以讀寫模式打開,并確保Quit()無論終止錯誤如何都會被呼叫。
為避免以只讀模式打開作業簿,請將傳遞給的第三個引數引數的值更改為Open():
$wb = $excel.Workbooks.Open($excelfile,$true,$false)
# ^
# This is the ReadOnly parameter
要確保Quit()始終呼叫,請使用try/finally陳述句:
$excelfile="C:\Users\Administrator\Pictures\unprotect_org - Copy\unprotect - Copy (2).xlsx"
$excel = New-Object -ComObject Excel.Application
try {
$excel.Visible = $false
$excel.DisplayAlerts = $false
$wb = $excel.Workbooks.Open($excelfile,$true,123)
$wb.SaveAs($excelfile,[Type]::Missing,$password)
}
finally {
$excel.Quit()
}
如果 PowerShell 到達try塊內的第一條陳述句,它保證finally塊將在從塊回傳控制權之前執行 - 即使呼叫SaveAs()(或任何其他呼叫)拋出終止例外
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/433515.html
上一篇:Powershellexcel(xlsx)如何使用知道密碼取消保護excel作業表和excel作業簿
下一篇:Excel將公式轉換為用戶函式
