Powershell忽略"只读"请求并打开,不提供只读

时间:2016-05-11 18:31:17

标签: excel powershell

我有一个文档集请求用户打开只读版本(Option" Read-only Recommended")。我想打开excel文档而不用只读PowerShell(拒绝提示打开" Read Only")。这是我目前的代码。

$dir = "\\file_path\*"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$latest.name

$excelObj = New-Object -ComObject Excel.Application
$excelObj.Visible = $True
$excelObj.DisplayAlerts = $False


$workBook = $excelObj.Workbooks.Open($latest)

如何忽略只读提示并打开完整版?

2 个答案:

答案 0 :(得分:3)

您可以在工作簿开放方法中提供IgnoreReadOnlyRecommended参数:

$workBook = $excelObj.Workbooks.Open($latest,,,,,,$True,,,,,,,)

Workbooks.Open Method (MSDN)

修改

根据以下评论,似乎在提供$null参数时存在阻止此方法工作的错误。感谢this answer在另一个问题上看来可能有办法解决这个问题:

1,此功能是必需的:

Function Invoke-NamedParameter {
    [CmdletBinding(DefaultParameterSetName = "Named")]
    param(
        [Parameter(ParameterSetName = "Named", Position = 0, Mandatory = $true)]
        [Parameter(ParameterSetName = "Positional", Position = 0, Mandatory = $true)]
        [ValidateNotNull()]
        [System.Object]$Object
        ,
        [Parameter(ParameterSetName = "Named", Position = 1, Mandatory = $true)]
        [Parameter(ParameterSetName = "Positional", Position = 1, Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]$Method
        ,
        [Parameter(ParameterSetName = "Named", Position = 2, Mandatory = $true)]
        [ValidateNotNull()]
        [Hashtable]$Parameter
        ,
        [Parameter(ParameterSetName = "Positional")]
        [Object[]]$Argument
    )

    end {  ## Just being explicit that this does not support pipelines
        if ($PSCmdlet.ParameterSetName -eq "Named") {
            ## Invoke method with parameter names
            ## Note: It is ok to use a hashtable here because the keys (parameter names) and values (args)
            ## will be output in the same order.  We don't need to worry about the order so long as
            ## all parameters have names
            $Object.GetType().InvokeMember($Method, [System.Reflection.BindingFlags]::InvokeMethod,
                $null,  ## Binder
                $Object,  ## Target
                ([Object[]]($Parameter.Values)),  ## Args
                $null,  ## Modifiers
                $null,  ## Culture
                ([String[]]($Parameter.Keys))  ## NamedParameters
            )
        } else {
            ## Invoke method without parameter names
            $Object.GetType().InvokeMember($Method, [System.Reflection.BindingFlags]::InvokeMethod,
                $null,  ## Binder
                $Object,  ## Target
                $Argument,  ## Args
                $null,  ## Modifiers
                $null,  ## Culture
                $null  ## NamedParameters
            )
        }
    }
}

这表明可以像这样调用Workbooks.Open()方法:

$workBook = Invoke-NamedParameter $excelObj "Workbooks.Open" @{"FileName"=$latest;"IgnoreReadOnlyRecommended"=$True}

答案 1 :(得分:1)

如果您只想打开文件进行阅读并忽略提示,那么这就可以了:

$workBook = $excelObj.Workbooks.Open($latest,$null,$true)

第三个参数表示打开只读是真的。 这种方法似乎不受上述错误的影响!