Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tllcvg - 27 Sep 2006 16:46 GMT
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
Jean-Guy Marcil - 27 Sep 2006 20:22 GMT
tllcvg was telling us:
tllcvg nous racontait que :

> Hi,
>
[quoted text clipped - 26 lines]
>
> 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
'_______________________________________

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Russ - 15 Oct 2006 05:08 GMT
Tllcvg,

> tllcvg was telling us:
> tllcvg nous racontait que :
[quoted text clipped - 3 lines]
>> Is there a way to set a Range for each section of a document that has
>> a given Style?
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


>> 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
[quoted text clipped - 60 lines]
> Application.ScreenUpdating = True
> '_______________________________________

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Jean-Guy Marcil - 15 Oct 2006 06:15 GMT
Russ was telling us:
Russ nous racontait que :

> Tllcvg,
>
[quoted text clipped - 27 lines]
> 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!

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Russ - 15 Oct 2006 08:51 GMT
Jean-Guy,
You're right I didn't use aRange.select to test my sub. It just appeared to
do it all at once. :(
I only had in the back of my mind that your sub was iterating through
paragraphs and what if the OP didn't mean whole paragraphs?, then the find
would be faster and offer another solution. His post skirts around and
doesn't use the word 'paragraph'; with phrases like "section","lines", and
"body of text". But I guess the majority of time, when someone uses a style,
it is a paragraph style, encompassing all lines in the paragraph.

> Russ was telling us:
> Russ nous racontait que :
[quoted text clipped - 39 lines]
> 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!

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Russ - 15 Oct 2006 22:09 GMT
Tllcvg,

The revised subroutine below now appears to select contiguous style whether
it is a character style or paragraph style.
VBA Help says that when reading the style of an range like a word or
sentence, the match will only be with the first style used in that range and
it doesn't return a position of where that first style is used. So there was
no quick way to tell if the whole word or whole sentence was that style.
Therefore I wasn't able to use words and sentences to speed up the
subroutine.

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

Set aRange = ActiveDocument.Range(0, 0)
Set aDoc = ActiveDocument
myStyle = "Style1"

With aRange.Find
   .Style = aDoc.Styles(myStyle)
   While .Execute
       While aRange.Paragraphs.Last.Next.Style = aDoc.Styles(myStyle)
           aRange.MoveEnd unit:=wdParagraph
       Wend
       While aRange.Characters.Last.Next.Style = aDoc.Styles(myStyle)
           aRange.MoveEnd unit:=wdCharacter
       Wend
       
       aRange.Select ' put your code here to alter aRange
       
       aRange.Start = aRange.End 'needed to avoid endless looping
   Wend
End With
End Sub

> Jean-Guy,
> You're right I didn't use aRange.select to test my sub. It just appeared to
[quoted text clipped - 49 lines]
>> 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!

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID


Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.