• Any Way to Avoid Caps on Words in Fields That Shouldn’t Have Them?

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Any Way to Avoid Caps on Words in Fields That Shouldn’t Have Them?

    Author
    Topic
    #499611

    I’m trying to set up a document template using REF fields. There are places where I want the field to have what I think of as “Title Case”: i.e., the first letter of each word is generally capitalized, excepting certain common words like “and” and “the” and “to” and “in.” The *CAPS switch doesn’t quite work, because it capitalizes everything. There doesn’t seem to be any way to filter out the exceptions.

    A good example of where this comes up is for a dollar amount spelled out in words. Imagine bookmark Price has a value of $349.95. { REF Price *DollarText *Caps } produces the result Three Hundred Forty Nine And 95/00. That capital A in And is really bothering me.

    The only way I thought of to address this is to keep the *CAPS switch, update the fields, and then find the exceptions and reformat them in lower case. So I wrote an AutoNew subroutine that says
    [INDENT]
    ActiveDocument.Fields.Update
    Call FixCase
    [/INDENT]

    Then I wrote a FixCase subroutine by cobbling together the following VBA code from bits and pieces of things I found on the forum. After several hours, I managed to get it to stop throwing errors, which I count as a victory. But it doesn’t seem to do anything — And is still capitalized after I run it. Any ideas what I’m doing wrong? Or is this whole approach hopeless?

    (In an ideal world, I’d refine this so it could distinguish between sentence-opening “The” and “The” elsewhere, and change only the latter to lower case. But at this point I’ll settle for anything that works at all.)

    Here’s the code:

    Sub FixCase()
    [INDENT]
    [/INDENT]
    On Error GoTo errHandler

    Dim objStoryRng As Range
    Dim objFld As Field
    Dim NumWords As Long
    Dim objOutput As Range
    Dim i As Integer
    Dim j As Integer
    Dim arrNoCaps(1 To 2) As String
    Dim strTemp As String

    ‘Array of terms that should not be capitalized[INDENT] arrNoCaps(1) = “And”
    arrNoCaps(2) = “Or”

    [/INDENT]
    For Each objStoryRng In ActiveDocument.StoryRanges[INDENT] For Each objFld In objStoryRng.Fields[/INDENT]
    [INDENT=2] If objFld.Type = wdFieldRef Then[/INDENT]
    [INDENT=3] NumWords = objFld.Result.Words.Count[/INDENT]
    [INDENT=3] For i = 1 To NumWords[/INDENT]
    [INDENT=4] For j = 1 To 2[/INDENT]
    [INDENT=5] If objFld.Result.Words.Item(i).Text = arrNoCaps(j) Then[/INDENT]
    [INDENT=6] strTemp = objFld.Result.Words.Item(i).Text[/INDENT]
    [INDENT=6] strTemp = LCase(strTemp)[/INDENT]
    [INDENT=6] objFld.Result.Words.Item(i).Text = strTemp[/INDENT]
    [INDENT=5] End If[/INDENT]
    [INDENT=4] Next j[/INDENT]
    [INDENT=3] Next i[/INDENT]
    [INDENT=2] End If[/INDENT]
    [INDENT] Next objFld[/INDENT]
    Next objStoryRng

    Exit Sub

    errHandler:[INDENT] MsgBox “Error ” & Err.Number & “: ” & Err.Description[/INDENT]
    [INDENT] Resume Next[/INDENT]

    End Sub

    Viewing 1 reply thread
    Author
    Replies
    • #1501079

      This seems like an academic exercise rather than a real life project but this is what I would do

      Code:
      Sub FixCase()
        On Error GoTo errHandler
        
        Dim objStoryRng As Range
        Dim objFld As Field
        Dim strResult As String
      
        For Each objStoryRng In ActiveDocument.StoryRanges
          For Each objFld In objStoryRng.Fields
            If objFld.Type = wdFieldRef Then
              strResult = objFld.Result
              strResult = Replace(strResult, "And", "and")
              strResult = Replace(strResult, "Or", "or")
              objFld.Result.Text = strResult
              objFld.Locked = True
            End If
          Next objFld
        Next objStoryRng
      
        Exit Sub
      
      errHandler:
        MsgBox "Error " & Err.Number & ": " & Err.Description
        Resume Next
      
      End Sub
      
    • #1501203

      Take a look at the code on this Editorium site. I use the version of this macro that comes with Editorium’s Editor’s Toolkit. As Jack writes, you may want to omit some or the longer prepositions.

      • #1501594

        Thanks, Andrew, that worked perfectly. (I regret to say that this is, in fact, a real project, not an academic exercise; I just get hung up on details like that sometimes.)

        Thanks for the link, Pam; there’s some interesting stuff there. I went with Andrew’s suggestion because it seemed more elegant to use the object properties rather than a Selection procedure. But I’ll keep Editorium in mind as a reference.

    Viewing 1 reply thread
    Reply To: Any Way to Avoid Caps on Words in Fields That Shouldn’t Have Them?

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: