Select certain words in specified style and unbold/unitalic

T

toadflax

Hi,

I am trying to write a macro in Word 2002 to do this stuff for me on a
large list, but I've tried recording them without success. It seems to
do it to all pragraphs, not just those of a given style. What I need to
do is:
(1) search for paragraphs in a given style, select all *except* the
first word and make it not bold, not italic. I still want to be able to
search for the style--should I avoid selecting the paragraph return or
does it not matter?
(2) search for paragraphs in a given style, select all *except* the
first TWO words and make it not bold and not italic (I actually need to
do this for two different styles, but figure it's really only one
macro.) I also need to be able to search for the style afterward.

Can anyone help? I'd really appreciate it, as it would save a lot of
laborious effort!


Cheers,
Michelle
 
M

Malcolm Smith

*From:* (e-mail address removed)
*Date:* 10 Dec 2006 21:39:31 -0800

Hi,

I am trying to write a macro in Word 2002 to do this stuff for me on a
large list, but I've tried recording them without success. It seems to
do it to all pragraphs, not just those of a given style. What I need to
do is:
(1) search for paragraphs in a given style, select all *except* the
first word and make it not bold, not italic. I still want to be able to
search for the style--should I avoid selecting the paragraph return or
does it not matter?
(2) search for paragraphs in a given style, select all *except* the
first TWO words and make it not bold and not italic (I actually need to
do this for two different styles, but figure it's really only one
macro.) I also need to be able to search for the style afterward.

Can anyone help? I'd really appreciate it, as it would save a lot of
laborious effort!


Cheers,
Michelle


Michelle,

This is a quick hack. It may need improving but it seems to work. You
will need to add error handling to it for when it goes wrong. But
basically it goes through each Paragraph object and checks to see if it's
the correct style and then has enough words to change and, if so, changes
it.

And then returns to cursor to where it was.

Hope that this helps

- Malc
www.dragondrop.com



Sub Test1()

ConvertParagraphs "Normal", 1

End Sub



Sub test2()

ConvertParagraphs "Data", 2

End Sub


Private Sub ConvertParagraphs(sStyle As String, nOmitNumberOfFirstWords As
Long)

Dim oPara As Paragraph
Dim nWords As Long

Dim oSelection As Range

' Remember the selection poit
Set oSelection = Selection.Range


For Each oPara In ActiveDocument.Paragraphs
If oPara.Style = sStyle Then
nWords = oPara.Range.Words.Count
If nWords > nOmitNumberOfFirstWords Then
oPara.Range.Words(nOmitNumberOfFirstWords + 1).Select
Selection.MoveDown wdParagraph, 1, wdExtend

Selection.Range.Font.Bold = False
Selection.Range.Font.Italic = False

End If
End If
Next oPara

' Return the selection point
oSelection.Select

End Sub
 
G

Greg Maxey

It might be a tad faster to work with a range rather than a selection.

Sub ScratchMacro
Dim oRng As Word.Range
Set oRng = ActiveDocument.Content
oRng.Collapse wdCollapseStart
Do
With oRng
.Expand wdParagraph
Select Case .Style
Case Is = "Style1"
.MoveStart wdWord, 1
.MoveEnd wdCharacter, -1
.Font.Bold = False
.Font.Italic = False
Case Is = "Style2"
.MoveStart wdWord, 2
.MoveEnd wdCharacter, -1
.Font.Bold = False
.Font.Italic = False
Case Else
'Do nothing
End Select
.Move wdParagraph, 1
End With
Loop Until oRng.End = ActiveDocument.Range.End - 1
End Sub
 
G

Greg Maxey

Malc,

I did a few simple comparison using about 30, 300 snf 3000 short
paragraphs of text. The range method is a bit faster and the delta
grows as the number of paragraphs increases. This could be due more to
the avoiding For Each/Next than a range/selection efficiency.
 
M

Malcolm Smith

*From:* "Greg Maxey" <[email protected]>
*Date:* 11 Dec 2006 06:33:53 -0800

Malc,

I did a few simple comparison using about 30, 300 snf 3000 short
paragraphs of text. The range method is a bit faster and the delta
grows as the number of paragraphs increases. This could be due more to
the avoiding For Each/Next than a range/selection efficiency.


I agree it would be. However, in my defence m'lud, this was a quick hack
as stated in my message and it may have been good enough for the task in
hand.

I would have done it by Ranges but I have enough stuff of my own to deal
with today and so I was something short of time.

- Malc
 
G

Greg Maxey

Malc,

Copy all ;-)


Malcolm said:
I agree it would be. However, in my defence m'lud, this was a quick hack
as stated in my message and it may have been good enough for the task in
hand.

I would have done it by Ranges but I have enough stuff of my own to deal
with today and so I was something short of time.

- Malc
 
G

Greg Maxey

My first answer assumed that the first two words of each list item
where not punctuation marks. As Word counts punctuation marks as a
"word" then the following may be more accurate for your purposes:

Sub ScratchMacro()
Dim oRng As Word.Range
Dim i As Long
Set oRng = ActiveDocument.Content
oRng.Collapse wdCollapseStart
Do
With oRng
.Expand wdParagraph
Select Case .Style
Case Is = "Style1"
i = 0
Do Until i = 1
If oRng.Characters(1) Like "[A-z]" Then
i = i + 1
.MoveStart wdWord, 1
Else
.MoveStart wdWord, 1
End If
Loop
.MoveEnd wdCharacter, -1
.Font.Bold = False
.Font.Italic = False
Case Is = "Style2"
i = 0
Do Until i = 2
If oRng.Characters(1) Like "[A-z]" Then
i = i + 1
.MoveStart wdWord, 1
Else
.MoveStart wdWord, 1
End If
Loop
.MoveEnd wdCharacter, -1
.Font.Bold = False
.Font.Italic = False
Case Else
'Do nothing
End Select
.Move wdParagraph, 1
End With
Loop Until oRng.End = ActiveDocument.Range.End - 1
End Sub
 
T

toadflax

Thanks very much to everyone who replied--that seems to have done the
trick and I'll only have to do minor fiddling to get the format
perfect. Of course, if Word would just not mess up my styles the world
would be a better place, but that's a gripe for another forum.

Thanks again for saving me a lot of pain.


Cheers,
Michelle
 

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