请参: '* +++++++++++++++++++++++++++++++++++++++ '* Created By 守柔(ShouRou)@ExcelHome 2005-1-15 15:54:27 '仅测试于System: Windows NT Word: 10.0 Language: 2052 '^The Code CopyIn [ThisDocument-ThisDocument]^' '* -------------------------------------------------------------------------- Sub AddBookMarks()
Dim LineCount As Integer, RangeStart As Long, RangeEnd As Long, MyRange As Range
Dim strBK As String
On Error Resume Next
With ActiveDocument
If .Content.End <= 1 Then Exit Sub '如果没有文档内容则退出宏
LineCount = .BuiltInDocumentProperties("Number of lines").Value
For i = 1 To LineCount
RangeStart = .GoTo(wdGoToLine, , i).Start '行起点
'如果到达最后一行,则为文档尾位置
RangeEnd = VBA.IIf(i = LineCount, .Content.End, .GoTo(wdGoToLine, , i + 1).Start)
'定义一个RANGE对象
Set MyRange = .Range(RangeStart, RangeEnd)
'添加书签
If InStr(MyRange, "#") > 0 Then strBK = .Range(MyRange.Start, MyRange.Start + InStr(MyRange, "#") - 1)
' MsgBox strBK
MyRange.Bookmarks.Add Name:=strBK '命名书签
Next
End With
End Sub
'---------------------- 注意:书签不得以数字开头,如果你的每行开头是数字的话,可能不能添加为书签。另外,我觉得楼主肯定是每行,而不是每段?应为每段的处理要比每行容易和方便也科学得多。 |