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