Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 132
Default Error handling problem

I have the following macro run in a template file and works well without the
error handling:

Sub Save()
' Macro recorded 02/10/2007 by me

Dim MyJobNo As String
Dim myFileName As String
Dim myDir As String
Dim myBUDir As String
Dim myBUName As String
On Error GoTo ErrorHandler

MyJobNo = Sheets("Summary").Range("A8").Formula = "left($a$7,4)"
myFileName = "CAPEXSummary-"
myDir = "N:\MINING\PLANT\SERVICES\AXAPTA\Job Analysis\Seraji\CAPEX\"
'enter correct path name
ActiveWorkbook.SaveAs Filename:= _
myDir & myFileName & MyJobNo

myBUDir = "N:\MINING\PLANT\SERVICES\AXAPTA\Job
Analysis\Seraji\CAPEX\Backup\"
myBUName = "Backup-CAPEXSummary-"
ActiveWorkbook.SaveCopyAs Filename:= _
myBUDir & myBUName & MyJobNo
End Sub

When the file already exists the message "File already exists...etc" is fine.

If users have mistakenly left the previously saved file open and restart the
Template file the file saves as normal but MyJobNo changes to "FALSE" and the
user is unaware a new file wasn't saved.

Can I put an error trap in that warns the user that the file is open and
asks if they would like to close it before saving?

I've tried the following addition but I don't know how to identify the open
file in order to close it automatically (or even if it's a good idea):

ErrorHandler: ' Error-handling routine.
Select Case Err.Number ' Evaluate error number.
Case 55 ' "File already open" error.
MsgBox msg = "The file you are trying to save is already open" &
vbNewLine & _
" " & vbNewLine & _
"Select OK to have the file close and resave"
Close THE OPEN FILE 'Close open file.
Case Else
' Handle other situations here...
End Select
Resume ' Resume execution at same line
' that caused the error.

My applogies if this is long winded and difficult to read.

--
Jim
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 180
Default Error handling problem

I agree you can certainly write a error handling routine.
There is one more way you can achieve this...write a function which checks
if the file already exists and if yes, deletes it. Write your error handling
inside this function such as if the file is open can not be deleted the
function returns FALSE, else the function deletes the file and returns TRUE.
Now you should have this functino call with IF statement before your SAVE
statement.

HTH,
--
Pranav Vaidya
VBA Developer
PN, MH-India
If you think my answer is useful, please rate this post as an ANSWER!!


"Jim G" wrote:

I have the following macro run in a template file and works well without the
error handling:

Sub Save()
' Macro recorded 02/10/2007 by me

Dim MyJobNo As String
Dim myFileName As String
Dim myDir As String
Dim myBUDir As String
Dim myBUName As String
On Error GoTo ErrorHandler

MyJobNo = Sheets("Summary").Range("A8").Formula = "left($a$7,4)"
myFileName = "CAPEXSummary-"
myDir = "N:\MINING\PLANT\SERVICES\AXAPTA\Job Analysis\Seraji\CAPEX\"
'enter correct path name
ActiveWorkbook.SaveAs Filename:= _
myDir & myFileName & MyJobNo

myBUDir = "N:\MINING\PLANT\SERVICES\AXAPTA\Job
Analysis\Seraji\CAPEX\Backup\"
myBUName = "Backup-CAPEXSummary-"
ActiveWorkbook.SaveCopyAs Filename:= _
myBUDir & myBUName & MyJobNo
End Sub

When the file already exists the message "File already exists...etc" is fine.

If users have mistakenly left the previously saved file open and restart the
Template file the file saves as normal but MyJobNo changes to "FALSE" and the
user is unaware a new file wasn't saved.

Can I put an error trap in that warns the user that the file is open and
asks if they would like to close it before saving?

I've tried the following addition but I don't know how to identify the open
file in order to close it automatically (or even if it's a good idea):

ErrorHandler: ' Error-handling routine.
Select Case Err.Number ' Evaluate error number.
Case 55 ' "File already open" error.
MsgBox msg = "The file you are trying to save is already open" &
vbNewLine & _
" " & vbNewLine & _
"Select OK to have the file close and resave"
Close THE OPEN FILE 'Close open file.
Case Else
' Handle other situations here...
End Select
Resume ' Resume execution at same line
' that caused the error.

My applogies if this is long winded and difficult to read.

--
Jim

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 132
Default Error handling problem

You give me more credit than I deserve. I usually develop these macros by
melding bits and pieces I find here that I think might work.

I was hoping for a suggested code snippet to add to my library.

I was not thinking more of closing the file after a warning rather than
deleting the file without an option to back out.

Thanks for your suggestion
--
Jim


"Pranav Vaidya" wrote:

I agree you can certainly write a error handling routine.
There is one more way you can achieve this...write a function which checks
if the file already exists and if yes, deletes it. Write your error handling
inside this function such as if the file is open can not be deleted the
function returns FALSE, else the function deletes the file and returns TRUE.
Now you should have this functino call with IF statement before your SAVE
statement.

HTH,
--
Pranav Vaidya
VBA Developer
PN, MH-India
If you think my answer is useful, please rate this post as an ANSWER!!


"Jim G" wrote:

I have the following macro run in a template file and works well without the
error handling:

Sub Save()
' Macro recorded 02/10/2007 by me

Dim MyJobNo As String
Dim myFileName As String
Dim myDir As String
Dim myBUDir As String
Dim myBUName As String
On Error GoTo ErrorHandler

MyJobNo = Sheets("Summary").Range("A8").Formula = "left($a$7,4)"
myFileName = "CAPEXSummary-"
myDir = "N:\MINING\PLANT\SERVICES\AXAPTA\Job Analysis\Seraji\CAPEX\"
'enter correct path name
ActiveWorkbook.SaveAs Filename:= _
myDir & myFileName & MyJobNo

myBUDir = "N:\MINING\PLANT\SERVICES\AXAPTA\Job
Analysis\Seraji\CAPEX\Backup\"
myBUName = "Backup-CAPEXSummary-"
ActiveWorkbook.SaveCopyAs Filename:= _
myBUDir & myBUName & MyJobNo
End Sub

When the file already exists the message "File already exists...etc" is fine.

If users have mistakenly left the previously saved file open and restart the
Template file the file saves as normal but MyJobNo changes to "FALSE" and the
user is unaware a new file wasn't saved.

Can I put an error trap in that warns the user that the file is open and
asks if they would like to close it before saving?

I've tried the following addition but I don't know how to identify the open
file in order to close it automatically (or even if it's a good idea):

ErrorHandler: ' Error-handling routine.
Select Case Err.Number ' Evaluate error number.
Case 55 ' "File already open" error.
MsgBox msg = "The file you are trying to save is already open" &
vbNewLine & _
" " & vbNewLine & _
"Select OK to have the file close and resave"
Close THE OPEN FILE 'Close open file.
Case Else
' Handle other situations here...
End Select
Resume ' Resume execution at same line
' that caused the error.

My applogies if this is long winded and difficult to read.

--
Jim

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
Error Handling works only once linglc Excel Discussion (Misc queries) 1 March 7th 07 07:37 AM
Handling #NUM! error Michel Khennafi Excel Worksheet Functions 1 February 26th 07 08:49 PM
Error handling in macro michaelberrier Excel Discussion (Misc queries) 7 May 23rd 06 09:33 PM
Error handling in a search michaelberrier Excel Discussion (Misc queries) 2 May 21st 06 07:08 PM
Error Handling for "my" menu bar dstiefe Excel Discussion (Misc queries) 0 July 27th 05 10:52 PM


All times are GMT +1. The time now is 10:52 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"