Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default Error trap only works once

Hello :-)

I have some code to save a workbook for the first time using the SaveAs
dialog.

I need to trap if the same filename is currently being used by aother Excel
session -to prevent VBA from getting upset.

Essentially the code is:

Dim FD As FileDialog ' Variable for dialog object

On Error GoTo ErrorHandler

'Create file dialog object
Set FD = Application.FileDialog(MsoFileDialogType.msoFileDi alogSaveAs)

'Set the default file name (& path)
FD.InitialFileName = Application.DefaultFilePath & "\Planning Spreadsheet"

SaveLoop: 'Point to restart save routine if error occurred
'Display the save as dialog and save file if cancel not selected
If FD.Show < 0 Then FD.Execute
On Error GoTo 0
Exit Sub

ErrorHandler:
' Traps file save error e.g. if file currently in use by another Excel session
MsgBox "Please change the file name", vbExclamation, "File Currently In
Use"
On Error GoTo ErrorHandler
GoTo SaveLoop ' Return to Save As dialog
End Sub

The error trap works fine the first time round, however if I try to
overwrite the file a second time I get a run time error 2147467259 (80004005)
Cannot Access Planning Spreadsheet.xls

I have also tried Err.Clear.

How can I reset the error handler? If I hide and reshow the SaveAs dialog,
would this clear it? If so, how do I hide the built in dialog as it doesn't
seem to have a Hide method?

Regards and thanks,

Z
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,718
Default Error trap only works once

ErrorHandler:
On Error GoTo ErrorHandler ''<Get rid of this
GoTo SaveLoop ' << use Resume instead of Goto


Every error handler must have a Resume unless you are directly exiting the
sub.


--
Jim
"Zarch" wrote in message
...
| Hello :-)
|
| I have some code to save a workbook for the first time using the SaveAs
| dialog.
|
| I need to trap if the same filename is currently being used by aother
Excel
| session -to prevent VBA from getting upset.
|
| Essentially the code is:
|
| Dim FD As FileDialog ' Variable for dialog object
|
| On Error GoTo ErrorHandler
|
| 'Create file dialog object
| Set FD = Application.FileDialog(MsoFileDialogType.msoFileDi alogSaveAs)
|
| 'Set the default file name (& path)
| FD.InitialFileName = Application.DefaultFilePath & "\Planning
Spreadsheet"
|
| SaveLoop: 'Point to restart save routine if error occurred
| 'Display the save as dialog and save file if cancel not selected
| If FD.Show < 0 Then FD.Execute
| On Error GoTo 0
| Exit Sub
|
| ErrorHandler:
| ' Traps file save error e.g. if file currently in use by another Excel
session
| MsgBox "Please change the file name", vbExclamation, "File Currently In
| Use"
| On Error GoTo ErrorHandler
| GoTo SaveLoop ' Return to Save As dialog
| End Sub
|
| The error trap works fine the first time round, however if I try to
| overwrite the file a second time I get a run time error 2147467259
(80004005)
| Cannot Access Planning Spreadsheet.xls
|
| I have also tried Err.Clear.
|
| How can I reset the error handler? If I hide and reshow the SaveAs
dialog,
| would this clear it? If so, how do I hide the built in dialog as it
doesn't
| seem to have a Hide method?
|
| Regards and thanks,
|
| Z


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default Error trap only works once

That's the reason! - and the solution.

Many thanks Jim.

Regards,

Z

"Jim Rech" wrote:

ErrorHandler:
On Error GoTo ErrorHandler ''<Get rid of this
GoTo SaveLoop ' << use Resume instead of Goto


Every error handler must have a Resume unless you are directly exiting the
sub.


--
Jim
"Zarch" wrote in message
...
| Hello :-)
|
| I have some code to save a workbook for the first time using the SaveAs
| dialog.
|
| I need to trap if the same filename is currently being used by aother
Excel
| session -to prevent VBA from getting upset.
|
| Essentially the code is:
|
| Dim FD As FileDialog ' Variable for dialog object
|
| On Error GoTo ErrorHandler
|
| 'Create file dialog object
| Set FD = Application.FileDialog(MsoFileDialogType.msoFileDi alogSaveAs)
|
| 'Set the default file name (& path)
| FD.InitialFileName = Application.DefaultFilePath & "\Planning
Spreadsheet"
|
| SaveLoop: 'Point to restart save routine if error occurred
| 'Display the save as dialog and save file if cancel not selected
| If FD.Show < 0 Then FD.Execute
| On Error GoTo 0
| Exit Sub
|
| ErrorHandler:
| ' Traps file save error e.g. if file currently in use by another Excel
session
| MsgBox "Please change the file name", vbExclamation, "File Currently In
| Use"
| On Error GoTo ErrorHandler
| GoTo SaveLoop ' Return to Save As dialog
| End Sub
|
| The error trap works fine the first time round, however if I try to
| overwrite the file a second time I get a run time error 2147467259
(80004005)
| Cannot Access Planning Spreadsheet.xls
|
| I have also tried Err.Clear.
|
| How can I reset the error handler? If I hide and reshow the SaveAs
dialog,
| would this clear it? If so, how do I hide the built in dialog as it
doesn't
| seem to have a Hide method?
|
| Regards and thanks,
|
| Z



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
Need error trap GregR Excel Programming 4 June 14th 07 08:28 PM
Error Handling - On Error GoTo doesn't trap error successfully David Excel Programming 9 February 16th 06 05:59 PM
Error Trap Kirk P. Excel Programming 2 September 8th 05 09:51 PM
error trap Rhonda[_3_] Excel Programming 2 October 22nd 03 07:07 PM


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