6

我有一个宏,它以某种方式格式化文档,然后使用ActiveDocument.Save.

但是,有时文档还没有保存,在某些情况下我不想保存它。不幸的是,在显示“另存为”对话框时单击“取消”会导致运行时错误 (4198) -

命令失败

有谁知道我怎样才能防止这种情况发生?谢谢。

4

3 回答 3

3

请通过添加一些错误处理说明尝试以下操作:

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
于 2013-03-14T11:05:30.960 回答
2

更新:现在

1. 测试文件之前是否已保存
2. 如果文件未保存,则使用受控进程显示SaveAs对话框以保存文件或处理Cancel

代码

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
于 2013-03-14T11:22:10.573 回答
0

对于 vsto 开发者,请到这里

    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();
        }

结果将存储按下哪个按钮(0-取消,1-确定,2-关闭)

于 2014-07-16T14:09:09.130 回答