Cómo volver al cuadro Guardar mensaje al hacer clic en "No" para reemplazar el archivo

In Excel-VBA, I am saving a file using the following code:

fullFileName = Application.GetSaveAsFilename(...)
ActiveWorkbook.SaveAs fullFileName

This works fine until the name chosen already exists. The message box then prompts: "Should the file be replaced?". I want to answer No, and return to the previous message box and chose another name.

Instead, clicking No interrupts the macro and produces an error.

¿Cómo podría solucionar esto?

(The web is full of examples showing how to bypass this message box using Application.DisplayAlerts=False and save anyway. This is not what I want!)

preguntado el 31 de julio de 12 a las 11:07

1 Respuestas

This is usually what I use...

Sub Sample()
    Dim fullFileName

    fullFileName = Application.GetSaveAsFilename( _
                   fileFilter:="Text Files (*.txt), *.txt")
    If fullFileName <> False Then
        If fileExists(fullFileName) = False Then
            ActiveWorkbook.SaveAs fullFileName
        Else
            MsgBox "File Exists. File Save Aborted"
        End If
    End If
End Sub

Public Function fileExists(strFullPath As Variant) As Boolean
    On Error GoTo Whoa
    If Not Dir(strFullPath, vbDirectory) = vbNullString Then fileExists = True
Whoa:
    On Error GoTo 0
End Function

SEGUIMIENTO

Quieres decir así?

Sub Sample()
    Dim fullFileName
    Dim conti As Boolean

    conti = True

    Do While conti = True
        fullFileName = Application.GetSaveAsFilename( _
                       fileFilter:="Text Files (*.txt), *.txt")
        If fullFileName <> False Then
            If fileExists(fullFileName) = False Then
                ActiveWorkbook.SaveAs fullFileName
                conti = False
            Else
                MsgBox "File Exists. Returning you back to the dialog box"
            End If
        Else
            conti = False
        End If
    Loop
End Sub

Public Function fileExists(strFullPath As Variant) As Boolean
    On Error GoTo Whoa
    If Not Dir(strFullPath, vbDirectory) = vbNullString Then fileExists = True
Whoa:
    On Error GoTo 0
End Function

Respondido 31 Jul 12, 15:07

Thanks Siddhart. With your code, when the file exists, the save is aborted. Is there a way to click "No, I do not want to replace the existing file" and return to the GetSaveAsFilename ¿diálogo? - Nicolas

That's exaclty it. Thanks for the quick replies! - Nicolas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.