PDA

View Full Version : [SOLVED:] Getting character after / to capitalize



HTSCF Fareha
08-05-2021, 09:20 AM
I've been scratching my head trying to get the following to detect any '/' that appears as a second character, then to capitalize the character immediately after it, which will be at position three.

If I use this as an example and capitalize the third character if it finds a 'c' at position two, then it works okay.


If oCtrl.Name = "txtVictim" Then
strTC = strConv(oCtrl.Text, vbProperCase)
Set oCC = ActiveDocument.SelectContentControlsByTag("Victim").Item(1)
oCC.Range.Text = strTC
For lngIndex = 1 To oCC.Range.Words.Count
If oCC.Range.Words(lngIndex).Characters(2) = "c" Then
oCC.Range.Words(lngIndex).Characters(3) = UCase(oCC.Range.Words(lngIndex).Characters(3))
End If
Next

If I change from 'c' to a '/' or char (47), then I get a Runtime error '5941' - The requested member of the collection does not exist on this line.


If oCC.Range.Words(lngIndex).Characters(2) = "/" Then

gmayor
08-05-2021, 11:22 PM
Because of the way 'words' are defined "/" is a word in its own right so cannot have two characters, hence the error.
If you want to capitalise after "/" then you will need to do it another way e.g.


Set oWrd = oCC.Range
With oWrd.Find
Do While .Execute(findText:="/")
If oWrd.InRange(oCC.Range) Then
oWrd.Next.Characters(1).Case = wdUpperCase
End If
oWrd.Collapse 0
Loop
End With

HTSCF Fareha
08-06-2021, 11:06 AM
Thank you, Graham.

I would never have figured on a "/" being a word!

Is this the same for other characters such as "-", "#" and "&" etc.?

gmayor
08-06-2021, 08:45 PM
If in doubt use the following macro to list the words in the selected string in the immediate window


Sub Words()
Dim i As Integer
For i = 1 To Selection.Words.Count
Debug.Print Selection.Words(i)
Next i
End Sub

HTSCF Fareha
08-06-2021, 10:46 PM
Lovely, thanks again Graham.