Sub CreateBookMarkMenu() Dim bkBookmark As Bookmark Dim cbrBar As CommandBar Dim cbrPopup As CommandBarPopup Dim cbrButton As CommandBarButton Dim ShowHiddenStatus As Boolean '查看隐藏书签是否设置为“可见”, '将此设置存储在一个变量中,以便在结尾返回。 '然后使隐藏的书签不可见 '(不希望交叉引用等出现在菜单中) ShowHiddenStatus = ActiveDocument.Bookmarks.ShowHiddenActiveDocument.Bookmarks.ShowHidden = False Set cbrBar = CommandBars.ActiveMenuBar '如果已经存在,则首先删除书签菜单 Set cbrPopup = CommandBars.FindControl(Tag:="重新创建") If Not cbrPopup Is Nothing Then cbrPopup.Delete End If If ActiveDocument.Bookmarks.count > 0 Then Set cbrPopup = cbrBar.Controls.Add(Type:=msoControlPopup, Before:=cbrBar.Controls.count + 1) With cbrPopup.Caption = "书签" .Tag = "重新创建" End With For Each bkBookmark In ActiveDocument.Bookmarks Set cbrButton = cbrPopup.Controls.Add(Type:=msoControlButton) With cbrButton .Caption = bkBookmark.Name .Style = msoButtonCaption .OnAction = "SelectBookMark" End With Next bkBookmark '在底部添加刷新按钮 Set cbrButton = cbrPopup.Controls.Add(Type:=msoControlButton) With cbrButton.Caption = "刷新列表" .Style = msoButtonCaption .OnAction = "CreateBookMarkMenu" .BeginGroup = True End With End If ActiveDocument.Bookmarks.ShowHidden = ShowHiddenStatus Set cbrButton = Nothing Set cbrPopup = Nothing Set cbrBar = Nothing Set bkBookmark = Nothing End Sub
时间: 2023-12-06 13:43:53 浏览: 118
这段代码是用于创建一个书签菜单的子程序。它首先检查是否存在名为“重新创建”的命令栏弹出菜单,如果存在,则删除它。然后,它将书签菜单添加到活动文档的菜单栏中,并为每个书签添加一个菜单项。最后,它添加一个“刷新列表”按钮以便用户可以更新书签列表。在代码执行期间,它还将检查是否存在隐藏书签,并将它们设置为不可见,以确保不会在菜单中显示。
阅读全文