Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Search for Style via VBA 1

Status
Not open for further replies.

IanPatUE

Technical User
Sep 23, 2001
5
AU
Hi,

I am trying to determine which heading the selected text is under when the macro is executed.

Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
With Selection.Find
.Text = "^p"
.Replacement.Text = ""
.Forward = False
.Wrap = False
.Format = True
End With
Selection.Find.Execute
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend

The code above searches upward for the nearest paragraph marker with a Style name of Heading 1. It then highlights to the start of the line. By doing this the selection contains the heading without the paragraph marker. This is the end result that I am looking for...

However, the nearest heading above the selection may be any of the Heading styles. Thus I would like the code to search upward for the nearest paragraph marker with a style name that contains "Heading".

Any suggestions?
 
'**********************************************************************
Application.ScreenUpdating = False
While Not Selection.Information(wdFirstCharacterLineNumber) = 1 And _
Not Left(Selection.Style, 7) = "Heading"
Selection.MoveUp Unit:=wdLine, Count:=1
Wend
Application.ScreenUpdating = True
MsgBox Selection.Style

i hope it helps you
ide
 
Hi,

Many thanks! This snippet of code was very handy.

Ian P.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top