Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default Why Isn't This Working?

Snippet of code from my routine to check file permissions is below.
Represents my crude attempt to check whether or not I have permission to
access a file.

For Each oFile In Dir.Files
fileIsOkay = False
On Error GoTo Bad_Permission
Set ts = oFile.OpenAsTextStream '<<== Blows up here on second bad
file
ts.Close
Set ts = Nothing
On Error GoTo 0 '<<== This should reset the error, right?
fileIsOkay = True ' If we got here, I have access to the file
Bad_Permission:
On Error GoTo 0
tName = oFile.Name
If (Not fileIsOkay) Then
tName = tName & ": PERMISSION PROBLEM"
Else
tName = tName & ": OKAY"
End If
Debug.Print tName
Next oFile

I loop through all files in the given (test) folder. I know there are a
couple of files that I don't have permissions to view. When the first one is
checked using the code above, it triggers the On Error and gives the
appropriate message. When I hit the second "bad" file, I get "Run-time error
'70': Permission denied" instead. The On Error code no longer works. What
am I doing wrong?

Thanks in advance,

Eric
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default Why Isn't This Working?

Thanks Jim!

I did a small test - I looked at the Err object while I ran my code. When
it hit the first bad file, its value became 70. When the code ran "On Error
Goto 0", Err reset to 0. That seems to indicate that it does clear the
error. However, my code still didn't work...

I made one small change to yours - at the end I had to add "Set ts =
Nothing", but otherwise your code worked and fills my need, which is to find
all the files that have permission issues.

Eric
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 449
Default Why Isn't This Working?

Hi

On Error Goto 0
does not clear the error, it clears previous instructions on what to do when
errors happen. Clear with Err.Clear like this:

Sub test()
On Error Resume Next
MsgBox 4 / 0
MsgBox "Error is: " & Error
Err.Clear
MsgBox "Error is: " & Error
End Sub

HTH. Best wishes Harald


"Eric G" wrote in message
...
Snippet of code from my routine to check file permissions is below.
Represents my crude attempt to check whether or not I have permission to
access a file.

For Each oFile In Dir.Files
fileIsOkay = False
On Error GoTo Bad_Permission
Set ts = oFile.OpenAsTextStream '<<== Blows up here on second bad
file
ts.Close
Set ts = Nothing
On Error GoTo 0 '<<== This should reset the error, right?
fileIsOkay = True ' If we got here, I have access to the file
Bad_Permission:
On Error GoTo 0
tName = oFile.Name
If (Not fileIsOkay) Then
tName = tName & ": PERMISSION PROBLEM"
Else
tName = tName & ": OKAY"
End If
Debug.Print tName
Next oFile

I loop through all files in the given (test) folder. I know there are a
couple of files that I don't have permissions to view. When the first one
is
checked using the code above, it triggers the On Error and gives the
appropriate message. When I hit the second "bad" file, I get "Run-time
error
'70': Permission denied" instead. The On Error code no longer works.
What
am I doing wrong?

Thanks in advance,

Eric


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculate working days but change working week SamB Excel Discussion (Misc queries) 1 September 1st 08 09:17 PM
Making weekend days working days - the system cuts the working tim Fluffy Excel Discussion (Misc queries) 1 May 30th 08 10:02 PM
Newly created Get Function is not working when I copied the syntax from a working function CJ Excel Programming 1 January 16th 07 05:28 AM
Macro working in Excel 2003; not working in Excel 2000 Leslie Barberie Excel Programming 5 May 20th 04 07:51 PM
Adding sales from a non working day to the previous working day Alex Excel Programming 1 September 19th 03 08:48 AM


All times are GMT +1. The time now is 07:14 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"