Private Sub Document_Close()
Dim bSaved As Boolean, bRtn
With ActiveDocument
If .Name Like "Document#*" And .Saved = True Then Exit Sub
bSaved = .Saved
If .Bookmarks.Exists("_DocClose") Then
bRtn = MsgBox(Prompt:="Save current location for next time?" & vbCr & _
"Yes to update 'return to' location" & vbCr & _
"No to delete previous 'return to' location" & vbCr & _
"Cancel to retain previous 'return to' location", _
buttons:=vbYesNoCancel, Title:="Resume Location") = vbYes
Else
bRtn = MsgBox(Prompt:="Save current location for next time?", _
buttons:=vbYesNo, Title:="Resume Location") = vbYes
End If
If bRtn = vbYes Then _
.Bookmarks.Add Name:="_DocClose", Range:=Selection.Range.Characters.First
If bRtn = vbNo Then _
If .Bookmarks.Exists("_DocClose") Then .Bookmarks("_DocClose").Delete
If bSaved = True And .Saved = False Then .Save
End With
End Sub
我是VBA的新手,是的,我确实意识到这个问题有更简单的问题,但它们都不适用于我的特定代码。我从this website
上提出的问题中获得了上述代码当我在创建宏之后在视图宏窗口中查看宏时,它没有显示但是代码仍然存在于VBA编辑器中,我可以让宏显示的唯一原因是添加“ Sub AutoClose()“在代码块的开头,但是当关闭文档时,VBA编辑器打开,我得到一个弹出窗口,显示”编译错误:预期的结束子“VBA编辑器自动突出显示我添加的行。我试图在运行Windows 10操作系统的笔记本电脑上的Word 2016中执行此操作。