网上有许多代码,但是我是新手,不知道怎么操作,请图解,另存到E:\备份。
最好请图解,满意加分,playboy6537@tom.com
如果是操作过程中自动备份,这个功能word和excel自带。
如果是为了操作完毕,关掉文档时,自动将当前文档复制一份到e盘。这个有两种方式。
1、用vba方式
2、调用bat方式
假设你的文件都在C:\My document\目录中,并且你希望把这些文件备份在E:\备份中,你的EXCEL安装在默认目录下
1。先打开记事本,输入以下语句:
Start/W "C:\Program Files\Microsoft office\office\EXCEL.EXE" %1
XCOPY/M/D/Y/S "C:\My document\*.xls" E:\备份
保存为.bat文件后退出。
2。在桌面为这个文件建个快捷方式,然后右击,选择属性->程序->运行->最小化
以后要打开EXCEL就用这个快捷方式,它会自动打开EXCEL,并且在你退出EXCEL之后自动备份你刚才修改过的EXCEL文件,没有修改的不备份。
3这种备份方法同样适用于word等其它的程序,而且是全自动的。
解释一下这两句命令:
start是启动一个程序,加上/W是等待的意思,也就是说等这个程序关闭才执行下面的语句。
XCOPY的/M参数是指拷贝具有存档属性的文件,几乎所有的程序都会在修改文件后给文件加上存档属性,此文件在拷贝后会去掉存档属性。
XCOPY的/D参数是指拷贝的源文件比目标文件的日期要新。
XCOPY的/Y是指复盖文件不提示。
XCOPY的/S参数是指拷贝子目录。
如果是为了操作完毕,关掉文档时,自动将当前文档复制一份到e盘。这个有两种方式。
1、用vba方式
2、调用bat方式
假设你的文件都在C:\My document\目录中,并且你希望把这些文件备份在E:\备份中,你的EXCEL安装在默认目录下
1。先打开记事本,输入以下语句:
Start/W "C:\Program Files\Microsoft office\office\EXCEL.EXE" %1
XCOPY/M/D/Y/S "C:\My document\*.xls" E:\备份
保存为.bat文件后退出。
2。在桌面为这个文件建个快捷方式,然后右击,选择属性->程序->运行->最小化
以后要打开EXCEL就用这个快捷方式,它会自动打开EXCEL,并且在你退出EXCEL之后自动备份你刚才修改过的EXCEL文件,没有修改的不备份。
3这种备份方法同样适用于word等其它的程序,而且是全自动的。
解释一下这两句命令:
start是启动一个程序,加上/W是等待的意思,也就是说等这个程序关闭才执行下面的语句。
XCOPY的/M参数是指拷贝具有存档属性的文件,几乎所有的程序都会在修改文件后给文件加上存档属性,此文件在拷贝后会去掉存档属性。
XCOPY的/D参数是指拷贝的源文件比目标文件的日期要新。
XCOPY的/Y是指复盖文件不提示。
XCOPY的/S参数是指拷贝子目录。
温馨提示:答案为网友推荐,仅供参考
第1个回答 2010-08-26
按Alt+F11,
贴入
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
ThisWorkbook.SaveAs "e:\备份" & Format(Date, "yyyymmdd") & ".xls"
End Sub
贴入
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
ThisWorkbook.SaveAs "e:\备份" & Format(Date, "yyyymmdd") & ".xls"
End Sub
第2个回答 2010-08-27
Option Explicit '放在类模块中
Public WithEvents WordApplication As Word.Application
Private Const c_strFileNamePrefix = ""
Private Sub WordApplication_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
On Error Resume Next
Dim fso As Object
Dim strOldName As String, strNewName As String, oArray As Variant, ErrArray() As Variant
Set fso = CreateObject("Scripting.FileSystemObject")
strOldName = ActiveDocument.FullName
strNewName = Doc.Range(Start:=Doc.Paragraphs(1).Range.Start, _
End:=Doc.Paragraphs(2).Range.End)
ErrArray = Array("\", "/", "*", "?", "<", ">", "|", """", Chr(9), Chr(11), Chr(13))
For Each oArray In ErrArray '遍历替换
strNewName = Replace(strNewName, oArray, "")
Next
If UCase(strNewName) <> UCase(strOldName) Then
ChangeFileOpenDirectory "E:\备份\"
ActiveDocument.SaveAs strNewName
fso.DeleteFile strOldName
Cancel = True
End If
Set fso = Nothing
End Sub
'------------------------------
Option Explicit '放在this中
Dim oLogFileCreator As LogFileCreator
Private Sub Document_Open()
On Error Resume Next
Set oLogFileCreator = New LogFileCreator
Set oLogFileCreator.WordApplication = Application
End Sub
Private Sub Document_Close()
On Error Resume Next
Set oLogFileCreator.WordApplication = Nothing
Set oLogFileCreator = Nothing
End Sub
Public WithEvents WordApplication As Word.Application
Private Const c_strFileNamePrefix = ""
Private Sub WordApplication_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
On Error Resume Next
Dim fso As Object
Dim strOldName As String, strNewName As String, oArray As Variant, ErrArray() As Variant
Set fso = CreateObject("Scripting.FileSystemObject")
strOldName = ActiveDocument.FullName
strNewName = Doc.Range(Start:=Doc.Paragraphs(1).Range.Start, _
End:=Doc.Paragraphs(2).Range.End)
ErrArray = Array("\", "/", "*", "?", "<", ">", "|", """", Chr(9), Chr(11), Chr(13))
For Each oArray In ErrArray '遍历替换
strNewName = Replace(strNewName, oArray, "")
Next
If UCase(strNewName) <> UCase(strOldName) Then
ChangeFileOpenDirectory "E:\备份\"
ActiveDocument.SaveAs strNewName
fso.DeleteFile strOldName
Cancel = True
End If
Set fso = Nothing
End Sub
'------------------------------
Option Explicit '放在this中
Dim oLogFileCreator As LogFileCreator
Private Sub Document_Open()
On Error Resume Next
Set oLogFileCreator = New LogFileCreator
Set oLogFileCreator.WordApplication = Application
End Sub
Private Sub Document_Close()
On Error Resume Next
Set oLogFileCreator.WordApplication = Nothing
Set oLogFileCreator = Nothing
End Sub
第3个回答 2010-08-27
什么叫自动保存,总有条件吧,比如关闭前自动保存、打开EXCEL或WORD前自动保存或者定时保存。你这个问题提的也真是……我给你段代码是在打开时自动保存(EXCEL的)
Private Sub Workbook_Open()
If Dir("E:\备份", vbDirectory) = "" Then '查检E盘中有没有“备份”文件夹
MkDir "E:\备份" '没有就建一个“备份”文件夹
End If
Application.DisplayAlerts = False '取消提示
ThisWorkbook.SaveAs "E:\备份\" & ThisWorkbook.Name '备份文件
Application.DisplayAlerts = True '恢复提示
2L的路径和文件名不正确,请检查。
QQ:394901158
给你录像,可以看得很明白
Private Sub Workbook_Open()
If Dir("E:\备份", vbDirectory) = "" Then '查检E盘中有没有“备份”文件夹
MkDir "E:\备份" '没有就建一个“备份”文件夹
End If
Application.DisplayAlerts = False '取消提示
ThisWorkbook.SaveAs "E:\备份\" & ThisWorkbook.Name '备份文件
Application.DisplayAlerts = True '恢复提示
2L的路径和文件名不正确,请检查。
QQ:394901158
给你录像,可以看得很明白
第4个回答 推荐于2016-04-20
excel vba代码:
Sub 工作簿备份()
Dim s, f As String, sht As Worksheet
f = [a3]
If f = "" Then Exit Sub
Application.ScreenUpdating = False
Workbooks.Add (xlWBATWorksheet)
Sheets(1).Name = "临时"
For Each sht In ThisWorkbook.Sheets
With ActiveWorkbook
sht.Copy after:=.Sheets(.Sheets.Count)
End With
Next
Application.DisplayAlerts = False
Sheets("临时").Delete
Sheets(1).Activate
Application.DisplayAlerts = True
Application.ScreenUpdating = True
s = Application.Dialogs(5).Show(arg1:=f & ".xls")
If s = True Then
ActiveWorkbook.Close
MsgBox "备份完毕"
Else
MsgBox "没有备份"
End If
End Sub本回答被提问者采纳
Sub 工作簿备份()
Dim s, f As String, sht As Worksheet
f = [a3]
If f = "" Then Exit Sub
Application.ScreenUpdating = False
Workbooks.Add (xlWBATWorksheet)
Sheets(1).Name = "临时"
For Each sht In ThisWorkbook.Sheets
With ActiveWorkbook
sht.Copy after:=.Sheets(.Sheets.Count)
End With
Next
Application.DisplayAlerts = False
Sheets("临时").Delete
Sheets(1).Activate
Application.DisplayAlerts = True
Application.ScreenUpdating = True
s = Application.Dialogs(5).Show(arg1:=f & ".xls")
If s = True Then
ActiveWorkbook.Close
MsgBox "备份完毕"
Else
MsgBox "没有备份"
End If
End Sub本回答被提问者采纳