6

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.

David Gard
  • 11,225
  • 36
  • 115
  • 227

3 Answers3

3

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
Kazimierz Jawor
  • 18,861
  • 7
  • 35
  • 55
  • 1
    Thanks for the reply. While I understand that you an skip errors in VBA, I'd prefer to understand why the error is being thrown if possible, and work around it that way. I will use this method if required to do so though. – David Gard Mar 14 '13 at 11:14
  • What I know is that VBA doesn't provide simple solutions for some user actions like your example or very popular problem with Cancel button for `Application.Inputbox` in Excel VBA. Therefore some other VBA implementations are reqired... – Kazimierz Jawor Mar 14 '13 at 11:18
  • @kaz Jaw That is incorrect, there is a simple solution without error handling. – brettdj Mar 14 '13 at 11:23
  • @KazJaw - Don't I know it! Some things that should be simple really are not. And as great as error handling is, VBA seems to rely on it far to much for some tasks that really should have a built in 'get out'. I have implemented the above for now, but if you do even come across another way to do it, I'd appreciate it if you could post it. Thanks. – David Gard Mar 14 '13 at 11:39
2

Updated: Now

1. Tests if the file has been previously saved or not
2. If the fileis unsaved, a controlled process is used to show the SaveAs dialog to either save the file, or handle the Cancel

code

Dim bSave As Boolean
If ActiveDocument.Path = vbNullString Then
bSave = Application.Dialogs(wdDialogFileSaveAs).Show
If Not bSave Then MsgBox "User cancelled", vbCritical
Else
ActiveDocument.Save
End If
brettdj
  • 54,857
  • 16
  • 114
  • 177
  • Thanks @bredttdj, but I'm afraid this doesn't work. `Compile error: Expected Function or variable`, with `.save` being indicated as the offending piece of code. – David Gard Mar 14 '13 at 11:35
  • @DavidGard while the code worked for me without any errors, I should have tested the existence of `Path` not `Saved`. Pls try the above code. – brettdj Mar 14 '13 at 11:43
  • Apologies, I mis-read your code. `.saved` does work, however I'm afraid you have misinterpreted my question (editing now to make it clearer). Some times the user will want to save previously unsaved documents using the Save As dialogue, and sometimes that will not. So unfortunately skipping it if the document is not already saved is not an option. I realise I can then ask the user if they wish to save, but that still gives them the option of pressing 'Cancel', so I'd be back to square one. Thanks. – David Gard Mar 14 '13 at 11:43
  • 1
    @brettdj - Tested and working, many thanks for your assistance in figuring this out. – David Gard Mar 14 '13 at 12:09
0

for vsto developers, please go here

    if (Globals.ThisAddIn.Application.ActiveDocument.Path == String.Empty)
        {
            Word.Dialog dlg;
            Object timeout = 3000;
            dlg = Globals.ThisAddIn.Application.Dialogs[
                Word.WdWordDialog.wdDialogFileSaveAs];
            int result = dlg.Display(ref timeout);
        }
        else
        {
            Globals.ThisAddIn.Application.ActiveDocument.Save();
        }

The result will store which button is pressed (0- cancel, 1- ok, 2- close)

Gomes
  • 3,330
  • 25
  • 17