Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #13  
Old 01-03-2012, 09:04 PM
macropod's Avatar
macropod macropod is online now Please help copy inside brackets Windows 7 64bit Please help copy inside brackets Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,514
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi Jana,

Here's an improved version - it allows you to pick the Heading level!
Code:
Sub InsertRefs()
Application.ScreenUpdating = False
Dim RngHdA As Range, RngHdB As Range, RngRef As Range
Dim iRefHd As Long, StrTxt As String, oPara As Paragraph
On Error Resume Next
iRefHd = InputBox("What is the Reference Heading Level Number (from 1 to 8)?", "Heading Selector")
' Word's inbuilt heading styles are indexed as -2 to -10, so invert the input # and subtract 1
iRefHd = -iRefHd - 1
On Error GoTo 0
' Valid #s must be between -2 (Heading 1) and -9 (Heading 8)
If iRefHd > -2 Or iRefHd < -9 Then Exit Sub
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .Style = iRefHd
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  Do While .Find.Found
    Set RngHdA = .Paragraphs(1).Range.Duplicate
    With RngHdA
      On Error GoTo ParaLast
      While ActiveDocument.Styles(.Paragraphs.Last.Next.Style).BuiltIn = False Or _
        .Paragraphs.Last.Next.Style > ActiveDocument.Styles(iRefHd) Or _
        .Paragraphs.Last.Next.Style < ActiveDocument.Styles(iRefHd + 1)
          .MoveEnd wdParagraph, 1
      Wend
ParaLast:
      StrTxt = ""
      If .Paragraphs.Count > 2 Then
        Set RngRef = RngHdA.Paragraphs(3).Range.Characters.Last
        .MoveStart wdParagraph, 3
        Set RngHdB = RngHdA
        With RngRef
          .MoveEnd wdCharacter, -2
          For Each oPara In RngHdB.Paragraphs
            If ActiveDocument.Styles(oPara.Style).BuiltIn = True Then
              ' To get all lower Heading Styles, change '(iRefHd - 2)' to '(-11)'
              If oPara.Style < ActiveDocument.Styles(iRefHd - 2) And _
                oPara.Style > ActiveDocument.Styles(iRefHd) Then
                If Len(Trim(oPara.Range.Text)) > 1 Then
                  StrTxt = StrTxt & Left(oPara.Range.Text, Len(oPara.Range.Text) - 1) & ", "
                End If
              End If
            End If
          Next
          If Len(StrTxt) > 0 Then
            StrTxt = "{" & Left(StrTxt, Len(StrTxt) - 2) & "}"
            .InsertAfter StrTxt
          End If
        End With
      End If
    End With
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub
As coded, the macro only looks reports Heading Styles one level below the chose one. To get all lower Heading Styles, change '(iRefHd - 2)' to '(-11)' where indicated in the code.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Gray square brackets waldux Word 8 09-25-2013 04:14 PM
Please help copy inside brackets Find/Replace Brackets Problem fatso Word 2 08-04-2011 11:34 AM
brackets citation uncung Word 1 07-13-2011 01:39 PM
Brackets Issue... DarkJudge1 Outlook 0 07-06-2010 05:15 PM
copy a file which does not have copy option jkind Word 0 01-17-2010 09:25 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:57 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft