benners Posted February 5, 2018 Share Posted February 5, 2018 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 comment Share on other sites More sharing options...
Subz Posted February 5, 2018 Share Posted February 5, 2018 Could try: Run('Excel.exe /r "V:\AutoIt\QuickLaunch\somefile.xlsx") Or ShellExecute('Excel.exe', '/r "V:\AutoIt\QuickLaunch\somefile.xlsx"') arqstaad 1 Link to comment Share on other sites More sharing options...
benners Posted February 5, 2018 Author Share Posted February 5, 2018 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 Link to comment Share on other sites More sharing options...
benners Posted February 6, 2018 Author Share Posted February 6, 2018 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 comment Share on other sites More sharing options...
Subz Posted February 6, 2018 Share Posted February 6, 2018 (edited) 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 February 6, 2018 by Subz benners 1 Link to comment Share on other sites More sharing options...
AdamUL Posted February 6, 2018 Share Posted February 6, 2018 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 benners 1 Link to comment Share on other sites More sharing options...
benners Posted February 6, 2018 Author Share Posted February 6, 2018 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 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 Link to comment Share on other sites More sharing options...
Moonscarlet Posted February 7, 2018 Share Posted February 7, 2018 Well, if it's just excel you can use _Excel_BookOpen() and pass $bReadOnly = True https://www.autoitscript.com/autoit3/docs/libfunctions/_Excel_BookOpen.htm Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now