View Full Version : Solved: multiple string search
Im trying to search for few strings at a particular positions in my document and change font to bold if there is a match.
strings to search = abc, bcd, def, ddd, eee, fff, ggg, hhh, iii, jjj
for i = 1 to activedocument.count
if mid(activedocument.paragraphs(i), 59, 12) contains any of the test strings then change font of string to bold.
Thanks
Suji
fumei
04-14-2009, 10:36 AM
What is your question?
How do I search for the strings and change the font to bold. I know how to check for a single string using instr function but how can accomplish this when there are more than one string to search.
Thanks
Paul_Hossler
04-14-2009, 12:27 PM
Something like this -- I did not use the comma in your string
Sub aaaa()
Dim s As String
Dim v As Variant
Dim i As Long
s = "abc bcd def ddd eee fff ggg hhh iii jjj"
v = Split(s, " ")
For i = LBound(v) To UBound(v)
With ActiveDocument.Content
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Font.Bold = True
.Text = v(i)
.Replacement.Text = "^&"
.Wrap = wdFindContinue
.Format = True
.Execute Replace:=wdReplaceAll
End With
End With
Next i
End Sub
Other ways to organize, and make it more modular
Paul
fumei
04-14-2009, 01:31 PM
1. This syntax would fail:
activedocument.count
ActiveDocument does not have a .Count
2. Please clarify - precisely - your requirements. You seem (in your original post) to be trying to do something with paragraphs. Not just each .Found string.
Essentially though you would do as Paul suggests...use an array.
The question (or one of them) is: Are you simply trying to change ALL .Found instances of "abc" and make that string bold? This is what Paul's code does.
BTW: Paul, your build of the array is cute. I would have just explicitly set it.
Dim SearchStrings()
SearchStrings = Array("abc", "bcd", _
"def", "ddd", "eee", _
"fff", "ggg", "hhh", _
"iii", "jjj")
Your way is better I think. It is so nice that I am still learning stuff! I mean I use Split, but not like that. It gave me both an "Aha!" and a "Doh!" moment.
Thankyou Paul ... I have apapted your code as below since your code searches all document for the test strings and changes them to bold but my requirement was to search only characters 59 to 71 of every paragraphs.
Fumei ... You were right about the paragraphs . I made a typing mistake.
Now the problem with my code ... It changes the whole paragraph to bold but I would prefer that only characters 59 to 71 of the paragraph be changed to bold. Is that possible ?
Secondly is there any better way to accomplish this other than using the multiple for loops.
Thanks
Suji
Sub aaaa()
Dim s As String
Dim v As Variant
Dim i As Double
Dim j As Double
s = "abc bcd def ddd eee fff ggg hhh iii jjj"
v = Split(s, " ")
For j = 1 To ActiveDocument.Paragraphs.Count
For i = LBound(v) To UBound(v)
If InStr(Mid(ActiveDocument.Paragraphs(j), 59, 12), v(i)) <> 0 Then
ActiveDocument.Paragraphs(j).Range.Font.Bold = True
End If
Next i
Next j
End Sub
fumei
04-17-2009, 12:40 PM
"It changes the whole paragraph to bold but I would prefer that only characters 59 to 71 of the paragraph be changed to bold. Is that possible ?"
Yes.
It would really really help when you post questions to try and be as explicit as possible. Your logic is:
1. I have a list of search strings ("abc", "ddd".....)
2. I want to search each paragraph at a specific character count for each of the strings.
3. If ANY of the strings of the list are found at that location - and ONLY that location - make the paragraph Bold.
To be clear, if "abc" is in a paragraph, but NOT in the location, do nothing.
If "abc" is in a paragraph AT THE LOCATION, make the paragraph bold.
The following does this.Option Explicit
Sub SpecificBold()
Dim oPara As Paragraph
Dim s As String
Dim v As Variant
Dim j As Long
s = "abc bcd def ddd eee fff ggg hhh iii jjj"
v = Split(s, " ")
For Each oPara In ActiveDocument.Paragraphs
For j = LBound(v) To UBound(v)
' no point of Bolding if already bold
If oPara.Range.Font.Bold = True Then Exit For
If InStr(Mid(oPara, 59, 12), v(j)) <> 0 Then
oPara.Range.Font.Bold = True
' may as well exit because if "abc" (or "ddd" etc.)
' made it bold...why bother checking the others?
Exit For
End If
Next j
Next oPara
End Sub
Demo attached. Click "Specific Bold" on the top toolbar.
Before you click though, notice there are three paragraphs. The second one DOES have strings from the list. In fact, it has three strings from the list. "ggg", "ddd", and "iii". However, they are NOT within the location parameters...therefore - by your logic - the paragraph is NOT bolded.
fumei
04-17-2009, 12:49 PM
darn...I should take my own suggestions. I forgot that you want JUST the characters 59 to 71 bolded.
BTW: this is an extremely odd thing to do, IMO.
Hang on, I do not see this as being too difficult.
OK, here you go. If the search string is found within 59 to 71, then 59 - 71 (and ONLY 59 - 71) is bolded.
fumei
04-17-2009, 12:55 PM
For those who are lazy (just kidding), here is the amended code to only action against characters 59 - 71. It - of course - uses a Range object to do the work.
Option Explicit
Sub SpecificBold()
Dim oPara As Paragraph
Dim s As String
Dim v As Variant
Dim j As Long
Dim r As Range
Dim rStart As Long
s = "abc bcd def ddd eee fff ggg hhh iii jjj"
v = Split(s, " ")
For Each oPara In ActiveDocument.Paragraphs
For j = LBound(v) To UBound(v)
If oPara.Range.Font.Bold = True Then Exit For
If InStr(Mid(oPara, 59, 12), v(j)) <> 0 Then
rStart = oPara.Range.Start
Set r = ActiveDocument.Range( _
Start:=rStart + 59, _
End:=rStart + 71)
r.Font.Bold = True
Exit For
End If
Next j
Next oPara
End Sub
fumei
04-17-2009, 12:57 PM
Hmmmm. it does not need that test against the whole paragraph anymore....
Option Explicit
Sub SpecificBold()
Dim oPara As Paragraph
Dim s As String
Dim v As Variant
Dim j As Long
Dim r As Range
Dim rStart As Long
s = "abc bcd def ddd eee fff ggg hhh iii jjj"
v = Split(s, " ")
For Each oPara In ActiveDocument.Paragraphs
For j = LBound(v) To UBound(v)
If InStr(Mid(oPara, 59, 12), v(j)) <> 0 Then
rStart = oPara.Range.Start
Set r = ActiveDocument.Range( _
Start:=rStart + 59, _
End:=rStart + 71)
r.Font.Bold = True
Exit For
End If
Next j
Next oPara
End Sub
Thanks Fumei..
Thats exactly what I was looking for. Now I learned little bit about range object.
fumei
04-22-2009, 10:41 AM
I am curious. WHY is there a hard-coded requirement for such specific characters?
It seems odd to me that if a paragraph has "ddd" in ONLY that range (59 - 71), do something. If it has "ddd" anywhere else, do nothing.
Seems very strange.
Range 59-71 is a column in a system generated file that I have to work on.
fumei
04-23-2009, 12:21 PM
A column??????
Yikes. This is even more strange as the code you are working with here has no connection to columns. These are characters in a specified range in ONE paragraph.
Bizarre. Oh well, as long as it is working somehow for you.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.