VBA equivalent of StyleRef

J

John Marshall, MVP

Is there a VBA equivalent of StyleRef?

I am searching through a document for a specific style and once found I am
adding the text in that style and page number to a table. I would also like
to add the content of the previous heading1 to the table.

The current method is to use StylRef in the header and then once the
specific style is found, open the header and then "read" the value placed by
the Styleref.

John... Visio MVP
 
J

Jay Freedman

Hi John,

Use a Find to search for the style. This sample will look for the
specific heading style you put in the .Style parameter. Looking for
the previous Heading 1 and getting it into the table without
duplications is going to be a lot more complicated.

Sub demo()
Dim oRg As Range
Dim summaryTbl As Table
Dim rowNum As Long

' Select an existing table like this, or
' add a new table and assign it to
' the summaryTbl object. Assume it has
' 2 columns and starts with only a header row.
Set summaryTbl = ActiveDocument.Tables(1)
rowNum = 2

Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Text = ""
.Format = True
.Style = ActiveDocument.Styles("your style")
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = False
Do While .Execute
' At this point, oRg contains the text
' of one occurrence of the style.
With summaryTbl
.Rows.Add
.Cell(rowNum, 1).Range.Text = oRg.Text
.Cell(rowNum, 2).Range.Text = _
oRg.Information(wdActiveEndAdjustedPageNumber)
rowNum = rowNum + 1
End With
Loop
End With
End Sub
 
J

John Marshall, MVP

Thanks Jay.
This is code I inherited and the main routine is similar to yours. The code
involved for getting the Heading1, opens the header, does some manipulation
to get the Heading1 information placed by StyleRef. I was trying to see if
there was an easy way to avoid playing with the header.

So I'll do a search for the previous Heading1.

John... Visio MVP
 
R

Russ

John,
Working off of Jay's code I added a search within search for preceding
Heading 1 style. I commented out the table stuff to test it.

Sub demo()
Dim oRg As Range
Dim oRg2 As Range
Dim summaryTbl As Table
Dim rowNum As Long

' Select an existing table like this, or
' add a new table and assign it to
' the summaryTbl object. Assume it has
' 2 columns and starts with only a header row.
'Set summaryTbl = ActiveDocument.Tables(1)
'rowNum = 2

Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Text = ""
.Format = True
.Style = ActiveDocument.Styles("Strong") 'Whatever
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = False
Do While .Execute
oRg.Select
' At this point, oRg contains the text
' of one occurrence of the style.
Set oRg2 = ActiveDocument.Range(0, oRg.Start)
'With summaryTbl
' .Rows.Add
' .Cell(rowNum, 1).Range.Text = oRg.Text
' .Cell(rowNum, 2).Range.Text = _
' oRg.Information(wdActiveEndAdjustedPageNumber)
' rowNum = rowNum + 1
' End With
With oRg2.Find
.Style = ActiveDocument.Styles("Heading 1")
.Format = True
.Forward = False
If .Execute Then
oRg2.Select
Else
MsgBox "Did not find a preceding Heading 1 style."
End If
End With
Loop
End With
End Sub
 
R

Russ

John,
Also, using a StyleRef in a header only searches within the scope of the
current page and not any preceding page.

See this page and expand the links for StyleRef field locations:
 
J

John Marshall, MVP

Thanks for the update.

John... Visio MVP

Russ said:
John,
Also, using a StyleRef in a header only searches within the scope of the
current page and not any preceding page.

See this page and expand the links for StyleRef field locations:
 
J

John Marshall, MVP

Actually, that information is wrong.

I've been using StyleRef for years and it does remember the content of a
style several pages back. It is quite common to use a StyleRef to place the
chapter heading in a header along with a StyleRef of a section header for
the chapter.

John... Visio MVP
 
J

John Marshall, MVP

You have to expand some of the text on the page to find:
"Word searches the current page, from top to bottom, for the specified
style. If the style isn't found, Word searches next from the top of the page
to the beginning of the document, and then from the bottom of the page to
the end of the document."

John... Visio MVP
 
R

Russ

John,
You're right of course. I had forgotten what I read before starting to
rewrite Jay's macro and was thinking that would be a good reason to do a
search rather than use the StyleRef. My Bad. :(
Glad you clarified it.
 
J

John Marshall, MVP

The article is poorly worded. It gives the impression that it is limited to
the current page.

John... Visio MVP
 

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