Open file validation

A

Alberto Ast

I am opening a file with macros but some times the file is open by other
users and then my macro fails... how can I avoid the macro to fail if the
file is open?

I do not want it to ask me if I want to open as ready only... I would like
to be able by program to know I could not open it and then procedd to do
something else.

What do I need to do?
Thanks.
 
A

Alberto Ast

I did some testing and it will open the file as read only and do not show any
error but while saving the file with the macro it goes to the "save as"
screen because can not save under same name because it was opened as read
only.

How can I validate this?
 
S

Stefano

Does this simple solution work?

Set WB = Workbooks.Open(FileName)
On Error Resume Next
WB.Save
If Err.Number = 1004 Then
 
A

Alberto Ast

thanks but not sure how to structure the whole macro...
will set WB= workbook.open(Fielname) check for file status?
 
A

Alberto Ast

Have been doing some testing... I got to see the 1004 error... I think I am
close to make it work... but still since the file was open as read only while
tring to save it, it ask me if I want to replace it... I would like not to be
ask so I can procedd without saving...
 
S

Stefano

The On Error Resume Next before the WB.Save should prevent any message from
showing up.

If the file is not read only then it does save (I is not nice to waste time
in a useless save overwriting a file just for a test, but it's good if it
works for you.)

If the file is read only then it fails, sets Err.Number to 1004, and you can
decide what to do next.

Set WB = Workbooks.Open(FileName)
On Error Resume Next
WB.Save
IsReadOnly = 1004 = Err.Number
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top