Jump to content

ShellExecute parameters

Recommended Posts

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?

Link to post
Share on other sites

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:

Link to post
Share on other sites

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


Link to post
Share on other sites

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>


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


Edited by Subz
Link to post
Share on other sites

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)
While ProcessExists($iPID)
FileSetAttrib($sFile, "-R")



Link to post
Share on other sites
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:

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...