Error Code

D

daniels012

I have code to save my file and make a copy of the file as a new name
All is great!
Sometimes on error I save the file twice... or try to, and I get a
error 1004.

Is there a way for me to add in my code
If Error 1004 then end the function?

Below is the code I have. Where would I add the code that I need also.

Sub Print_And_Save()
'
' Print_And_Save Macro
' Prints Proposal, saves proposal, then saves as to Completed Proposal
folder.
'
'
Range("F2").Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
ActiveWorkbook.Save
Path = "C:\WINDOWS\Personal\Completed Proposals\"
Path2 = "C:\WINDOWS\Personal\Surface Systems\"
ActiveWorkbook.SaveAs Filename:= _
Path & "Proposal" & _
Str(Application.Range("N2").Value), FileFormat:=xlNormal _
, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False
Range("F2").Select
Workbooks.Open Filename:=Path2 & "Proposal for XL.xls"
End Sub

Thanks in advance!
Michae
 
X

xld

Try

On Error Resume Next
ActiveWorkbook.SaveAs Filename:= _
Path & "Proposal" & _
Str(Application.Range("N2").Value), FileFormat:=xlNormal _
, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False
If Err.Number = 1004 Then Exit Su
 
D

daniels012

This is OK but I really would like to enter my own Message to let the
knowit's already saved... something like "A proposal has already bee
saved with that name"

If Err.Number = 1004 Then
(How do I make a message box here?)
End If
Exit Su
 
D

Dave Peterson

If Err.Number = 1004 Then
msgbox "An error occurred." & vblf _
& "A proposal has already been saved with that name"
End If

But you could just check first and avoid the error:

Option Explicit
Sub testme01()

Dim myFileName As String
Dim myPath As String

myPath = "C:\my documents\excel\"

myFileName = myPath & Worksheets("sheet1").Range("N2").Value

If myFileName = myPath Then
MsgBox "Please put the proposal number in N2"
Exit Sub
End If

If Dir(myFileName) = "" Then
'ok to save
ActiveWorkbook.SaveAs Filename:=myFileName, FileFormat:=xlNormal
Else
MsgBox "A proposal has already been saved with that name" & vbLf _
& "Please change N2 and try again"
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top