Run-time error when canceling a 'Save As' dialog displayed via VBA

David Gard picture David Gard · Mar 14, 2013 · Viewed 8k times · Source

I have a macro that formats a document in a certain way, and then saves it, using ActiveDocument.Save.

However, sometimes the document is not already saved, and in some cases I don't want to save it. Unfortunately, clicking 'Cancel' when the 'Save As' dialogue is displayed is causing a run-time error (4198) -

Command failed

Does anyone know how I can prevent this from happening? Thanks.

Answer

Kazimierz Jawor picture Kazimierz Jawor · Mar 14, 2013

Please try the following by adding some error handling instructions:

On Error Resume Next    'to omit error when cancel is pressed
   ActiveDocument.Save

If Err.Number <> 0 Then   'optional, to confirmed that is not saved
   MsgBox "Not saved"      
End If
On Error GoTo 0         'to return standard error operation