Code to Recognize Read Only File

erock24

Well-known Member
Joined
Oct 26, 2006
Messages
1,163
I have this code that opens up a file that I click on. Works great except when someone is already in file. How can I stop my code from erroring out if someone is in the file.

Code:
    ChDrive "E:\"
    ChDir "E:\2007 Work\Analytical Review\Operating Expenses"

    TheFile2 = Application.GetOpenFilename("Excel Files (*.xls), *.xls", , "Open Current Month Operating Expense Review File")
    If TheFile2 = "False" Then
    OpenA.Activate
    ActiveWindow.Close
      Exit Sub
End If
    Workbooks.Open Filename:=TheFile2
    Set OpenB = Workbooks(Workbooks.Count)
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Open workbook as Read Only

I believe if you add the additional code below beginning where you called out "Workbooks.Open..." when opening "TheFile2" it should open the file in the Read Only state and it shouldn't cause any problems if someone else has the file open for editing. I've used it here at my work and haven't had the issue you describe when coworkers have the file open while I'm running my macro for pulling in data.


Code:
Workbooks.Open(Filename:=TheFile2, UpdateLinks:=0, ReadOnly:=True, IgnoreReadOnlyRecommended:=True)
 
Upvote 0
Actually, I want to open the file normally, when possible. If the file is in use and the only way I can open it is read only then I don't want to open it and I want my macro to skip that part and continue instead of error out.

Sorry I didn't make myself clear.
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,779
Members
449,049
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top