View Full Version : search for words & phrases
trueblue
08-06-2012, 01:26 PM
i want to use a macro to search for words & phrases in any document.
i would like to link each selection above to highlight in the colours red,yellow,green and blue.
any help appreciated regards tony
fumei
08-06-2012, 10:00 PM
What do you mean "link"?
macropod
08-06-2012, 10:57 PM
Hi Tony,
Try the following macro - it includes its own browser. You add it to the document that contains your Find strings. Note the comments about how your document needs to be laid out. The code also provides for more highlighting options than you specified.
Sub BulkDocumentUpdate()
'This macro uses a tab-delimited list of Find strings and highlight colour names text list in this document
'for updating multiple other documents. The Find/Highlight strings are assumed to be formatted as:
'Find text <Tab> highlight colour name <Paragraph>.
'Note: This document must not have extraneous paragraph breaks.
Application.ScreenUpdating = False
Dim FHList As String, j As Long, HghLt As Long, Rng As Range
Dim strFolder As String, strFile As String, wdDoc As Document
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
FHList = ThisDocument.Range.Text
While strFile <> ""
Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
With wdDoc
'Parse the Find/Highlight List
For j = 0 To UBound(Split(FHList, vbCr)) - 1
Set Rng = .Range
With Rng
With .Find
.ClearFormatting
.MatchWholeWord = True
.MatchCase = True
.Wrap = wdFindStop
With .Replacement
.ClearFormatting
.Text = ""
.Highlight = True
End With
'Get the 'Find' text.
.Text = Split(Split(FHList, vbCr)(j), vbTab)(0)
'Get & set the highlight colour
Select Case LCase(Trim(Split(Split(FHList, vbCr)(j), vbTab)(1)))
Case "bright green": HghLt = wdBrightGreen
Case "dark red": HghLt = wdDarkRed
Case "dark yellow": HghLt = wdDarkYellow
Case "dark blue": HghLt = wdDarkBlue
Case "red": HghLt = wdRed
Case "yellow": HghLt = wdYellow
Case "green": HghLt = wdGreen
Case "blue": HghLt = wdBlue
Case "teal": HghLt = wdTeal
Case "turquoise": HghLt = wdTurquoise
Case "pink": HghLt = wdPink
Case "violet": HghLt = wdViolet
Case Else: HghLt = wdNoHighlight
End Select
.Execute
End With
'Highlight the found words
Do While .Find.Found
.Duplicate.HighlightColorIndex = HghLt
.Collapse Direction:=wdCollapseEnd
.Find.Execute
Loop
End With
Next
.Close SaveChanges:=True
End With
strFile = Dir()
Wend
Set wdDoc = Nothing: Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
trueblue
08-07-2012, 07:28 AM
hi guys thanks for the prompt response.
sorry about any confusion ( reference to the link ).
i want to be able to enter the words or phrases in the code and select the
colour i choose to highlight in any document.
regards tony
macropod
08-07-2012, 03:21 PM
So have to tried the solution I provided - which matches your specifications (such as they are)?
fumei
08-07-2012, 04:47 PM
Must admit I am confused as to what you want to do.
It sounds like you want to be able to input a search word, and ALSO input what colour you want the found word to be. (if found).
Paul, I am also not sure what you are doing with the listing of multiple documents. That is not how I interpreted the OP request, although of course I could be way off-base.
Tony could you please clearly describe what you want?
macropod
08-07-2012, 05:10 PM
Hi gerry,
I took "search for words & phrases in any document" as a reference to multiple documents, where any = all.
The solution I posted allows Tony to store a list of terms and their preferred highlight colours in one document, then process all documents in a given folder, so that all documents in that folder get those terms highlighted.
macropod
08-07-2012, 08:04 PM
Cross-posted at: http://answers.microsoft.com/en-us/office/forum/office_2003-customize/search-for-words-phrases/dd80a10f-b7dc-4c4b-b4a6-08d5ba6dd64d
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
fumei
08-08-2012, 12:48 AM
Sure seems to be a lot of cross-postings lately.
I did understand what you answer was doing, I was (am?) not sure if that was what is desired. Although again I could be off on Mars and you nailed it.
macropod
08-08-2012, 01:03 AM
Sure seems to be a lot of cross-postings lately.
Indeed. Including after answers have already been given. I don't mind the cross-posting, per se, but the lack of links and aknowledgement of help already given. What most of the cross-posters seem to disregard is the unnecessary work they could be creating for people who voluteer their time. I'm sure if they had to pay they wouldn't ask for help from multiple sources - especially without letting each know about what the others might be doing.
I did understand what you answer was doing, I was (am?) not sure if that was what is desired.
Nor am I. Unfortunately, we've had a run of poorly-specified problems of late too (and some of these have been cross-posted)!!
trueblue
08-08-2012, 12:17 PM
hi guys thanks to everybody who have contributed to this thread.
sometimes it is difficult to get a across in words in what you are trying to do.
reference fumei post no 6, his explanation of what i am trying to do is correct.
example :-
RED > challenging,difficult,unpredictable.
YELLOW > in line with expectations,cash.
GREEN > exceeding expectations, positive.
BLUE > covernants,contract for difference.
i will be using those words, phrases in the example i have used above.
but i would like to be able to add other words and phrases to a colour if required
in the future i would also like to be able to add other words, phrases to a different colour.
i would like to search each word doc individually but all the documents added will be kept in a folder for reference.
reference macropod post no 3
i ran the macro in a document but got a Compile error :
Only comments may appear after end sub, end function, or end property
stepped through the program and it highlighted at :-
Function GetFolder() As String
i am not sure what i did wrong ?.
hope my explanation is a little bit clearer, thanks again for everybody's contribution regards tony
macropod
08-08-2012, 02:55 PM
If you got a compile error, that suggests you didn't copy all of the code I posted and/or you made some changes afterwards.
FWIW, the data in the document for which the macro is coded should be laid out as:
quick<tab>red
brown<tab>yellow
fox<tab>blue
jumps<tab>green
over<tab>yellow
lazy<tab>red
dog<tab>black
where '<tab>' is an actual tab character. The expressions before the tab can be a single word or a phrase.
fumei
08-08-2012, 06:19 PM
reference fumei post no 6, his explanation of what i am trying to do is correct.
example :-
RED > challenging,difficult,unpredictable.
YELLOW > in line with expectations,cash.
GREEN > exceeding expectations, positive.
BLUE > covernants,contract for difference.Still not completely clear.
So. You want some sort of...ummmm...storage of parameters for a search. With the ability to input a search word or phrase. As in:
IF I select the word "positive" to search for, if "positive' is found make that found "positive" green.
IF I select the word "difficult" to search for, if "difficult" is found make that found "difficult" red.
If this is what you are trying to do....say so. Oh, and if is, hopefully someone (macropod?) can help you come up with something. I am going to be gone for a while.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.