在自动关闭工作簿之前保存文件
save file before auto closing workbook
如果我的工作簿闲置 1 分钟,我想关闭它。但是在关闭之前,我想保存它的备份,但不对原始文件进行任何更改。我如何合并此代码:
ActiveWorkbook.SaveAs filename:=filename, FileFormat:=xlWorkbookNormal
进入这个程序
Sub SetTimer()
Dim bookname
Dim filename
DownTime = Now + TimeValue("00:01:00")
bookname = ActiveWorkbook.Name
filename = "C:\myhome\backups\" & bookname
Application.OnTime EarliestTime:=DownTime, _
Procedure:="ShutDown", Schedule:=True
End Sub
如果我将它插入到代码中,它会要求我在计时器完成之前保存。我想让它在时间结束后询问。
只需为 Workbook_BeforeClose 添加一个事件处理程序。我会亲自检查它是否也被更改,这样你就不会得到一堆不必要的备份:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.DisplayAlerts = False
If Not Me.Saved Then
Me.SaveAs "C:\myhome\backups\" & Me.Name, xlWorkbookNormal
End If
Application.DisplayAlerts = True
End Sub
我想这就是你想要的:
Public Sub SetTimer()
Dim DownTime As Date
DownTime = Now + TimeValue("00:01:00")
Application.OnTime EarliestTime:=DownTime, _
Procedure:="ShutDown", Schedule:=True
End Sub
Private Sub ShutDown()
' Be careful about using ActiveWorkbook vs ThisWorkbook vs getting a direct reference to the required workbook.
Dim bookname As String
bookname = ActiveWorkbook.Name
Dim filename As String
filename = "C:\myhome\backups\" & bookname
ActiveWorkbook.SaveAs filename:=filename, FileFormat:=xlWorkbookNormal
End Sub
您需要将 SaveAs
放入一个名为 "ShutDown" 的单独方法中。我假设您将它放在原始方法的末尾。对 Application.OnTime()
运行 的调用和稍后调用的调度 ShutDown()
然后立即继续 运行 SetTimer()
中的其余代码。
如果我的工作簿闲置 1 分钟,我想关闭它。但是在关闭之前,我想保存它的备份,但不对原始文件进行任何更改。我如何合并此代码:
ActiveWorkbook.SaveAs filename:=filename, FileFormat:=xlWorkbookNormal
进入这个程序
Sub SetTimer()
Dim bookname
Dim filename
DownTime = Now + TimeValue("00:01:00")
bookname = ActiveWorkbook.Name
filename = "C:\myhome\backups\" & bookname
Application.OnTime EarliestTime:=DownTime, _
Procedure:="ShutDown", Schedule:=True
End Sub
如果我将它插入到代码中,它会要求我在计时器完成之前保存。我想让它在时间结束后询问。
只需为 Workbook_BeforeClose 添加一个事件处理程序。我会亲自检查它是否也被更改,这样你就不会得到一堆不必要的备份:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.DisplayAlerts = False
If Not Me.Saved Then
Me.SaveAs "C:\myhome\backups\" & Me.Name, xlWorkbookNormal
End If
Application.DisplayAlerts = True
End Sub
我想这就是你想要的:
Public Sub SetTimer()
Dim DownTime As Date
DownTime = Now + TimeValue("00:01:00")
Application.OnTime EarliestTime:=DownTime, _
Procedure:="ShutDown", Schedule:=True
End Sub
Private Sub ShutDown()
' Be careful about using ActiveWorkbook vs ThisWorkbook vs getting a direct reference to the required workbook.
Dim bookname As String
bookname = ActiveWorkbook.Name
Dim filename As String
filename = "C:\myhome\backups\" & bookname
ActiveWorkbook.SaveAs filename:=filename, FileFormat:=xlWorkbookNormal
End Sub
您需要将 SaveAs
放入一个名为 "ShutDown" 的单独方法中。我假设您将它放在原始方法的末尾。对 Application.OnTime()
运行 的调用和稍后调用的调度 ShutDown()
然后立即继续 运行 SetTimer()
中的其余代码。