Get heading number from previous heading

左心房为你撑大大i 提交于 2019-12-11 06:28:10

问题


This Word macro written by Paul Beverley adds a comment to the document and inserts the page and line numbers.

Sub CommentAdd()
' Version 20.02.12
' Add a comment
' Ctrl-Alt-#
attrib1 = "PB: "
attrib2 = "PB: "

postText = ""
keepPaneOpen = False
addPageNum1 = True
addLineNum1 = True
addPageNum2 = True
addLineNum2 = True

highlightTheText = False
textHighlightColour = wdYellow
colourTheText = False
textColour = wdColorBlue

Set rng = Selection.Range
rng.Collapse wdCollapseEnd
rng.MoveEnd , 1
pageNum = rng.Information(wdActiveEndAdjustedPageNumber)  ' <<<<<----- This line
lineNum = rng.Information(wdFirstCharacterLineNumber)
If Selection.End <> Selection.Start Then
  If Right(Selection, 1) = Chr(32) Then Selection.MoveEnd wdCharacter, -1
  Set rng1 = Selection.Range
' Either highlight it ...
  myTrack = ActiveDocument.TrackRevisions
  ActiveDocument.TrackRevisions = False
  If highlightTheText = True Then Selection.Range.HighlightColorIndex _
       = textHighlightColour
' And/or change the text colour to red
  If colourTheText = True Then Selection.Font.Color = textColour
  ActiveDocument.TrackRevisions = myTrack
' Now add the comment
  Selection.Comments.Add Range:=Selection.Range
  If addPageNum1 = True Then attrib1 = attrib1 & "(p. " & _
       pageNum & ") "
  If addLineNum1 = True Then attrib1 = attrib1 & "(line " & _
       lineNum & ") "
  Selection.TypeText Text:=attrib1 & ChrW(8216) & ChrW(8217)
' Move back to between the close and open quotes
  Selection.MoveEnd wdCharacter, -1
' 'Paste' in a copy of the selected text
  Set rng2 = Selection.Range
  rng2.FormattedText = rng1.FormattedText
  rng2.Revisions.AcceptAll
  rng2.Start = rng2.End - 1
  If rng2.Text = Chr(13) Then rng2.Delete
' Move back past the close quote
  rng2.Start = rng2.End + 1
  If postText > "" Then
    rng2.InsertAfter Text:=postText
  Else
    rng2.InsertAfter Text:=" " & ChrW(8211) & " "
  End If
If keepPaneOpen = False Then ActiveWindow.ActivePane.Close
Else
  cmntText = attrib2
  If addPageNum2 = True Then cmntText = cmntText & _     ' <<<<<----- And this I guess
       "(p. " & pageNum & ") "
  If addLineNum2 = True Then cmntText = cmntText & _
       "(line " & lineNum & ") "
  Selection.MoveEnd , 1
  Selection.Comments.Add Range:=Selection.Range, Text:=cmntText
End If
End Sub

I have been trying (in Word's VB Editor) to tweak the highlighted line (here followed by '<<<<----- This line') to have the macro get and write the number of the previous header instead of the page number. The idea is that if the user selects a string, the macro looks for the closest header level (h1, h2, h3, etc.) above it and writes its number.

Example:

1 This is header1 > If the selected string happens to be under this header, then write "1"

1.2 This is header2 > If the selected string happens to be under this header, then write "1.2"

1.2.1 This is header3 > If the selected string happens to be under this header, then write "1.2.1"

So far I have tried these two options to replace the bolded line, to no avail (I have renamed "pageNum" to "headerNum"):

' Option 1
headerNum = ActiveDocument.Styles("Heading")

' Option 2
headerNum = Selection.range = Styles("Heading")

What am I doing wrong?

Any help will be highly appreciated!!!

来源:https://stackoverflow.com/questions/13970614/get-heading-number-from-previous-heading

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!