Jump to content

Help needed in showing error message when Excel workbook (read-only) is opened


Recommended Posts

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

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 - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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 Gude
How 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

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

@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 Gude
How 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

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

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 - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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

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 - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • 4 years later...

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

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 

Link to comment
Share on other sites

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 - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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 - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...