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
Malcolm Smith - 11 Dec 2006 09:00 GMT
> *From:* toadflax@australia.edu
> *Date:* 10 Dec 2006 21:39:31 -0800
[quoted text clipped - 19 lines]
> 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
Greg Maxey - 11 Dec 2006 14:33 GMT
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.
> > *From:* toadflax@australia.edu
> > *Date:* 10 Dec 2006 21:39:31 -0800
[quoted text clipped - 76 lines]
>
> End Sub
Malcolm Smith - 11 Dec 2006 15:27 GMT
> *From:* "Greg Maxey" <gmaxey@mvps.org>
> *Date:* 11 Dec 2006 06:33:53 -0800
[quoted text clipped - 5 lines]
> 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
Greg Maxey - 11 Dec 2006 16:13 GMT
Malc,
Copy all ;-)
> > *From:* "Greg Maxey" <gmaxey@mvps.org>
> > *Date:* 11 Dec 2006 06:33:53 -0800
[quoted text clipped - 14 lines]
>
> - Malc
Greg Maxey - 11 Dec 2006 13:25 GMT
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
> Hi,
>
[quoted text clipped - 16 lines]
> Cheers,
> Michelle
Greg Maxey - 11 Dec 2006 20:40 GMT
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
> Hi,
>
[quoted text clipped - 16 lines]
> Cheers,
> Michelle
toadflax@australia.edu - 11 Dec 2006 22:07 GMT
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