从覆盖对话框

时间:2016-08-24 14:07:30

标签: excel excel-vba vba

我合并了一些我在SO上找到的代码(特别是第8行的Dir()部分)。一旦'你想要覆盖文件'部分显示如何获得用户按下的值(是,否或取消)以应用更多逻辑?这是我的代码:

Sub SaveFile(fName As String)

cancelSave = False
Retry:
saveAsFileName = Application.GetSaveAsFilename(InitialFileName:=fName, FileFilter:="Excel Files (*.xlsx), *.xlsx")
On Error Resume Next
If saveAsFileName <> False Then
    If Not Dir(saveAsFileName) <> vbNullString Then
        ActiveWorkbook.SaveAs fileName:=saveAsFileName
    Else
        If Err.Number = 1004 Then
        -----HERE, I want to find out if the user pressed yes, no, or cancel.-----
        Else
            ActiveWorkbook.SaveAs fileName:=saveAsFileName, FileFormat:=xlWorkbook, ConflictResolution:=xlLocalSessionChanges
        End If
    End If
Else
    cancelSave = True
End If

End Sub

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为这解决了它:

Sub SaveFile(fName As String)

cancelSave = False
Retry:
saveAsFileName = Application.GetSaveAsFilename(InitialFileName:=fName, FileFilter:="Excel Files (*.xlsx), *.xlsx")
If saveAsFileName <> False Then
    If Not Dir(saveAsFileName) <> vbNullString Then
        ActiveWorkbook.SaveAs fileName:=saveAsFileName
    Else
        On Error Resume Next
        ActiveWorkbook.SaveAs fileName:=saveAsFileName, FileFormat:=xlWorkbook, ConflictResolution:=xlLocalSessionChanges
        If Err.Number = 1004 Then
            On Error GoTo 0
            GoTo Retry
        Else
            On Error GoTo 0
        End If
    End If
Else
    cancelSave = True
End If

End Sub