[Solved] Macro to check if a file is open or locked

Creating a macro - Writing a Script - Using the API

[Solved] Macro to check if a file is open or locked

Postby ttt_1978 » Mon Jan 17, 2011 4:12 pm

I'm looking for a code to check if a OOo Calc or OOo Writer File is open.
In VBA I used the function below:
Code: Select all   Expand viewCollapse view
Function FileLocked(strFileName As String) As Boolean
   On Error Resume Next
   ' If the file is already opened by another process,
   ' and the specified type of access is not allowed,
   ' the Open operation fails and an error occurs.
   Open strFileName For Binary Access Read Write Lock Read Write As #1
   Close #1
   ' If an error occurs, the document is currently open.
   If Err.Number <> 0 Then
      ' Display the error number and description.
      FileLocked = True
      Err.Clear
   End If
End Function

If the file was open by another user, the FileLocked Function returned True.
But it doesn't work for Ooo Files.
Can anyone help me?
Last edited by ttt_1978 on Mon Jan 17, 2011 7:22 pm, edited 1 time in total.
Windows XP/BrOffice 3.2
ttt_1978
 
Posts: 36
Joined: Thu Dec 02, 2010 1:13 pm

Re: Macro to check if a file is open or locked

Postby RoryOF » Mon Jan 17, 2011 4:53 pm

You could look for the file lock, which is a short length file, with name of form .~lock.(file name)#
Apache OpenOffice 4.1.6 on Xubuntu 18.04.2 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 28749
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Macro to check if a file is open or locked

Postby ttt_1978 » Mon Jan 17, 2011 5:51 pm

Hhmmm, not all the Ooo Files generates a .~lock.(file name)# when accessed.
I did some tests in our shared documents at my work and those files never appear in the container folder.
Windows XP/BrOffice 3.2
ttt_1978
 
Posts: 36
Joined: Thu Dec 02, 2010 1:13 pm

Re: Macro to check if a file is open or locked

Postby ttt_1978 » Mon Jan 17, 2011 6:36 pm

RoryOF,
I realized that the .~lock.(file name) is a hidden file.
I will write a code that search for this file.
Windows XP/BrOffice 3.2
ttt_1978
 
Posts: 36
Joined: Thu Dec 02, 2010 1:13 pm

[SOLVED] Macro to check if a file is open or locked

Postby ttt_1978 » Mon Jan 17, 2011 7:20 pm

RoryOF,

Thank you. It works.

My code:

Code: Select all   Expand viewCollapse view
Sub CheckIfFileIsLocked()
Dim StrPath, StrFile, StrFileLocked As String
StrPath = "C:/"
StrFile = "Test.odt"
StrFileLocked  = StrPath & ".~lock." & StrFile & "#"
If FileExists(StrFileLocked) Then
  MsgBox "File locked for editing by another user. Try again later.", 16
  Exit Sub
End If
End Sub

Or

Code: Select all   Expand viewCollapse view
Function FileLocked(StrPath As String, StrFile As String) As Boolean
Dim StrFileLocked As String
StrFileLocked  = StrPath & ".~lock." & StrFile & "#"
If FileExists(StrFileLocked) Then
   FileLocked = True
Else
   FileLocked = False
End If
End Function
Last edited by ttt_1978 on Mon Jan 17, 2011 7:30 pm, edited 1 time in total.
Windows XP/BrOffice 3.2
ttt_1978
 
Posts: 36
Joined: Thu Dec 02, 2010 1:13 pm

Re: [SOLVED] Macro to check if a file is open or locked

Postby ttt_1978 » Mon Jan 17, 2011 7:30 pm

If anyone has another method, it is wellcomed.
Windows XP/BrOffice 3.2
ttt_1978
 
Posts: 36
Joined: Thu Dec 02, 2010 1:13 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 10 guests