Jump to content
santinkdr

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

Recommended Posts

santinkdr

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

 

Share this post


Link to post
Share on other sites
water

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 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


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

Share this post


Link to post
Share on other sites
BrewManNH

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

Share this post


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

Share this post


Link to post
Share on other sites
BrewManNH

@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

Share this post


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

Share this post


Link to post
Share on other sites
water

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 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


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

Share this post


Link to post
Share on other sites
water

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 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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

×