PDA

View Full Version : Formatting word before Backslash



sg225551
12-23-2007, 05:24 AM
hi

I have this document which contain

Person L Name 1 \ Some Stories blah blah blah
Person Short Name 2 \ Some Stories blah blah blah
Person Some Name 3 \ Some Stories blah blah blah
Person Demo Name 4\ Some Stories blah blah blah
Person Ten Name 5 \ Some Stories blah blah blah
Person K Name 6 \ Some Stories blah blah blah
Person AK Name 7 \ Some Stories blah blah blah

I have about 30,000 name to bold and I am thinking of using VBA to do it rather then CTRL B every single name.

How do i bold the person's name just before the back-slash (\) character using VBA? Thanks

sg225551
12-23-2007, 06:35 AM
hi

Sorry, just want to make it clear. My intention was to bold everything before the "\" character. Thanks

fumei
12-24-2007, 11:46 AM
Like this? Click the text "Test Bold" on the top toolbar.

Just for fun, I made it a toggle. Click it and it makes the text from the "\" bold. Click it again, it makes it unBold.

NOTE: this only works if the "lines" are terminated by a paragraph mark. That is, each one of your:

Person L Name 1 \ Some Stories blah blah blah
Person Short Name 2 \ Some Stories blah blah blah
...etc.

is an actual paragraph.

It they are not, then something else can be done.

Further, I made it to do literally what you asked. It makes evreything prior to the "\" bold. It does NOT make any distinction for the name itself, i.e. it includes "Person". You can get the code from the attached, but here it is for posting sake.Sub BoldTest()
Dim oPara As Paragraph
Dim r As Range
Dim j As Long
For Each oPara In ActiveDocument.Paragraphs
Set r = oPara.Range
With r.Find
.Text = "\"
.Execute
If .Found = True Then
j = r.Start - 1
With r
.SetRange Start:=oPara.Range.Start, _
End:=j
' this is the toggle part
If r.Characters(1).Bold = True Then
.Font.Bold = False
Else
.Font.Bold = True
End If
.Collapse Direction:=wdCollapseEnd
End With
End If
End With
Next
End Sub

TonyJollans
01-03-2008, 05:54 AM
If every line is a paragraph a simple Find & Replace should do the trick without the need for any code ...

Find paragraph mark * \
replace all bold

fumei
01-03-2008, 09:49 AM
Just answering the question.

"How do i bold the person's name just before the back-slash (\) character using VBA? "

But, yup. You do not need VBA to actually do it. A Find and Replace would work faster as well. This could be significant if you have 30,000!