Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Apr 2015
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

    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

    ActiveDocument.Fields.Update
    Call FixCase

    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()

    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
    arrNoCaps(1) = "And"
    arrNoCaps(2) = "Or"

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

    Exit Sub

    errHandler:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Next

    End Sub

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts
    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
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. The Following User Says Thank You to Andrew Lockton For This Useful Post:

    tedlaw (2015-04-24)

  4. #3
    WS Lounge VIP
    Join Date
    Mar 2006
    Location
    Maryland, USA
    Posts
    690
    Thanks
    17
    Thanked 66 Times in 56 Posts
    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.
    Pam Caswell

  5. #4
    New Lounger
    Join Date
    Apr 2015
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •