¿Cómo detectar si el archivo de Excel está en modo de solo lectura y salir?
Tengo problemas para detectar si un archivo de Excel está en modo de solo lectura. Sé que hay un método workbook.open que tiene un atributo Léame, pero ¿hay alguna manera de tenerlo si sabe que el archivo es de solo lectura y sale? Intenté agregar .readonly al archivo de variables al final para ver si funcionaba.
Esto es lo que tengo hasta ahora.
$file = 'path'
$excl = New-Object -ComObject 'Excel.Application'
if ($file.readonly) {
Exit
}
try {
$wrkb = $excl.Workbooks.Open($file)
}
Catch {
Exit
}
$excl.DisplayAlerts = $FALSE
try {
$wrkb.Save()
}
catch {
Exit
}
Aceptado
Supongo que lo que estás buscando es probar si la FileInfo
instancia tiene la ReadOnly
bandera :
$file = 'path'
# Can also do it thru:
# .Attributes.HasFlag([System.IO.FileAttributes]::ReadOnly)
if ((Get-Item $file).IsReadOnly) {
return
}
try {
$excl = New-Object -ComObject 'Excel.Application'
$wrkb = $excl.Workbooks.Open($file)
$excl.DisplayAlerts = $FALSE
$wrkb.Save()
}
catch {
# handling
}
finally {
if ($excl) {
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($excl)
}
}