How to set Range for individual sections of text of a given style

T

tllcvg

Hi,

Is there a way to set a Range for each section of a document that has a
given Style?

While I can easily set a Range that encompasses all text of a given style
and change the all of the text of that one style to a different style, I have
some additional textual changes that need to be made to each individual set
of lines of the initial style beyond simply changing the text style.

I have documents that will contain blocks of text of a defined Style (for
example, Style1). Each document will have a different number of blocks/lines
of this style and they will be scattered throughout the document in different
places for each document.

I want to have a macro that will accomplish the following:
1) change the style to another style (Style2). (Already can do this globally)
2) Change the text of the first line of each section of the first style to a
predefined text constant.
3) Remove the text from each contiguous line of text of that style while
leaving the same number of blank lines in the second style for each section.

If I could create Ranges for each section of Style1 text, it shouldn't be
hard to identify the number of lines in the section, replace the text in the
first line, and remove the text while leaving the line for the remaining
lines -- right?

Thank you, in advance, for your help with this.

Luke
 
J

Jean-Guy Marcil

tllcvg was telling us:
tllcvg nous racontait que :
Hi,

Is there a way to set a Range for each section of a document that has
a given Style?

While I can easily set a Range that encompasses all text of a given
style and change the all of the text of that one style to a different
style, I have some additional textual changes that need to be made to
each individual set of lines of the initial style beyond simply
changing the text style.

I have documents that will contain blocks of text of a defined Style
(for example, Style1). Each document will have a different number of
blocks/lines of this style and they will be scattered throughout the
document in different places for each document.

I want to have a macro that will accomplish the following:
1) change the style to another style (Style2). (Already can do this
globally) 2) Change the text of the first line of each section of the
first style to a predefined text constant.
3) Remove the text from each contiguous line of text of that style
while leaving the same number of blank lines in the second style for
each section.

If I could create Ranges for each section of Style1 text, it
shouldn't be hard to identify the number of lines in the section,
replace the text in the first line, and remove the text while leaving
the line for the remaining lines -- right?

Thank you, in advance, for your help with this.

Play around with this:

'_______________________________________
Dim rgeStyle As Range
Dim parCheck As Paragraphs
Dim rgeStart As Range
Dim lngDocParaCount As Long

Set parCheck = ActiveDocument.Paragraphs
Set rgeStart = Selection.Range
lngDocParaCount = parCheck.Count

Application.ScreenUpdating = False

For i = 1 To parCheck.Count
If parCheck(i).Range.Style = "Heading 1" Then
Set rgeStyle = parCheck(i).Range
If i < lngDocParaCount Then
Do While parCheck(i).Next.Range.Style = "Heading 1"
rgeStyle.End = parCheck(i + 1).Range.End
i = i + 1
If i = lngDocParaCount Then Exit Do
Loop
End If
With rgeStyle
.Style = "Heading 2"
.Select
Selection.Bookmarks("\Line").Range.Text = "New text" &
vbVerticalTab
End With
End If
Next

rgeStart.Select

Application.ScreenRefresh
Application.ScreenUpdating = True
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

Russ was telling us:
Russ nous racontait que :
Tllcvg,

Be careful of how you use the word 'section' in these newsgroups, as
it has a specific meaning in Word.
This macro will find contiguous text with the style you search for,
then you can alter what it finds.

Sub ManipulateStyleRange()
Dim aRange As Range
Dim aDoc As Document

Set aRange = ActiveDocument.Range(0, 0)
Set aDoc = ActiveDocument

With aRange.Find
.Style = aDoc.Styles("Style1")
While .Execute

aRange.Italic = True ' put your code here to alter aRange

aRange.Start = aRange.End ' this is needed to avoid endless
looping Wend
End With
End Sub

Except that in the case of paragraph styles, the Find only finds one
paragraph at the time. If, for example, 3 contiguous paragraphs are set tot
the same style ("Style1"), .Execute will go to each paragraph one after the
other in three passes through the Loop. The OP wanted to find all paragraphs
that were contiguous in one shot...

While I did not provide a full answer to all of his queries, I did get him
started, but I guess it wasn't important since we haven't heard from him!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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