Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-28-2024, 12:18 PM
gmaxey gmaxey is offline Capitalization macro doesn't work for hyphenated words or words after paretheses Windows 10 Capitalization macro doesn't work for hyphenated words or words after paretheses Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,601
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

I could be missing something, but your code seems to be doing a lot of extraneous work.


Would this do?


Code:
Sub CapitalizeHeading()
Dim arrExcludedWords() As String
Dim colExclude As New Collection
Dim lngCount As Long, lngPar As Long, lngIndex As Long
Dim oRng As Range
Dim oPar As Paragraph
Dim bTC As Boolean
  Application.ScreenUpdating = False
  'List of word to be excluded from capitalization
  arrExcludedWords = Split("a, above, across, against, along, among, an, and, around, as, at, because, before, behind, below, beneath, beside, between, beyond, but, by, down, during, for, from, in, into, nor, of, off, on, onto, or, over, per, since, so, the, through, to, toward, under, unless, with, within, without, yet", ", ")
  'Add words to collection.  Makes it more efficient to check if each word is excluded.
  For lngIndex = 0 To UBound(arrExcludedWords)
    colExclude.Add arrExcludedWords(lngIndex), arrExcludedWords(lngIndex)
  Next lngIndex
  'Store user's track changes status
  bTC = ActiveDocument.TrackRevisions
  'Turn on tracked changes
  ActiveDocument.TrackRevisions = True
  lngCount = ActiveDocument.Paragraphs.Count
  For lngPar = 1 To lngCount
    Set oPar = ActiveDocument.Paragraphs(lngPar)
    oPar.Range.MoveEnd unit:=wdCharacter, Count:=-1 ' Exclude the paragraph mark
    Application.StatusBar = "Processing paragraph " & lngPar & " of " & lngCount
    Select Case oPar.Style
      Case "Heading 1", "Heading 2", "Heading 3", "Report Title", "Report Subtitle", "Figure/Table Title"
        For lngIndex = 1 To oPar.Range.Words.Count
          Set oRng = oPar.Range.Words(lngIndex)
          If lngIndex = 1 Then
            If oRng.Characters(1).Text Like "[a-z]" Then
              oRng.Characters(1).Text = UCase(oRng.Characters(1).Text)
            End If
          Else
            If oRng.Words.Last.Next = "-" Then
              oRng.MoveEnd wdWord, 1
              lngIndex = lngIndex + 1
            End If
            On Error Resume Next
            colExclude.Add Trim(oRng.Text), Trim(oRng.Text)
            If Err.Number = 0 Then
              colExclude.Remove (colExclude.Count)
              If oRng.Characters(1).Text Like "[a-z]" Then
                oRng.Characters(1) = UCase(oRng.Characters(1))
              End If
            Else
              Err.Clear
              'Skip it is an excluded word.
            End If
          End If
        Next
    End Select
  Next lngPar
  ActiveDocument.TrackRevisions = bTC
  Application.StatusBar = False
  Application.ScreenUpdating = True
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Tags
capitalization, hyphenation

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace any UK Words to US words in the entire document laith93 Word VBA 4 12-25-2023 01:56 AM
Capitalization macro doesn't work for hyphenated words or words after paretheses Macro to insert certain words if the number of words than 20 laith93 Word VBA 6 10-28-2022 01:12 AM
How to find (highlight) two and more words in a list of 75k single words in Word 2010 Usora Word 8 05-29-2018 03:34 AM
Capitalization macro doesn't work for hyphenated words or words after paretheses How to mark underlined words in a sentence as A, B, C, D (beneath the words) thudangky Word 13 12-12-2013 02:22 AM
Why Words doesn’t show the style of the selected words automatically???? Jamal NUMAN Word 0 04-14-2011 03:20 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:52 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft