Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word macro loop control 1

Status
Not open for further replies.

kaloger

Technical User
Dec 1, 2004
10
US
hi, i am trying to bold all the first words in a paragraph. this code works but how do i set up a loop so it will do for the whole file?

Sub bold_first_word()
'
' bold_first_word Macro
' Macro recorded 11/30/2004 by bill
'

Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Font.Bold = wdToggle
Selection.MoveDown Unit:=wdParagraph, Count:=1

End Sub
 
Sub bold_first_word()
'
' bold_first_word Macro
' Macro recorded 11/30/2004 by bill
'
dim lx as integer

Do Until lx > ActiveDocument.Sentences.Count

Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Font.Bold = wdToggle
Selection.MoveDown Unit:=wdParagraph, Count:=1

lx=lx+1

loop

End Sub



Do Until lx > ActiveDocument.Sentences.Count
 
Errm, ETID,

What about ActiveDocument[red.Paragraphs.[/red]Count? [smile]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
Hmm,

I thought I could skip previewing such a simple post - apparently not!!

What about ActiveDocument.[red]Paragraphs[/red].Count?

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
Oh...you're right Tony, I got too granular

 
yeah that is it. thank you! so how could i have found that using ms word help. the help has me lost. i have written lots of code in my life but the vba doesn't seem to have a manual. or at least one i can understand. i write most of my code from examples. (i have done a fair bit of excel stuff)
 
Oh, and B.T.W. Tony,

You wern't perhaps being condescending were you?

I.E.
I thought I could skip previewing such a simple post - apparently not!!

...Didn't think so :)



 
Code:
Sub BoldEachFirstWord()
Dim aPara As Paragraph
Dim aRange As Range
For Each aPara In ActiveDocument.Paragraphs
    Set aRange = aPara.Range
    aRange.Words(1).Bold = True
    Set aRange = Nothing
Next
End Sub

use range not selection object. make every first word bold.

also make stupid "blank" paragraphs (ones people use 4 spaces between paragraph) bold 2. may use logic - if aRange.Text = "", skip bolding. or not use "blank" spacing paragraphs....
 
ETID,

No, no, no ... LOL - I was just referring to my own screw up with the TGML [smile]

kaloger,

The Word object model takes a bit of learning. No amount of macro recording on its own is going to get you there by itself - and even finding what you want in Help isn't easy without a bit of trial and error. Try entering "Microsoft Word Objects" in the Help Answer Wizard and reviewing the object model.

Also take note of Phaed's post - he is correct in (a) addressing the Words collection and (b) being aware that some paragraphs may not have any words.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
hey thanks a lot. you all have been very helpful.

bill kaloger
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top