0

I'd like to check if a file located in my_path exists, if it does overwrite it with a file declared "garcat" containing 1 single sheet. Creating a file for the 1st time works, however overwriting it gives me this error 1004: Method 'SaveAs' of object'_Workbook' failed

Sub FileCreate(ByVal TGName As String)

Dim garcat As Workbook

Dim file As String

file = my_path
If Dir(file) <> "" Then
    SetAttr file, vbNormal
    Kill file
    MsgBox "The file already exists, it will be replaced"

End If

Application.SheetsInNewWorkbook = 1
Set garcat = Workbooks.Add
garcat.SaveAs FileName:=file
MsgBox "File Saved"
garcat.Sheets(1).name = "GARCAT " & TGName
garcat.Close (True)

End Sub

EDIT: I set Application.DisplayAlerts = False and Application.EnableEvents = False before saving the file and I still get the pop-up error message.

1 Answer 1

0

This works by just telling the file to save regardless of any other factors: "And no back-talk! If there's already a file there... Just overwrite it."

    Sub FileCreate(ByVal TGName As String)

Dim garcat As Workbook

Dim chemin As String

file = my_path

Application.SheetsInNewWorkbook = 1
Set garcat = Workbooks.Add
Application.DisplayAlerts = False 
garcat.SaveAs FileName:=file
Application.DisplayAlerts = True
MsgBox "File Saved"
garcat.Sheets(1).name = "GARCAT " & TGName
garcat.Close (True)
Sign up to request clarification or add additional context in comments.

2 Comments

The Application.DisplayAlerts = False deosn't seem to work for me because I still get the pop-up error message.
@SwiftStreet Setting DisplayAlerts to False should prevent the "Do you want to replace it?" dialog box. So are you still getting this dialog box? Or are you getting something else? Can you please be specific?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.