2

I have an Excel file that I am opening, applying a password to it, and then saving it using PowerShell. I am getting the following error:

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

I have searched a lot but nothing has resolved my issue. Here are some questions I refered to already: Powershell - SaveAs function when file already exists and How to Remove ReadOnly Attribute on File Using PowerShell?.

My code:

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)

I have tried these codes after the Workbooks.Open statement to see if it will save the read-only file, and it worked, but then when I closed and reopened the code it stopped working:

Code1:
$file = Get-Item "$($FilePath)"
if ($file.IsReadOnly -eq $true)
{
    $file.IsReadOnly = $false
}

Code2:
Set-ItemProperty "$($FilePath)" -name IsReadOnly -value $false

Actually, the file is not read only but the folder is and I am unable to check out the box that says read only. Same as this problem: 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

1
  • Perhaps you just don't have security rights. Can you open the file manually and save it? Commented Apr 18, 2017 at 3:15

1 Answer 1

4

According to the documentation for the Open() method, the third argument allows you to specify whether to open the file in read-only mode.

Set it to $false:

$wb = $xl.Workbooks.Open("$($FilePath)", 0, $false)
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for your answer but its still not working. It worked at first but when I replaced the file, it gave me the same read-only error again. After I was done with my testing, I wanted to test with the original template so I replaced the file with original template and ran SSIS package. When I opened the file, there was no password. So I ran the code frm PoSh and the same read-only error appeared. I want a code where it SAVES a read-only file. Looks like everything else is just opening the file as not read only but saving with the same name is a problem. Saving with different name works.
This seems like an overwrite issue. Cannot overwrite after applying password to it
Have you considered saving to a temp file/another filename with the password, then delete the original and rename the temp file?
@Stephanie Check if you properly terminate the Excel application. It's possible that you don't, and the leftover process still holds to the file, causing your subsequent open requests to not allow writing.
@MathiasR.Jessen exactly what my way around was. I saved it into a different location, delete the original file and moved the file with passw to original file location. I moved on with this method but my developer mind is still curios as to why I can't do it. I feel like there is a way and I might be missing something,
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.