你好! 代码再修正了,放到Normal模板的ThisDocument中,可还是不起作用!
Private WithEvents wdApp As Word.Application Private Const vName As String = "上一次" Private Sub Document_Open() Set wdApp = Application '绑定对象,激活事件。 End Sub Private Sub wdApp_DocumentBentBeforeClose(ByValDoc As Document, Cancel As Boolean) Dim strTemp$: strTemp = Selection.Start& "|" & Selection.End If VarExist(Doc, vName) Then Doc.Variables(vName) = strTemp Else Doc.Variables.AddvName, strTemp End If End Sub Private Sub wdApp_DocumentOpen(ByVal Doc AsDocument) If VarExist(Doc, vName) Then Dim arr arr = Split(Doc.Variables(vName).Value,"|") Doc.Range(arr(0), arr(1)).Select Doc.Saved = True '避免麻烦。 End If End Sub Private Function VarExist(ByVal Doc AsDocument, ByVal Var$) As Boolean Dim vTemp$ On Error Resume Next vTemp = Doc.Variables(Var).Name VarExist = (vTemp <> "") End Function
|