I'd like to make first letter on each sentence bold in a MS Word document. What would be a good way to accomplish this?
+3
A:
This can be done with Word's built in advanced find+replace. You would need to specify a wildcard matching expression such as this one to select the first character following a sentence delimiter and space:
[\.\?\!] ?
You can specify how each character found is styled in the same UI (it is not strictly find/replace - you can find/style). Note that the expression above will make both the first character of each sentence and the preceding ?/!/. bold. You can correct this by doing another search for just the punctuation marks and un-bolding them.
See this guide: http://www.gmayor.com/replace_using_wildcards.htm
Not very programatic, I know, but much faster than delving into VBA.
tehblanx
2010-06-22 19:50:23
+4
A:
Pretty straight-forward in VBA
Sub BoldFirstLetterInSentence()
Dim ad As Document
Set ad = ActiveDocument
Dim sen As Range
For Each sen In ad.Sentences
sen.Words.First.Characters.First.Font.Bold = True
/* sen.Words(1).Characters(1).Font.Bold = True also works */
Next
End Sub
Otaku
2010-06-22 20:43:12
Would you mind telling me, why this code doesn't work, when I have `option exlicit` turned on?
froeschli
2010-09-20 20:07:03
It's because `sen` in the `For Each` loop is not defined and `Option Explicit` requires all variables to be defined. It is a `Range` object, so you would just need to add `Dim sen As Range` below the `Set ad...`. I've updated the code to reflect that.
Otaku
2010-09-20 20:34:37
+1
A:
The following works for me
Option Explicit
Public Sub SetFirstLetterBold()
Dim i As Integer
Dim doc As Document
Set doc = ActiveDocument
For i = 1 To doc.Sentences.Count
doc.Sentences(i).Characters(1).Bold = True
Next
End Sub
froeschli
2010-09-17 12:50:16