PDA

View Full Version : Solved: Add colon in front of each bold word



tamtam
10-17-2007, 03:22 AM
Hello,

Is it possible to write a VBA script that searches for words formatted with the style "bold" and then to add something in front of those words?

The problem is that I have a document where I have many words formatted bold and in front of each word I want to add a colon.

Thanks,
tom

OTWarrior
10-17-2007, 04:13 AM
how about :

Sub BoldWords()
Dim oRange As Word.Range
Dim oWord As Object
Dim i As Integer
Dim count As Integer
count = 0
Set oRange = ActiveDocument.Range
For Each oWord In oRange.Words
count = count + 1
If count < 20 Then
If oWord.Font.Bold = True Then
oWord = "?" & oWord & "?"
End If
Else
Exit Sub
End If
Next
End Sub



NB, not tested

tamtam
10-17-2007, 05:10 AM
Thanks OTWarrior for your quick reply!

I tried it and it works, but as the inserted colon is bold as well the script puts another colon in front of the first colon and so on. Would it work if the inserted character wouldn't be bold and how would I do this?

Another question: How does it work if I want to add sth in front of words formatted with "Heading 1" instead of bold?

Thanks a lot
tom

Paul_Hossler
10-17-2007, 06:08 AM
Needs some polishing, but the macro recorder gave me this.

If it's a one-time thing, Edit-Replace can do it quicker than creating a macro


Sub Macro1()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ":^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub


Paul

OTWarrior
10-17-2007, 07:28 AM
this is the code for turning bold on or off

Selection.Font.Bold = wdToggle

I tested my above code, and it does just do the same word again and again.

this updated code is nearly there, but it puts 2 ?? at the end instead of just one, and makes everything not bold

Sub BoldWords()
Dim oRange As Word.Range
Dim oWord As Object
Dim i As Integer
Dim count As Integer
count = 0
Set oRange = ActiveDocument.Range
For Each oWord In oRange.Words
count = count + 1
If count < 100 Then
If oWord.Font.Bold = True Then
oWord.Font.Bold = False
oWord = "?" & oWord & "?"
End If
Else
Exit Sub
End If
Next
End Sub

not quite what you need, but with some tweaking should be what you are after

the following is the text before the code, which i made all the words bold to test on:


?this ??is ??the ??code ??for ??turning ??bold ??on ??or ??off??
??
??Selection?.?Font?.?Bold ?= ?wdToggle??
??
??I ??tested ??my ??above ??code?, ?and ??it ??does ??just ??do ??the ??same ??word ??again ??and ??again?.?
??
??this ??updated ??code ??is ??nearly ??there?, ?but ??it ??puts ??2 ??? ?at ??the ??end ??instead ??of ??just ??one?, ?and ??makes ??everything ??not ??bold??

tamtam
10-17-2007, 08:27 AM
Hello OTWarrior,

great work!! Thanks to Paul as well.

Thats the code I needed; I just removed the count

Sub BoldWords()
Dim oRange As Word.Range
Dim oWord As Object
Dim i As Integer
Set oRange = ActiveDocument.Range
For Each oWord In oRange.Words
If oWord.Font.Bold = True Then
oWord.Font.Bold = False
oWord = ":" & oWord
End If
Next
End Sub

What is the right syntax to do this to Heading 1 text as well? oWord.Font.Heading1 seems too easy.

Thanks again,
tom

fumei
10-17-2007, 08:39 AM
Dim r As Range
Dim aWord
Set r = ActiveDocument.Range
For Each aWord In r.Words
If aWord.Font.Bold = True Then
If aWord <> r.Words(1) Then
If aWord.Previous <> ":" Then
aWord.InsertBefore Text:=":"
End If
End If
End If
NextThis will correctly place a ":" before each bolded word, with the ":" NOT bold. It works except for the very first word in the document. If it is bold, the : will be bold.

You can run it multiple times. It checks to see if there is a ":" before the word, if there is, it will NOT add another one.

NOTE 1: you mention "formatted with the style "bold"". You do really mean a Style, a proper Style? If so, they you should search for the Style, rather than a Font.Bold = True

NOTE 2: if they are manually formatted bold, and there are multiple words in a row, ir depends on what you did.

quick brown fox

If you selected the whole thing (quick brown fox) and bolded it, then sorry, that bolding will include the spaces, and the code above will put a bolded ":" before each word. This is because Word considers the whole range (from quick to fox) as having bold attribute.

If you made EACH word bold, then the code WILL work.

In other words, if "quick brown fox" was selected as a group, and bolded, then the : before each word is bold.

If "quick brown fox" were independently bold, then the ":" will not be bold.

fumei
10-17-2007, 08:44 AM
Sorry, I missed the last post. Your code turns the bold OFF, then adds the ":".

Is this what you want?

What is the right syntax to do this to Heading 1 text as well? oWord.Font.Heading1 seems too easy.Heading 1 is not a font, it is a style.

I am not sure what it is exactly you are trying to do. A style is applied to a paragraph, not a word - unless it is a character style.

Heading 1 is a paragraph style. So if you are searching for Heading 1, then:Dim oPara As Paragraph
For Each oPara In ActiveDocument.Paragraphs
If oPara.Style = "Heading 1" Then
' do your own stuff
MsgBox oPara.Range.Text
End If
Next

tamtam
10-17-2007, 09:26 AM
Hello fumei,

your solution is even better. Also thanks for the tip with "Heading 1". Does this also apply to my own styles? i.e.

Dim oPara As Paragraph
For Each oPara In ActiveDocument.Paragraphs
If oPara.Style = "My own Style" Then
' do your own stuff
MsgBox oPara.Range.Text
End If
Next

fumei
10-18-2007, 04:23 AM
It applies for styles. A style is a style no matter who made it.