View Single Post
 
Old 03-05-2015, 08:17 PM
Guessed's Avatar
Guessed Guessed is offline Windows 7 32bit Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,158
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Try the next iteration of the concept. It still has the bookmark uniqueness issue that I spoke about but the rest of it should show you how the code can arrive at the preceding heading's parent.
Code:
Sub ProofOfConcept()
  'macro by Andrew Lockton
  Dim aRng As Range, iLev As Integer
  Set aRng = ActiveDocument.Bookmarks("\page").Range
  aRng.InsertParagraphBefore
  With aRng.Paragraphs(1)
    .Style = "Normal"
    .OutlinePromote
    iLev = .OutlineLevel
    If iLev > 3 Then
      .Style = "Normal"
      aRng.Collapse Direction:=wdCollapseStart
      aRng.Select
      Selection.GoTo What:=wdGoToHeading, Which:=wdGoToPrevious, Count:=1
      While Selection.Paragraphs(1).OutlineLevel <> iLev - 1
        Selection.GoTo What:=wdGoToHeading, Which:=wdGoToPrevious, Count:=1
      Wend
      
      ActiveDocument.Bookmarks.Add Name:="ABC", Range:=Selection.Range
      aRng.InsertAfter " continued"
      aRng.Collapse Direction:=wdCollapseStart
      ActiveDocument.Fields.Add Range:=aRng, Text:="Ref ABC \w \h"
      aRng.Select
    Else
      aRng.Paragraphs(1).Range.Delete
    End If
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote