Application.FileDialog Help with user Cancel

Phil1981

New Member
Joined
May 27, 2011
Messages
27
I am trying to control users save ability in excel and I am making use of the FileDialog function.

My issue is that no matter what I have tried ... if the user clicks cancel in the FileDialog box it errors.

I think by now I have just spent too much time looking at this code but if anyone could have a look and tell me where I am going wrong I would greatly appreciate it.

Code:
Sub SaveAs()
Dim ReportDate As Variant
Dim SaveFile, TeamLead, NewSaveFile As String
Dim NewSave As FileDialog
ReportDate = Format(Sheets(11).Range("$F$14"), "MMMM DD YYYY")
SaveFile = TeamLead & " -" & "Monthly Operational Action Plan" & " - " & ReportDate
TeamLead = Sheets(1).Range("D1")
Set NewSave = Application.FileDialog(msoFileDialogSaveAs)
 
On Error GoTo ErrMsg
    If MsgBox("File will be saved as" & vbCr & SaveFile & vbCr & "in the Fido Reporting drive, WAP folder.", vbOKCancel, "Saving") = vbCancel Then
        NewSave.Show
        If NewSave.Show = True Then
            NewSaveFile = NewSave.SelectedItems(1)
            ActiveWorkbook.SaveAs NewSaveFile
            MsgBox "You saved the file to:" & vbCr & NewSaveFile
            Exit Sub
        Else: GoTo ErrMsg
        End If
    End If
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:="F:\MAPs\" & SaveFile
Application.DisplayAlerts = True
Exit Sub
ErrMsg:
    MsgBox "Your MAPs were not saved!", vbCritical
Exit Sub
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try this version:
Code:
Sub SaveAs()
    Dim ReportDate As Variant
    Dim SaveFile, TeamLead, NewSaveFile As String
    Dim NewSave As FileDialog
    
    ReportDate = Format(Sheets(11).Range("$F$14"), "MMMM DD YYYY")
    SaveFile = TeamLead & " -" & "Monthly Operational Action Plan" & " - " & ReportDate
    TeamLead = Sheets(1).Range("D1")
    
    Set NewSave = Application.FileDialog(msoFileDialogSaveAs)
     
    If MsgBox("File will be saved as" & vbCr & SaveFile & vbCr & "in the Fido Reporting drive, WAP folder.", vbOKCancel, "Saving") = vbCancel Then

        With NewSave
            If .Show Then
                NewSaveFile = .SelectedItems(1)
                ActiveWorkbook.SaveAs NewSaveFile
                MsgBox "You saved the file to:" & vbCr & NewSaveFile
            Else
                MsgBox "Your MAPs were not saved!", vbCritical
            End If
        End With
        
    Else
    
        Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs Filename:="F:\temp\excel\" & SaveFile
        Application.DisplayAlerts = True
    
    End If
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,351
Members
452,907
Latest member
Roland Deschain

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top