3

I have a document set to request that user open a read only version(Option "Read-only Recommended"). I would like to open the excel document without read on only in powershell (decline the prompt asking to open "Read Only"). Here is my current code.

$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) 

How do I ignore the read only prompt and open the full version?

2 Answers 2

3

There should be a IgnoreReadOnlyRecommended argument that you can supply in the workbook open method:

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

Workbooks.Open Method (MSDN)


Edit

Based on comments below, it appears that there is a bug preventing this method from working when the $null parameters are supplied. Thanks to this answer on another question it appears there may be a way around this:

1st, this function is required:

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 ) } } } 

Which would suggest the Workbooks.Open() method could be called like so:

$workBook = Invoke-NamedParameter $excelObj "Workbooks.Open" @{"FileName"=$latest;"IgnoreReadOnlyRecommended"=$True} 
Sign up to request clarification or add additional context in comments.

8 Comments

When did the first command become valid syntax? Have I slept through the class? :-) Haven't seen it before and it throws error in PowerShell 5.0
@FrodeF. I'm just about to edit that bit out - I'm not a proper PowerShell user, just a fan of Excel Interop but I've seen arguments passed in this fashion before so figured it might work. After some testing I realise that isn't the case!
-Filename .... etc. is for cmdlets and functions. Methods in .NET and COM-objects uses standard c# syntax Open("foo","bar",$null,$null....). You need to provide $null for unused optional parameters. However, when I tested this I met a bug in the Excel interop that was recently mentioned here on StackOverflow where the Open() method fails after 3-4 parameters.. stackoverflow.com/questions/35452262/…
@FrodeF. good to know, at risk of going off-topic is there no way to call arguments out-of-sequence by specifying the argument's name? (In VB for example you would use argName:=value syntax)
I see, that makes a lot of sense now - surprised that this hasn't been sorted by now though...
|
3

If your looking to just open the file for reading and ignore the prompt then this works:

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

The 3rd argument denotes true to open read-only. This approach does not appear to be subject to the aforementioned bug!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.