为什么当宏为 运行 时 ThisWorkbook.SaveCopyAs 不起作用,但当它不是 运行 时却起作用?
Why won't ThisWorkbook.SaveCopyAs work when the macro is running, but it does when it's not running?
以下 VBA 代码适用于 运行 MS Office 2003。因为这就是我们价值数十亿美元的公司让我们与之合作的原因。 =)
好消息。 如果我在 IDE 中编辑代码并点击保存,它会完美运行。如果我正在处理电子表格本身,则相同。如果 none 存在,则创建一个备份文件夹,并在其中保存一个带日期的备份副本。
坏消息。当我运行主宏(太大post)时,下面的代码执行但不保存备份复制。该事件被正确调用。事实上,如果 none 存在,它将创建一个备份文件夹。每行得到 运行。变量都是正确的。错误处理有效。
简单地说,如果主宏是运行ning并调用ThisWorkbook.Save.[=14=,ThisWorkbook.SaveCopyAs将不起作用]
几个月前我才学习 VBA 这个项目,如果有明显的问题,我深表歉意。然而,我阅读了所有相关的 MSDN 文档并疯狂地用 Google 搜索,但没有任何结果。
在此先感谢您的协助。
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'********************************************************************
'Purpose: Triggered by ThisWorkbook.BeforeSave event
' Creates backup folder and saves date appended copies
'********************************************************************
Dim strBackupPath As String 'Path to Backup Folder
Dim strFarkPath As String 'Path to running workbook
Dim strBackupName As String 'Filename of backup
Dim strFullName As String 'Full path & filename of running workbook
Dim strBackupExtension As String 'Extension of backup
Dim strDestination As String 'Full path & filename of backup
Dim strDrive As String 'Drive letter
strFarkPath = Application.ActiveWorkbook.Path
strDrive = Left(strFarkPath, 1)
strBackupPath = strFarkPath & "\_Backups"
strBackupName = "\Backup-" & Year(Now) & "-" & Month(Now) & "-" & Day(Now)
strFullName = Application.ActiveWorkbook.FullName
strBackupExtension = Right(strFullName, Len(strFullName) - InStrRev(strFullName, ".", -1, vbTextCompare) + 1)
strDestination = strBackupPath & strBackupName & strBackupExtension
On Error GoTo Incorrect
If Len(Dir(strBackupPath, vbDirectory)) = 0 Then
MkDir strBackupPath
End If
Application.DisplayAlerts = False
ThisWorkbook.SaveCopyAs Filename:=strDestination
Application.DisplayAlerts = True
Exit Sub
Incorrect:
MsgBox "Unable to back record keeper up. Next time, please run the program from a location where you can read and write files.", vbCritical + vbOKOnly
End Sub
这是您现有子项的最后一部分,经过修改以创建一个副本。
请注意,您不能使用内置 FileCopy
进行复制(您将得到 "Permission Denied")
On Error GoTo Incorrect
If Len(Dir(strBackupPath, vbDirectory)) = 0 Then
MkDir strBackupPath
End If
Application.DisplayAlerts = False
Application.EnableEvents = False
ThisWorkbook.Save
CreateObject("scripting.filesystemobject").copyfile _
ThisWorkbook.FullName, strDestination
Application.EnableEvents = True '<<<<
Application.DisplayAlerts = True
Exit Sub
Incorrect:
Application.EnableEvents = True 'never leave this False!
MsgBox "Unable to back record keeper up. Next time, please run the program from a location where you can read and write files.", vbCritical + vbOKOnly
End Sub
以下 VBA 代码适用于 运行 MS Office 2003。因为这就是我们价值数十亿美元的公司让我们与之合作的原因。 =)
好消息。 如果我在 IDE 中编辑代码并点击保存,它会完美运行。如果我正在处理电子表格本身,则相同。如果 none 存在,则创建一个备份文件夹,并在其中保存一个带日期的备份副本。
坏消息。当我运行主宏(太大post)时,下面的代码执行但不保存备份复制。该事件被正确调用。事实上,如果 none 存在,它将创建一个备份文件夹。每行得到 运行。变量都是正确的。错误处理有效。
简单地说,如果主宏是运行ning并调用ThisWorkbook.Save.[=14=,ThisWorkbook.SaveCopyAs将不起作用]
几个月前我才学习 VBA 这个项目,如果有明显的问题,我深表歉意。然而,我阅读了所有相关的 MSDN 文档并疯狂地用 Google 搜索,但没有任何结果。
在此先感谢您的协助。
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'********************************************************************
'Purpose: Triggered by ThisWorkbook.BeforeSave event
' Creates backup folder and saves date appended copies
'********************************************************************
Dim strBackupPath As String 'Path to Backup Folder
Dim strFarkPath As String 'Path to running workbook
Dim strBackupName As String 'Filename of backup
Dim strFullName As String 'Full path & filename of running workbook
Dim strBackupExtension As String 'Extension of backup
Dim strDestination As String 'Full path & filename of backup
Dim strDrive As String 'Drive letter
strFarkPath = Application.ActiveWorkbook.Path
strDrive = Left(strFarkPath, 1)
strBackupPath = strFarkPath & "\_Backups"
strBackupName = "\Backup-" & Year(Now) & "-" & Month(Now) & "-" & Day(Now)
strFullName = Application.ActiveWorkbook.FullName
strBackupExtension = Right(strFullName, Len(strFullName) - InStrRev(strFullName, ".", -1, vbTextCompare) + 1)
strDestination = strBackupPath & strBackupName & strBackupExtension
On Error GoTo Incorrect
If Len(Dir(strBackupPath, vbDirectory)) = 0 Then
MkDir strBackupPath
End If
Application.DisplayAlerts = False
ThisWorkbook.SaveCopyAs Filename:=strDestination
Application.DisplayAlerts = True
Exit Sub
Incorrect:
MsgBox "Unable to back record keeper up. Next time, please run the program from a location where you can read and write files.", vbCritical + vbOKOnly
End Sub
这是您现有子项的最后一部分,经过修改以创建一个副本。
请注意,您不能使用内置 FileCopy
进行复制(您将得到 "Permission Denied")
On Error GoTo Incorrect
If Len(Dir(strBackupPath, vbDirectory)) = 0 Then
MkDir strBackupPath
End If
Application.DisplayAlerts = False
Application.EnableEvents = False
ThisWorkbook.Save
CreateObject("scripting.filesystemobject").copyfile _
ThisWorkbook.FullName, strDestination
Application.EnableEvents = True '<<<<
Application.DisplayAlerts = True
Exit Sub
Incorrect:
Application.EnableEvents = True 'never leave this False!
MsgBox "Unable to back record keeper up. Next time, please run the program from a location where you can read and write files.", vbCritical + vbOKOnly
End Sub