本帖最后由 13907933959 于 2018-6-28 07:24 编辑
你好!
感谢回复! 经逐字比对,确实错了多处,修正后的代码按你说的,放到Normal模板的ThisDocument中,可还是不起作用,是具体的操作步骤不对?或是代码不完整?因我对VBA还是个门外汉,能否劳请你给一个详细的说明,将感激不尽!
Private WithEvents wdApp As Word.Application Private Const vNam As String = "上一次" Private Sub Document_Open() Set wdApp =Application '绑定对象,激活事件。 End Sub Private Sub wdApp_DocumentBentBeforeClose(ByVal Doc 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 As Document) 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 As Document, ByVal Var$) AsBoolean Dim vTemp$ On Error Resume Next vTemp = Doc.Variables(Var).Name VarExist = (vTemp <> "") End Function
|