![]() |
|
|
|
#1
|
||||
|
||||
|
One way is to ask the user yourself, using MsgBox, maybe like this:
Code:
If File_Already_Exists Then
OprReply = MsgBox("The Excel workbook " & filename & " already exists. Do you want to write over" _
& " it?",vbYesNoCancel + vbQuestion, "Filename conflict!"
Select Case OprReply
Case vbYes 'turn off the warning and SaveAs again
Case vbNo 'cancel the save
Case vbCancel 'I dunno, whatever you want to do if he hit Cancel
End Select
Code:
On Error GoTo ErrHandle Code:
Exit Sub
ErrHandle:
If Err.Number = 1004 Then
'do whatever you want to do if the user rejected the SaveAs
Else
Resume
End If
|
|
#2
|
|||
|
|||
|
Thanks greatly Bob, got pulled off to a more urgent project; will test this hopefully by EOD tomorrow or first part of next wk and get back w/ you with any issues!
|
|
#3
|
|||
|
|||
|
Ok Bob, finally got a chance to test it tonight - and appears I still have something wrong.. This error handling is new..
but likely important to start incorporating..I put the On Error part at the very top (just below the name of the Sub) as directed... then added the ErrHandle: piece below the 'SAVE RESULTS FILE' section Getting a compile error: Else without If (screenshot attached) If the user chooses "Yes" then I'd like it to continue w/ scripts If the user chooses "No" or "Cancel" (which is giving the 1004 error) l'd like the script to just end w/ a MsgBox "Exiting Sub" |
|
#4
|
||||
|
||||
|
Ah, you're new to VBA, I see
. Don't sweat it; everyone has to start out as a noob, and this one is easy.Take a look again at the code as you wrote it. It differs in a few ways from what I suggested, and some of the differences are important: Code:
ErrHandle:
If Err.Number = 1004 Then Exit Sub
MsgBox "Exiting Sub"
Else
Resume
End If
1) The way I wrote it went like this: Code:
If Err.Number = 1004 Then <do one thing> Else <do a different thing> End if So that explains the actual error message: You said "if <something is true> then <do something>", and that was the end of the If statement—so when it later saw an End If, it didn't understand why. Let's fix that one problem: Code:
If Err.Number = 1004 Then Exit Sub MsgBox "Exiting Sub" Else Resume End If Code:
If Err.Number = 1004 Then MsgBox "Exiting Sub" Exit Sub Else Resume End If Code:
If Err.Number = 1004 Then MsgBox "Exiting Sub" Exit Sub Else Resume End If Last edited by BobBridges; 04-04-2020 at 09:29 AM. Reason: Continuing now that I have more time |
|
| Tags |
| error handling, rename file |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| MS Word Error: This file is in use by another application or user | conky | Word | 4 | 02-26-2019 09:21 AM |
| How can I get Outlook from one user to another w/o a .pst file (primary user on computer is corrupt) | Tenmakk | Outlook | 0 | 03-01-2015 12:49 PM |
How to Control Worksheet Event Handler in Module?
|
tinfanide | Excel Programming | 2 | 10-19-2014 09:46 AM |
Stop macro if no file is selected in dialog box (when user presses cancel instead of selecting file)
|
spencerw.smith | Word VBA | 2 | 08-12-2014 07:56 AM |
| Word does not ask do i want to overwrite a file on saving a file | sam2149 | Word | 2 | 03-24-2014 04:18 AM |