私は開いているExcelファイルを持っており、パスワードを適用してPowerShellを使用して保存しています。次のエラーが表示されます。PowerShellを使用して読み取り専用ファイルを保存する方法
Exception calling "SaveAs" with "3" argument(s): "Cannot save as that name. Document was opened as read-only." At C:\PasswordProtectExcelFiles.ps1:38 char:45
+ $a = $wb.SaveAs("$($FilePath)",$xlNormal,"$($Password)")
+ ~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
私は多くを検索しましたが、何も問題は解決しませんでした。私が既に言及したいくつかの質問があります: Powershell - SaveAs function when file already existsと How to Remove ReadOnly Attribute on File Using PowerShell?です。
マイコード:
param([string]$FilePath, [string]$Password)
$xl = new-object -comobject excel.application
$xl.Visible = $True
$xl.DisplayAlerts = $False
$wb = $xl.Workbooks.Open("$($FilePath)")
$a = $wb.SaveAs("$($FilePath)",$xlNormal,"$($Password)")
$a = $xl.Quit()
$a = Release-Ref($wb)
$a = Release-Ref($xl)
私は、コードを閉じて再び開いたときに、私はそれが読み取り専用ファイルを保存しますかどうかを確認するためにWorkbooks.Open文の後にこれらのコードを試してみましたが、それは働いたが、
Code1:
$file = Get-Item "$($FilePath)"
if ($file.IsReadOnly -eq $true)
{
$file.IsReadOnly = $false
}
Code2:
Set-ItemProperty "$($FilePath)" -name IsReadOnly -value $false
実際には、ファイルは読み取り専用ではなく、フォルダがあり、読み取り専用のボックスをチェックアウトできません。この問題と同じ:https://social.technet.microsoft.com/Forums/windowsserver/en-US/f7ec4fc5-3bbe-4fd0-a8ca-c4ead75b010c/unable-to-removeclear-readonly-attribute-from-folder-in-windows-server-2008
をおそらく、あなただけのセキュリティ権限を持っていません。ファイルを手動で開いて保存できますか? –