Finding a style within a selected page

  • Thread starter Roderick O'Regan
  • Start date
R

Roderick O'Regan

I have a document split into "chapters". Each of these chapters is
headed by a specific style - and in no other place within the
"chapter". Let's call it Heading 1 for clarity.

Is there a way, please, of saying in VBA: "Is my selection point
within a page containing Heading 1"?

Roderick
 
G

Greg Maxey

Roderick,

Something like the following might do.

Sub Test()
Dim oStyle As Style
Dim oRng As Range
Dim oPara As Paragraph
Dim bAnswer As Boolean

Set oRng = ActiveDocument.Bookmarks("\Page").Range
bAnswer = False
For Each oPara In oRng.Paragraphs
Set oStyle = oPara.Style
If oStyle = "Heading 1" Then
bAnswer = True
Set oStyle = Nothing
Exit For
End If
Set oStyle = Nothing
Next

If bAnswer Then
MsgBox "You wanted VBA to ask if your selection point" _
& " is on a page containing the Heading 1 Style." _
& "The questions been asked and the answer" _
& " is yes."
Else
Msgbox "You wanted VBA to ask if your selection point" _
& " is on a page containing the Heading 1 Style." _
& "The questions been asked and the answer" _
& " is no."
End Sub
 
R

Roderick O'Regan

Thank you Greg. It worked perfectly.

When I look at the code and see the logic you applied it makes
absolute sense!

Hindsight=20/20 vision!

Roderick
 
G

Greg

Glad to help. Coming up with the logic is a first step. I often need
help writing the code myself :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top