#1
|
|||
|
|||
Capitalize the first letter in a content control
I'm using Word 2010. I would like the first letter of my rich text content controls to be capitalized. Is anyone aware of a macro that will do that or any other way? Margaret |
#2
|
|||
|
|||
You could likely write a macro to do that. If you have AutoCorrect set to capitalize the first letter in a sentence and there are multiple words it should capitalize the first word assuming it is not already in a sentence.
|
#3
|
||||
|
||||
You could use a ContentControlOnExit macro like:
Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean) With CCtrl Select Case .Type Case wdContentControlText, wdContentControlRichText, wdContentControlComboBox .Range.Characters.First = UCase(.Range.Characters.First) Case Else End Select End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
|||
|
|||
Paul,
Thanks for the code. I understand how to execute an "on exit" macro for legacy fields but how do you implement one for content controls? I don't see it under properties. Margaret |
#5
|
||||
|
||||
Simply open the VBE and add the code to your document's 'ThisDocument' code module.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
Thanks so much. Works great!
|
#7
|
|||
|
|||
Actually, had error pop up if I exited the field without entering information. Are you aware of any code to ignore it if no text is entered.
|
#8
|
|||
|
|||
Code:
Case wdContentControlText, wdContentControlRichText, wdContentControlComboBox On Error Resume Next .Range.Characters.First = UCase(.Range.Characters.First) |
#9
|
|||
|
|||
Perfect!!!
|
#10
|
|||
|
|||
Some people may feel an error statement like that is indiscriminate use of brute force.
Another way to handle that issue is check to ensure the CC is not showing placeholder text. While it probably doesn't matter in your case, if you had a shape or something anchored in the a rich text control that code my delete the shape. You could also try: Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean) With CCtrl Select Case .Type Case wdContentControlText, wdContentControlRichText, wdContentControlComboBox If Not CCtrl.ShowingPlaceholderText Then .Range.Case = wdTitleSentence End If Case Else End Select End With End Sub |
#11
|
|||
|
|||
Thanks Greg. The first code with the error handling worked perfectly. I'm not really understanding the purpose of the last code. Does it affect other content controls besides the rich text control?
|
#12
|
|||
|
|||
The reason you got the error was because you didn't enter anything into the CC (it was showing placeholder text) so there was no range.characters.first to format.
Error handling can be used to get around that, but another method is to evaluate if the condition exists that caused the error exists and if it does, don't perform the action that causes the error. |
#13
|
|||
|
|||
Thanks for the explanation.
Margaret |
#14
|
|||
|
|||
gmaxey,
The below code you provided has worked perfectly with all of my testinig. The form I'm developing contains rich text content controls. You have to enter a shift+enter for a carriage return for a new paragraph. Is there any code that we can add to this, or separate code, that will assure the first letter of a new paragraph after two shift+enters will automatically be capitalized? Margaret Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean) With CCtrl Select Case .Type Case wdContentControlText, wdContentControlRichText, wdContentControlComboBox On Error Resume Next .Range.Characters.First = UCase(.Range.Characters.First) Case Else End Select End With End Sub |
#15
|
||||
|
||||
It is poor practice to use a pair of soft returns instead of a single return. The coding for each (real) paragraph would be much easier too.
Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean) Dim aPara As Paragraph With CCtrl Select Case .Type Case wdContentControlText, wdContentControlRichText, wdContentControlComboBox On Error Resume Next For Each aPara In .Range.Paragraphs aPara.Range.Characters.First = UCase(aPara.Range.Characters.First) Next aPara Case Else End Select End With End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro to capitalize first letter after timecode + tab | muiy | Word VBA | 1 | 06-17-2015 11:03 PM |
Deleting a table from a content control -- preserving the content control | BrainSlugs83 | Word Tables | 8 | 11-14-2013 03:06 AM |
Only Capitalize First Letter & Delete Last Name | zulhfreelancer | Excel | 5 | 12-10-2012 07:51 AM |
Exceptions for 'Capitalize first letter of sentences' | gazpacho | Word | 1 | 01-12-2012 11:43 AM |
capitalize first letter of sentences | norco1 | Word | 0 | 06-25-2006 12:37 PM |