1. 程式人生 > >根據段落編號自動添加書簽的VBA

根據段落編號自動添加書簽的VBA

換行符 rep ndk 編號 鼠標 字符 bookmarks 一個 mat

操作方法,鼠標逐一點擊標題行,執行本宏,自動將標題編號取出作為書簽名稱

Sub 宏1() ‘ ‘ 宏1 宏 ‘ ‘

Dim myRange As Word.Range

Dim num As String, content As String

Selection.HomeKey Unit:=wdLine

Selection.EndKey Unit:=wdLine, Extend:=wdExtend

Set myRange = Selection.Range

With myRange ‘把Range結束範圍往前移一個字符,目的是為了不包括換行符

.MoveEnd Unit:=wdWord, Count:=-1

‘取出段落序號

num = Trim(.ListFormat.ListString)

‘取出Heading的內容

content = Trim(.Text) End With

If num <> "" Then

num = Replace(num, ".", "")

With ActiveDocument.Bookmarks

.Add Range:=Selection.Range, Name:="P" + num

.DefaultSorting = wdSortByName

.ShowHidden = False

End With

End If

End Sub

根據段落編號自動添加書簽的VBA