Jump to content
benners

ShellExecute parameters

Recommended Posts

benners

I am assuming that when ShellExecute passes any supplied parameters for the program it adds them after the file parameter?.

The reason I ask is that I am using it to open a variety of files, one of them being and excel file. With this file I need to open it in read only mode which can be done from the cmd line like so

excel.exe /r "V:\AutoIt\QuickLaunch\somefile.xlsx"

It doesn't work with ShellExecute

ShellExecute('V:\AutoIt\QuickLaunch\somefile.xlsx', '/r')

Is there a way to do this?

Share this post


Link to post
Share on other sites
Subz

Could try:

Run('Excel.exe /r "V:\AutoIt\QuickLaunch\somefile.xlsx")
Or
ShellExecute('Excel.exe', '/r "V:\AutoIt\QuickLaunch\somefile.xlsx"')

Share this post


Link to post
Share on other sites
benners

Cheers Subz

It would have to be the ShellExecute method as it's for a menu that is dynamically created so there will be different file types and default programs for those types.

Your ShellExecute code works. I tried something similar but had the /r inside the quotes :doh:

Share this post


Link to post
Share on other sites
benners

Well I need some new glasses.  I just copy\pasted Subz's code above and didn't notice the excel.exe part of the ShellExecute function. 

I am using ShellExecute so I don't have to specify the default program associated with the passed file name. Any parameters passed seem to be ignored or passed after the file name such as with excel.exe.

So I have to ask again. Is there a way to use this function to open an excel file in read only mode without specifying excel such as below?

ShellExecute('V:\AutoIt\QuickLaunch\somefile.xlsx', '/r')

 

Share this post


Link to post
Share on other sites
Subz

Sorry about that, unfortunately it won't work since "/r" needs to be before the file name (well it does in my case) otherwise it just opens normally, you could use some type of function for example:

#include <WinAPIShPath.au3>

_MyShellExecute("V:\AutoIt\QuickLaunch\somefile.xlsx")

Func _MyShellExecute($_sFileName)
    Local $sExtension = _WinAPI_PathFindExtension($_sFileName)
    Switch $sExtension
        Case ".xlsx"
            ShellExecute("Excel.exe", '/r "' & $_sFileName & '"')
        Case Else
            ShellExecute($_sFileName)
    EndSwitch
EndFunc

 

Edited by Subz
  • Like 1

Share this post


Link to post
Share on other sites
AdamUL

The other workaround is to set the file attribute to Read-only, then ShellExecute the file, and then remove the Read-only attribute after the process that opened the file is closed.  See example below.  

Global $sFile = "V:\AutoIt\QuickLaunch\somefile.xlsx"
If FileSetAttrib($sFile, "+R") Then 
    Global $iPID = ShellExecute($sFile)
EndIf
While ProcessExists($iPID)
    Sleep(10)
WEnd
FileSetAttrib($sFile, "-R")

 

Adam

  • Like 1

Share this post


Link to post
Share on other sites
benners
53 minutes ago, Subz said:

Sorry about that

Not our fault Subz, I blame Microsoft. Assumingly the wrote the ShellExecute API and the Excel application that places the switches first ,thus causing the issues. You think they would make work as I need :D

I'll look into using something like your function. I have been trying stuff like @ComSpec & " /c Start, trying to get the default program from the registry etc but most fail because of the switch placement.

@AdamUL nice, wouldn't have though of that.  The issues is this. The menu is built from an ini which has files, folders, urls etc and I let ShellExecute decide how to proceed when the menu item is selected. It would be a big task to try and code for items on a case by case basis.  I also need the script to continue after the function has run.

Guess it's just a nice to have. So far it's only this one Excel file that annoys me.

Thanks both for your suggestions :thumbsup:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×