View Single Post
 
Old 09-15-2020, 11:35 AM
Charles Kenyon Charles Kenyon is offline Windows 10 Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,140
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Quote:
Originally Posted by rcbjr2 View Post
Macropod,

I know this string is old, but I just found it, and I am trying to use your title case code. However, Word 2016 chocks on this line: StrPunct = "!,;,:,.,?,/,(,{,[,<,",""". Is there a different way to format it? In the forum, it looks like the next to last quote (inside the lasts comma) is a left curly quote. Could that be the issue? Anyway, I just copied and pasted your code into the VBA editor in Word and tried to run it and it stops on the StrPunct line (which I thought it might because it's higlighted in red). Thanks!

Here is what I'm running from Graham Mayor's vba examples:
Code:
Sub TrueTitleCase()  ' Graham Mayor
'   Graham Mayor vba examples page 2
'   Creates true title case - sort of
'   See http://www.gmayor.com/word_vba_examples_2.htm
'   2014-01-31 - tab formatting added to vba by ckk
'
    Dim sText As range, vFindText As Variant, vReplText As Variant, _
        i As Long, k As Long, m As Long
        '
    Set sText = Selection.range
    'count the characters in the selected string
    k = Len(sText)
    If k < 1 Then
        'If none, then no string is selected
        'so warn the user
        MsgBox "Select the text first!", vbOKOnly, "No text selected"
        Exit Sub 'and quit the macro
    End If
    'format the selected string as title case
    sText.Case = wdTitleWord
    'list the exceptions to look for in an array
    vFindText = Array("A", "An", "And", "As", "At", "But", "By", "For", _
    "If", "In", "Of", "On", "Or", "The", "To", "With")
    'list their replacements in a matching array
    vReplText = Array("a", "an", "and", "as", "at", "but", "by", "for", _
    "if", "in", "of", "on", "or", "the", "to", "with")
    With sText
        With .Find
            'replace items in the first list
            'with the corresponding items from the second
            .ClearFormatting
            .Replacement.ClearFormatting
            .Forward = True
            .Wrap = wdFindStop
            .MatchWholeWord = True
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Format = True
            .MatchCase = True
            For i = LBound(vFindText) To UBound(vFindText)
                .Text = vFindText(i)
                .Replacement.Text = vReplText(i)
                .Execute Replace:=wdReplaceAll
            Next i
        End With
        'Reduce the range of the selected text
        'to encompass only the first character
        .MoveEnd Unit:=wdCharacter, Count:=-Len(sText) + 1
        'format that character as upper case
        .Case = wdUpperCase
        'restore the selected text to its original length
        .MoveEnd Unit:=wdCharacter, Count:=k
        'and check to see if the string contains a colon
        If InStr(1, sText, ":") > 0 Then
            'If it does note the position of the character
            'after the first colon
            m = InStr(1, sText, ":") + 1
            'and set that as the new start of the selected text
            .MoveStart wdCharacter, m
            'set the end of the selected text to include
            'one extra character
            .MoveEnd Unit:=wdCharacter, Count:=-Len(sText) + 1
            'format that character as upper case
            .Case = wdUpperCase
        End If
    End With
  End Sub
When I run it on the text:
Save time in word with new buttons that show up where you need them.
It gives true title case including the word "you."
Save Time in Word with New Buttons That Show Up Where You Need Them.

I tried what I have from Paul's code, which does run fine on my computer, and it did not capitalize the "you." (You is in the exclusion list of the function.)
Save Time in Word with New Buttons That Show Up Where you Need Them.

The difference is in the exclusion lists, I expect. Paul's macro covers more things though.

Last edited by Charles Kenyon; 09-15-2020 at 04:14 PM.
Reply With Quote