我有一个宏,可以以特定的方式格式化文档,然后使用ActiveDocument.Save
保存。
但是,有时文档尚未保存,在某些情况下我不想保存它。不幸的是,当显示“另存为”对话框时,单击“取消”会导致运行时错误(4198)-
命令失败
有人知道如何防止发生这种情况吗?谢谢。
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
Application.Inputbox
时遇到的非常普遍的取消按钮问题。因此,需要一些其他的VBA实现... - Kazimierz Jawor更新:现在
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
.save
被指定为有问题的代码片段。 - David GardPath
的存在而不是 Saved
。请尝试使用上述代码。 - brettdj.saved
确实有效,但恐怕你误解了我的问题(现在正在编辑以使其更清晰)。有时用户会想要使用“另存为”对话框保存之前未保存的文档,有时则不需要。因此,如果文档尚未保存,则无法跳过它。我意识到我可以询问用户是否希望保存,但这仍然给他们按“取消”的选项,所以我又回到了起点。谢谢。 - David Gard针对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- 关闭)
On Error
路线。可悲的是,为了实现我想要的功能,必须选择On Error
,这并不是一件好事! - David Gard