View Single Post
 
Old 09-19-2015, 07:41 PM
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

Marrick,

I love it when I get credit for Graham's work

The reason for your continuous loop is because you can't collapse the selection to the right side of the end of document mark. That means if the last paragraph has Heading 1 applied you are constantly processing the last paragraph (end of document mark).

Here is my version:

Code:
Sub Greg_Apply_True_Title_Case_by_Style()
Dim oPar As Paragraph
Dim oRng As Range
Dim arrFind() As String
Dim lngIndex As Long
  arrFind = Split("A,An,And,As,At,But,By,For,If,In,Of,On,Or,The,To,With", ",")
  For Each oPar In ActiveDocument.Paragraphs
    If oPar.Style = "Heading 1" Then
      Set oRng = oPar.Range
      oRng.Case = wdTitleWord
      With oRng.Find
        For lngIndex = 0 To UBound(arrFind)
          .Text = arrFind(lngIndex)
          .Replacement.Text = LCase(.Text)
          .MatchWholeWord = True
          .Execute MatchCase:=True, Replace:=wdReplaceAll
        Next lngIndex
      End With
    End If
  Next oPar
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote