PDA

View Full Version : [SOLVED:] How do I search for a string and change the style and format of that line?



Helen269
01-26-2018, 09:27 AM
Hi, I wonder if anyone can help me?

I need to be able to search for the string ":." (colon dot) in a Word doc and if a line contains those characters together then apply bold and Name styling to that line without changing the text. How would I do this, please?

Thanks

macropod
01-26-2018, 06:15 PM
Depending on what you mean by a 'line', you could adapt the code in http://www.vbaexpress.com/forum/showthread.php?61836-Cycle-through-sentences-help for that.

Helen269
01-26-2018, 08:06 PM
Thank you for your reply but I wasn't able to tweak that code and make it work for me. Maybe I should explain better the effect I'm after. I need to turn this:

FirstNameA LastNameA:.
What they say.
FirstNameB LastNameB:.
What they say.
FirstNameC LastNameC:.
What they say.

into:

FirstNameA LastNameA:.What they say.
FirstNameB LastNameB:.What they say.
FirstNameC LastNameC:.What they say.

...but with the names in Name style as well. Does that help?

macropod
01-26-2018, 08:20 PM
The required code is almost identical to that in post #9 of the other thread:

Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^13[!^13^l^t]@:.[^13^l]"
.Replacement.Text = ""
.Forward = True
.Format = False
.Wrap = wdFindStop
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
i = i + 1
.Start = .Start + 1
.Font.Bold = True
.Characters.Last.Text = " "
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
MsgBox i & " terms processed."
End Sub

Helen269
01-26-2018, 09:03 PM
Thank you, that works but it's still not quite there. it's cutting the paragraph mark off after the :. so the names and speech run together and are on the same line. I've tried playing around with the ^p marker but I just get an error.

Also I need the names to be in Name style. I've tried putting in .Style = ActiveDocument.Styles("Name") and variations thereof but it still errors out. It must be something simply like that but I can't find the right syntax. Help, please?

Helen269
01-26-2018, 09:17 PM
.Characters.Last.Text = " "
was what was causing two lines to run together. I randomly commented out that line and it worked!

Now I just need to know how to apply the Name style and I'm happy.

macropod
01-26-2018, 09:26 PM
Thank you, that works but it's still not quite there. it's cutting the paragraph mark off after the :. so the names and speech run together and are on the same line.
I coded it that way because that's how you presented the desired outcome. Have a look at post #3!


Now I just need to know how to apply the Name style and I'm happy.
In that case, delete:

[^13^l]
and change:
.Font.Bold = True
.Characters.Last.Text = " "
to:
.Style = "Name"

Helen269
01-26-2018, 09:40 PM
I coded it that way because that's how you presented the desired outcome. Have a look at post #3!
Oops! My fault for not proof reading my reply! Sorry. :-)




In that case, delete:

[^13^l]
and change:
.Font.Bold = True
.Characters.Last.Text = " "
to:
Style = "Name"

Now it doesn't do anything. :-(

macropod
01-26-2018, 10:26 PM
That should be:
.Style = "Name"
I take it you do have a Style of that name...

Helen269
01-26-2018, 10:57 PM
Yes, that worked. Thank you! The problem was the missing dot which I probably played with putting in BUT...
I was trying it on a blank document instead of the company template I should have used that had a style called "Name" supplied. I do apologise, I thought "Name" was a built-in style that's found in Word by default. Now that I'm trying it on the template it works like a charm.

Anyway, thank you so much for your time and help, it was very much appreciated.