AutoClose宏.结束关闭动作基于答案没有保存提示



我有一个word文档,文档末尾有一个字段,用于更新文件路径,最后保存日期&最后一次保存由。我需要这些在文件关闭时更新。

我正在运行一个自动关闭宏来更新字段。问题是我需要在这些字段更新之前保存工作簿,因此最后保存日期和最后保存时间将更新。

我将重新创建"您是否要保存更改"消息框,带有是/否/取消选项。我有两个问题1-如果用户说"不"或"取消"..我如何抑制系统生成的信息?你想保存以避免重复相同的问题吗?(或者把他们对我的问题版本的回答传递给系统,这样他们就看不到了)2 -如果选择取消,如何停止系统关闭文件的动作?

我在网上发现,sendkeys (ESC)没有阻止系统关闭。'

Sub AutoClose()
'if the document is already saved
If ActiveDocument.Saved = True Then
'update all fields, save, close without prompt
Else
'which would be the document's status is not saved
answer = MsgBox("Yes No Cancel Example", vbYesNoCancel)
'above is do you want to save

If answer = vbYes Then
MsgBox "Yes"
ElseIf answer = vbNo Then
'dont save, dont update, close file without prompt
Else
'this is the cancel, so dont update, dont close
ActiveDocument.Saved = True
SendKeys "{ESC}"

End If
End If
End Sub

可能:

Sub AutoClose()
Application.ScreenUpdating = False
Dim Rslt
With ActiveDocument
If .Saved = True Then
Call UpdateFields
.Save
Else
Rslt = MsgBox("Save changes and close, or discard changes and close?", vbYesNoCancel)
If Rslt = vbYes Then
Call UpdateFields
.Save
ElseIf Rslt = vbNo Then
.Saved = True
Else
Call UpdateFields
Application.ScreenUpdating = True
Exit Sub
End If
End If
End Sub
Sub UpdateFields()
With ActiveDocument
.Fields.Update
.PrintPreview
.ClosePrintPreview
End With
End Sub

最新更新