View Single Post
 
Old 08-10-2016, 07:40 AM
gmaxey gmaxey is offline Windows 7 32bit Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

While I agree with Andrew, I would like to ask you a couple of questions about your code and offer some tips for looping through paragraphs when you need to.

Why did you declare numOfParagrpahs but not x1 or char_count?
Why did you use char_count in the first place? Isn't

If Len(Selection.Paragraphs(1).Range) < 50

the same as:

char_count = Len(Selection.Paragraphs(1).Range)
If char_count < 50 Then

In most case you don't really have to select things to do things with those things you selected.

In addition to Andrews For ... Each method, here are two alternate ways to loop through paragraphs.

Code:
Sub LoopThroughParagraphsI()
Dim lngIndex As Long
  For lngIndex = 1 To ActiveDocument.Range.Paragraphs.Count
    If Len(ActiveDocument.Range.Paragraphs(lngIndex).Range) < 50 Then
      ActiveDocument.Range.Paragraphs(lngIndex).Range.Font.Underline = wdUnderlineSingle
    End If
  Next
lbl_Exit:
  Exit Sub
End Sub
Sub LoopThroughParagraphsII()
'Typically faster.
Dim oPar As Paragraph
  Set oPar = ActiveDocument.Range.Paragraphs(1)
  Do
    If Len(oPar.Range.Text) < 50 Then oPar.Range.Font.Underline = wdUnderlineNone
    Set oPar = oPar.Next
  Loop Until oPar Is Nothing
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote