G
Gregory K. Maxey
The other day someone asked me how to link 4 content controls such that the
text in one would be lower case, one upper case, one title case and one
sentence case.
I couldn't figure out how to do this using the normal XML mapping method so
I thought I would try using the ContentControlOnExit event.
I entered the four controls and assigned each one the title "Test", I tagged
the controls "LC" "UC" "TC" and "SC." I tried using the following code and
all worked except for the Case Is SC part.
Private Sub Document_ContentControlOnExit(ByVal ContentControl As
ContentControl, Cancel As Boolean)
Dim oCC1 As ContentControl
Dim pStr As String
pStr = ContentControl.Range.Text
For Each oCC1 In ActiveDocument.ContentControls
If oCC1.Title = "Test" Then
Select Case oCC1.Tag
Case Is = "LC"
oCC1.Range.Text = LCase(pStr)
Case Is = "UC"
oCC1.Range.Text = UCase(pStr)
Case Is = "TC"
oCC1.Range.Text = pStr
oCC1.Range.Case = wdTitleWord
Case Is = "SC"
oCC1.Range.Text = pStr
oCC1.Range.Case = wdTitleSentence
End Select
End If
Next
End Sub
For some reason the text of a ContentControl can not be set to senctence
case using VBA. I confirmed this by selecting the text in a CC and running
the following code:
Sub Test()
Selection.Range.ContentControls(1).Range.Case = wdUpperSentence
End Sub
As a work around, I added a bookmark at the end of the document. I put the
CC text in the bookmark, formatted it, and then put the formatted text back
in CC as shown below. Does anyone know why the text can't be formatted
directly as in the other cases?
Thanks.
Private Sub Document_ContentControlOnExit(ByVal ContentControl As
ContentControl, Cancel As Boolean)
Dim oCC1 As ContentControl
Dim oBMs As Bookmarks
Set oBMs = ActiveDocument.Bookmarks
Dim pStr As String
Dim oRng As Word.Range
pStr = ContentControl.Range.Text
For Each oCC1 In ActiveDocument.ContentControls
If oCC1.Title = "Test" Then
Select Case oCC1.Tag
Case Is = "LC"
oCC1.Range.Text = LCase(pStr)
Case Is = "UC"
oCC1.Range.Text = UCase(pStr)
Case Is = "TC"
oCC1.Range.Text = pStr
oCC1.Range.Case = wdTitleWord
Case Is = "SC"
Set oRng = oBMs("ScratchPad").Range
oRng.Text = pStr
oBMs.Add "ScratchPad", oRng
oBMs("ScratchPad").Range.Case = wdTitleSentence
pStr = oBMs("ScratchPad").Range.Text
oCC1.Range.Text = pStr
oRng.Text = ""
oBMs.Add "ScratchPad", oRng
End Select
End If
Next
End Sub
text in one would be lower case, one upper case, one title case and one
sentence case.
I couldn't figure out how to do this using the normal XML mapping method so
I thought I would try using the ContentControlOnExit event.
I entered the four controls and assigned each one the title "Test", I tagged
the controls "LC" "UC" "TC" and "SC." I tried using the following code and
all worked except for the Case Is SC part.
Private Sub Document_ContentControlOnExit(ByVal ContentControl As
ContentControl, Cancel As Boolean)
Dim oCC1 As ContentControl
Dim pStr As String
pStr = ContentControl.Range.Text
For Each oCC1 In ActiveDocument.ContentControls
If oCC1.Title = "Test" Then
Select Case oCC1.Tag
Case Is = "LC"
oCC1.Range.Text = LCase(pStr)
Case Is = "UC"
oCC1.Range.Text = UCase(pStr)
Case Is = "TC"
oCC1.Range.Text = pStr
oCC1.Range.Case = wdTitleWord
Case Is = "SC"
oCC1.Range.Text = pStr
oCC1.Range.Case = wdTitleSentence
End Select
End If
Next
End Sub
For some reason the text of a ContentControl can not be set to senctence
case using VBA. I confirmed this by selecting the text in a CC and running
the following code:
Sub Test()
Selection.Range.ContentControls(1).Range.Case = wdUpperSentence
End Sub
As a work around, I added a bookmark at the end of the document. I put the
CC text in the bookmark, formatted it, and then put the formatted text back
in CC as shown below. Does anyone know why the text can't be formatted
directly as in the other cases?
Thanks.
Private Sub Document_ContentControlOnExit(ByVal ContentControl As
ContentControl, Cancel As Boolean)
Dim oCC1 As ContentControl
Dim oBMs As Bookmarks
Set oBMs = ActiveDocument.Bookmarks
Dim pStr As String
Dim oRng As Word.Range
pStr = ContentControl.Range.Text
For Each oCC1 In ActiveDocument.ContentControls
If oCC1.Title = "Test" Then
Select Case oCC1.Tag
Case Is = "LC"
oCC1.Range.Text = LCase(pStr)
Case Is = "UC"
oCC1.Range.Text = UCase(pStr)
Case Is = "TC"
oCC1.Range.Text = pStr
oCC1.Range.Case = wdTitleWord
Case Is = "SC"
Set oRng = oBMs("ScratchPad").Range
oRng.Text = pStr
oBMs.Add "ScratchPad", oRng
oBMs("ScratchPad").Range.Case = wdTitleSentence
pStr = oBMs("ScratchPad").Range.Text
oCC1.Range.Text = pStr
oRng.Text = ""
oBMs.Add "ScratchPad", oRng
End Select
End If
Next
End Sub