有时候,在使用Word进行文字编辑的时候,突然遇到断电,或者电脑自动关机、死机等故障的时候,导致辛辛苦苦做出来的劳动成果全部泡汤,是很多网友都非常抓狂的事情。今天小编就教大家利用VBA代码,实现自动备份功能,使Word每隔几分钟可以自动备份,而不影响正在编辑的文件。接下来就一起来了解一下吧!
1、打开Word文件的 VBA编辑器,快捷键 Alt+F11,右击【Thisdocument】-》 【插入模块】;

用VBA代码设置Word自动保存的步骤
2、双击刚才插入的【模块1】,添加如下代码:
Sub 自动备份()
Dim NewTime
NewTime = Now + Timevalue(“00:05:10”)
Dim myPath$, myName$
myPath = Activedocument.Path
myName = Left$(Activedocument.Name, Len(Activedocument.Name) - 4)
ChangeFileOpenDirectory myPath
Activedocument.SaveAs FileName:=myName & “_temp.doc”, ReadOnlyRecommended:=True
Activedocument.SaveAs FileName:=myName & “.doc”, ReadOnlyRecommended:=False
Application.onTime NewTime, “自动备份”
CreateObject(“Wscript.shell”).popup “备份成功,备份文件名为:” & myName & “_temp.doc”, 2, “提示!2秒后自动关闭!”
End Sub

用VBA代码设置Word自动保存的步骤

用VBA代码设置Word自动保存的步骤
3、双击【Thisdocument】并在其中 添加如下代码:
Private Sub document_Open()
Call 自动备份
End Sub

用VBA代码设置Word自动保存的步骤
4、默认自动备份时间为5min,如要调整请修改【模块1】中一句代码:如图中红框所示:
时间格式为:HH : mm : ss

用VBA代码设置Word自动保存的步骤
5、保存代码及文件,且关闭word并重新打开,重新打开点击【选项】-》 【启用此内容】,如图:

用VBA代码设置Word自动保存的步骤
6、默认备份文件名为:【原文件名_temp,Lee.doc】且为只读,提示对话框2s后自动关闭。备份效果显示如下:

用VBA代码设置Word自动保存的步骤