我的宏应该对当前正在保存的文件进行备份(在指定的路径中),这个过程经常被讨论,因为它是Word缺少的功能。
Private Sub Document_Close()
Dim blank As Range
For Each blank In ActiveDocument.StoryRanges
If Len(blank.Text) = 1 Then Exit Sub
Next
End Sub
Sub FileSave()
Dim BackupPath As String, objF As Object, retVal As Long, Rslt
BackupPath = "C:\Users\" & Environ("UserName") & "\Documents\BackupWord\"
With ActiveDocument
If .Path = "" Then: If Application.Dialogs(wdDialogFileSaveAs).Show <> -1 Then Exit Sub
If Len(Trim(.Range.Text)) = 1 Then Exit Sub
.Save
If Dir(BackupPath, vbDirectory) = "" Then
MkDir BackupPath
MsgBox "Backup folder has been created.", vbInformation
End If
If .Path & "\" = BackupPath Then
MsgBox "WARNING! Backup folder is the same as the source folder", vbExclamation
Exit Sub
End If
Set objF = CreateObject("Scripting.FileSystemObject")
retVal = -1
On Error Resume Next
retVal = objF.CopyFile(.FullName, BackupPath & .Name, True)
On Error GoTo 0
Set objF = Nothing
If retVal <> 0 Then MsgBox "Backup has not been copied to folder " BackupPath, vbExclamation
End With
End Sub
我将描述宏当前的作用。
我的宏无法两次备份。
答案 0 :(得分:1)
你需要一个班级,我相信在addin级别跟踪所有表格,比如......
Option Explicit
Private WithEvents wd As Word.Application
Public Sub initialise(w As Word.Application)
Set wd = w
End Sub
Private Sub wd_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
' Your code here
End Sub
在您的插件(.dotm)文件中,您有以下内容
Option Explicit
Public c As clsCustomWord
Sub AutoExec()
Set c = New clsCustomWord
c.initialise Application
End Sub
希望这有帮助