Hi All,
Hoping someone can help me with this.
I have some code that will open another Workbook, add data, save/close.
I’m trying to create a loop that will check if the file is opened read only, if the file is opened read only, if like it to close the file and try to open it again, after 5 failed attempts display a message box and stop the code.
This is what I’ve came up with, however, the code seems to run continually, without taking the 5 attempts into account.
Can you help me find where I’ve gone wrong please?
Thanks for any help ?
Hoping someone can help me with this.
I have some code that will open another Workbook, add data, save/close.
I’m trying to create a loop that will check if the file is opened read only, if the file is opened read only, if like it to close the file and try to open it again, after 5 failed attempts display a message box and stop the code.
This is what I’ve came up with, however, the code seems to run continually, without taking the 5 attempts into account.
Can you help me find where I’ve gone wrong please?
Code:
Dim wb1 as workbook
Dim attempts as interger
Attempts = 0
Start1:
Set wb1 = workbooks.open(filename:=“c:\temp\myfile.xls”)
Do until wb1.ReadOnly = False or Attempts > 5
Attempts = Attempts + 1
If wb1.readonly then
Wb1.close
Application.wait (now + TimeValue(“00:00:01”) If Attempts > 5 then
Msg box “Failed, to many attempts”
Exit sub
End if
Goto start1
End if
Loop
Thanks for any help ?