santinkdr Posted March 6, 2018 Share Posted March 6, 2018 Hi friends, I am trying to create a simple GUI which collects data from user and updates them in Excel workbook. Since many users will update the single workbook, I want to notify the user when the file is already opened by some other user (showing user name might also be helpful, if possible). But the Excel should be hidden. Now I am facing a problem, that when opening the read-only workbook, the excel opens and shows itself. See the below code I am using now to open the workbook. Please help me. Func ExcelOpen() $ExcelObj = _Excel_Open(False,False,True,True,True) If @error Then Exit MsgBox(0, "", 'Error creating object' & @CRLF & 'Error = ' & @error) Global $ExcelLxn = IniRead(@ScriptDir & '\Settings\settings.ini', 'ExcelLocation', 'location', '') $ExcelWB = _Excel_BookOpen($ExcelObj, $ExcelLxn, False, True, '1') If @error Then Exit MsgBox(0, "", 'Error opening Workbook' & @CRLF & 'Error = ' & @error) Local $mm = $ExcelWB.Activesheet.range("I4").NumberFormat If $mm <>"dd-mm-yyyy" Then $ExcelWB.Activesheet.range("I:L").NumberFormat = "dd-mm-yyyy" _Excel_BookSave($ExcelWB) EndIf EndFunc ;==>ExcelOpen Link to comment Share on other sites More sharing options...
water Posted March 6, 2018 Share Posted March 6, 2018 According to the help file: Quote @extended is set to 1 if $bReadOnly = False but read-write access could not be granted. When you set $bReadOnly = False but the document can't be opened read-write @extended is set to 1. The Workbook was opened read-only because it has already been opened by another user/task or the file is set to read-only by the filesystem. If you modify the workbook you need to use _Excel_BookSaveAs() to save it to another location or with another name. My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
santinkdr Posted March 6, 2018 Author Share Posted March 6, 2018 1 hour ago, water said: According to the help file: Yes, I've read it. What I want is to not open the workbook in read-only mode and to inform users that someone else is using and then to exit the program. Link to comment Share on other sites More sharing options...
BrewManNH Posted March 6, 2018 Share Posted March 6, 2018 That's what he was telling you, read the @extended value when you open it to see if it's opened in read only mode. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
santinkdr Posted March 6, 2018 Author Share Posted March 6, 2018 9 minutes ago, BrewManNH said: That's what he was telling you, read the @extended value when you open it to see if it's opened in read only mode. Oh ok. But for @extended value, error must come right? Or can I read @extended value even when there is no error? can u plz provide me an example? Link to comment Share on other sites More sharing options...
BrewManNH Posted March 6, 2018 Share Posted March 6, 2018 @extended and @error can be read separately, and @extended shouldn't need to be setting @error, although I don't know the inner workings of the Excel UDF to say for certain. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
santinkdr Posted March 6, 2018 Author Share Posted March 6, 2018 7 minutes ago, BrewManNH said: @extended and @error can be read separately, and @extended shouldn't need to be setting @error, although I don't know the inner workings of the Excel UDF to say for certain. I've tried getting @extended and It is set to value 1 when the file is Read-only, but the problem is, eventhough I've set the excel invisible, it becomes visible and opens the workbook in read-only mode and then it sets the value to 1. But all I want is to never open the workbook when the file is read-only. Is there any other possible way to do this? Link to comment Share on other sites More sharing options...
water Posted March 6, 2018 Share Posted March 6, 2018 Simplest way is to close the workbook when @extended = 1 $ExcelWB = _Excel_BookOpen($ExcelObj, $ExcelLxn, False, True, '1') If @error Then Exit MsgBox(0, "", 'Error opening Workbook' & @CRLF & 'Error = ' & @error) Else If @extended = 1 Then _ExcelBookClose($ExcelWB, False) EndIf My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
santinkdr Posted March 6, 2018 Author Share Posted March 6, 2018 8 minutes ago, water said: Simplest way is to close the workbook when @extended = 1 $ExcelWB = _Excel_BookOpen($ExcelObj, $ExcelLxn, False, True, '1') If @error Then Exit MsgBox(0, "", 'Error opening Workbook' & @CRLF & 'Error = ' & @error) Else If @extended = 1 Then _ExcelBookClose($ExcelWB, False) EndIf I've tried this code. But the problem is that, workbook is password protected. Hence the excel opens and asks for password, once I enter and open the file it closes. Link to comment Share on other sites More sharing options...
water Posted March 6, 2018 Share Posted March 6, 2018 IIRC there have been some discussions on the forum about subjects like "file in use by another user" and I think there was a solution based on Windows API. Will have a look and post the results. My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
szyMarek Posted November 18, 2022 Share Posted November 18, 2022 I apologize for digging up an old topic but it illustrates my problem perfectly and I guess there is no point in me describing again the same thing as santinkdr. In a nutshell. Using _Excel_Open and _Excel_BookOpen I open an excel sheet. The file is password protected from reading. Local $oExcelLogDrukiEASA = _Excel_Open(False, False, True, False, True) Local $workbookLogDrukiEASA = _Excel_BookOpen($oExcelLogDrukiEASA, $lokLogDrukiEASA & $plikLogDrukiEASA, False, True, "easa123", "easa123", 3) If @error Then MsgBox($MB_SYSTEMMODAL, "BLAD OTWARCIA PLIKU EXCEL: Log EASA", "Blad podczas otwierania pliku '" & $lokLogDrukiEASA & $plikLogDrukiEASA & @CRLF & " @error = " & @error & ", @extended = " & @extended) Exit EndIf $Lines = $workbookLogDrukiEASA.ActiveSheet.UsedRange.Rows.Count The file is a cumulative log file, hosted in a network directory, populated by several users. I am looking for a solution on how to handle the situation when two users want to open and write data to this file at the same time. Or rather, how to secure so that the second user gets a message that the file is currently in use and to wait. Link to comment Share on other sites More sharing options...
Nine Posted November 18, 2022 Share Posted November 18, 2022 Maybe (simple suggestion) you could use SQLite instead, and produce as required an Excel spreadsheet (if necessary). That way you wouldnt have the problem of managing multiple accesses at a time “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
water Posted November 18, 2022 Share Posted November 18, 2022 When you try to open a Workbook in write mode but the file has already been opened by another user in write mode, Excel opens the Workbook in read mode an _Excel_BookOpen sets @extended = 1. Copied from the help file: Quote When you set $bReadOnly = False but the document can't be opened read-write @extended is set to 1. My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
water Posted November 18, 2022 Share Posted November 18, 2022 MS creates a lock file when a file gets opened in write mode (~$filename). I can't test at the moment if this lock file gets created for read mode as well. Can you please check? My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki 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