我编写了代码,用宏执行替换标准 WORD 文档保存。但有一个问题,宏偶尔会自动执行一次,因此会干扰文档的工作。
我的代码:
Private WithEvents App As Word.Application
Sub Document_Open()
Call LogSet App = Word.Application
End Sub
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
If Doc = ThisDocument Then
Application.Run "save"
Cancel = True
End If
End Sub
我认为问题与文档的自动保存有关,但我不知道如何仅在本文档中禁用自动保存,而不是一次全部禁用。
它应该按标准工作,以便当用户尝试保存文档时,应取消标准保存并启动宏。
如何配置 beforeSave 函数仅在用户尝试手动保存文档时才执行,并且该函数不响应文档的自动保存?
这个解决方案很有帮助——打开一本书时完全禁用自动保存,退出时重新打开它。更新代码:
Private WithEvents App As Word.Application
Sub Document_Open()
'Call Log
With Application.Options
.SaveInterval = 0
.SaveNormalPrompt = False
End With
Set App = Word.Application
End Sub
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
If Doc = ThisDocument Then
Application.Run "save"
Cancel = True
End If
End Sub
Private Sub Document_Close()
With Application.Options
.SaveInterval = 10
.SaveNormalPrompt = True
End With
End Sub