Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 03-31-2020, 01:36 PM
BobBridges's Avatar
BobBridges BobBridges is offline Error Handler If User Does Not Want to Overwrite File Windows 7 64bit Error Handler If User Does Not Want to Overwrite File Office 2010 32bit
Expert
 
Join Date: May 2013
Location: USA
Posts: 700
BobBridges has a spectacular aura aboutBobBridges has a spectacular aura about
Default

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
In your case, though, that involves detecting ahead of time that the file exists, probably using FileSystemObject. Simpler, in my opinion, to tell VBA to spot the error when it happens, like this: 1) Before you get to this part of the program—I always do it first thing in a module—set error trapping like this:

Code:
On Error GoTo ErrHandle
That tells VBA that when there's an error—any error—instead of handling it in the normal way, VBA should go to the statement label you name. (I always call it "ErrHandle" but you can name it anything you like.) Then, down near the bottom of the program 2) add the part where you handle the error, like this:

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
You have to add the Exit Sub statement before the label, or your code will fall into these statements after it's finished normally. The Resume statement, if the error wasn't 1004, means in that case go back to the statement that caused the error and do it again, and this time abend in whatever way VBA considers normal for that error.
Reply With Quote
 

Tags
error handling, rename file

Thread Tools
Display Modes


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
Error Handler If User Does Not Want to Overwrite File How to Control Worksheet Event Handler in Module? tinfanide Excel Programming 2 10-19-2014 09:46 AM
Error Handler If User Does Not Want to Overwrite File 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

Other Forums: Access Forums

All times are GMT -7. The time now is 02:47 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft