Please can someone help with the below code? I am trying to write this to check whether a workbook is already open by another user but it gets stuck in the loop and ignores (or more probably my code is wrong) the counter which should cause an "Exit Do" at a value of two -
Sub Rd_Nly_Trial()
Dim strWorkbookPath2 As String
Dim strWorkbookName2 As String
Dim Counter As Variant
strWorkbookPath2 = "P:\General\"
strWorkbookName2 = "logger.xls"
Workbooks.Open (strWorkbookPath2 & strWorkbookName2)
If ActiveWorkbook.ReadOnly = True Then
Response = MsgBox("File is in use on another machine", vbAbortRetryIgnore, "Warning")
If Response = vbAbort Then
ActiveWorkbook.Saved = True
ActiveWorkbook.Close
MsgBox "Please try again later", vbInformation, "Information not updated"
Exit Sub
ElseIf Response = vbRetry Then
Counter = 0
Do ' Outer loop.
Do While ActiveWorkbook.ReadOnly = True
If ActiveWorkbook.ReadOnly = True Then ' If condition is True.
ActiveWorkbook.Saved = True
ActiveWorkbook.Close
ElseIf Counter = 2 Then
MsgBox "Failed to save information, try again later", vbCritical, "Warning"
Exit Do ' Exit inner loop.
Else
Exit Do
End If
Counter = Counter + 1
ActiveSheet.Range("A2").Value = Counter
Workbooks.Open (strWorkbookPath2 & strWorkbookName2)
Loop
Loop Until ActiveWorkbook.ReadOnly = False ' Exit outer loop immediately.
Else
MsgBox "Please enter information manually when file is available", vbExclamation
End If
Else
End If
'I will be calling another macro here!
End Sub
Also I realise this is perhaps the wrong approach for my solution. Is there a way of determing read only status without opening the workbook?
Many thanks
Dave
Bookmarks