Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search to find word docs that contain green text

Status
Not open for further replies.

sxschech

Technical User
Jul 11, 2002
1,033
US
I'm trying to locate a file or files that were given to me a year or two ago, can't recall the name(s), I do remember they had some text that was colored green. Is there a search mechanism that would allow me to find such files? I looked around and saw examples for searching within a document, which doesn't help in this instance.
 
I can think of a way to try to do this with VBA, but not just a general "search by color". And to search with VBA for the colors, you're going to need to likely try to narrow down WHAT Green, since there are oodles of shades and such. If you have the exact color that'll be best. Then you can search by numbers for the color. Also, if you have a general idea as to where in the sheet that green text would have been - or at least some of it - then you can narrow down the search range and therefore speed up the process. And then also anything else you can remember - a guess at part of the file name - narrow in on the date range as well as possible for instance.

In VBA, if you want to go that route, you'll need the FileSystemObject (have to add a reference or else use late binding - I'd just set a reference to the Microsoft Scripting Runtime as that'll make coding tons easier.

If you want to try the VBA route, and need help with that route, I'd suggest posting your question(s) here:
forum707

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Hi,

In your doc, use search and replace to replace every SPACE character with a PARAGRAPH character.

Then COPY the entire text and PASTE into Excel. Turn on the AutoFilter. Assuming you have Excel 2007+ you can filter by a color located in in the filter.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
KJV: You mentioned in your post "sheet", just want to be sure we're talking about the same things as I'm looking for a word document (doc/docx) rather than excel. If the vba solution pertains to word, I'll post in the vba as you advised.
Making the assumption that the original author used one of the standard green colors, I went to the properties window and looks like a good starting point would be to try RGB 0,176,80. Let me know if I should post the question there based on your response.

Skip: Not sure how your solution would work since I'm trying to find a word doc/docx file that happens to contain text formatted with green color, in order to locate a file rather than searching within a known file.
 
If you want/need to go the VBA route, then it'll work whether Excel or VBA. I guess what you'd need to do is first test what Skip is talking about with Find/Replace, get that down pat. Then you can record a macro (in Word - sorry, I've got Excel on the brain [smile]) to record how to do the Find/Replace. From there, you can embed that in a loop or series of loops if necessary to go through all the possible Word Docs.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
...because you're trying to find "text formatted with green color".

This technique will accomplish that! It will take every "word" in the document and put it on a separate line in your document. When you PASTE the text from your document, into Excel 2007+, and turn on the AutoFilter, those hundreds or thousands of "words", each in a separate row, hundreds or thousands of rows, can be filtered by COLOR in the filter.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
This whole process will take you less than 10 minutes. For a one-time-job, that's a bargain!

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
I'm trying to find a word doc/docx file that happens to contain text formatted with green color, in order to locate a file rather than searching within a known file
Do you at least know the folder? And, as KJV observed, there's the question of which shade of green you want. Assuming you know the folder and the green is the standard one, you might try:
Code:
Sub ReportGreenDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document, StrRpt As String
strDocNm = ActiveDocument.FullName
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  If strFolder & "\" & strFile <> strDocNm Then
    Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, ReadOnly:=True, Visible:=False)
    With wdDoc
      With .Range.Find
        .ClearFormatting
        .Text = ""
        .Font.ColorIndex = wdGreen
        With .Replacement
          .ClearFormatting
          .Text = ""
        End With
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .Execute
        If .Found = True Then StrRpt = StrRpt & vbCr & wdDoc.Name
      End With
      .Close SaveChanges:=False
    End With
  End If
  strFile = Dir()
Wend
Set wdDoc = Nothing
If StrRpt <> "" Then
  MsgBox "The following files with green body text were found:" & vbCr & StrRpt
Else
  MsgBox "No files with green body text were found."
End If
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

Cheers
Paul Edstein
[MS MVP - Word]
 
Two bits of good news. I found the file via an excel file a coworker keeps that is an index of where the various word docs are saved which included enough information for me to locate it. I also found the basis for a solution using vba. I tested out the code and it does what the person posing the question on that site asked. Since I found what I needed, not going to modify the code at this point to do what I want. Later, if I have some free time maybe I'll do it... I imagine I would set up a loop to go through the folder directories grab a filename, run it through the code and instead of echoing each instance, since I only care that it has color rather than identifying specific color, have the debug.print return back the name of the file and then exit the loop since it does not need to find anymore color in the file.


(Search for WdColor)

Slightly modified code to run in vba, needs closing word code, etc.
Code:
Sub FindColorInWord()

Const wdColorRed = 255
Const wdColorGreen = 32768

Set objWord = CreateObject("Word.Application")

objWord.Visible = True

Set objdoc = objWord.Documents.Open("C:\temp\Test.docx")

Set objselection = objWord.Selection

objselection.Find.Forward = True

objselection.Find.Format = True

objselection.Find.Font.Color = wdColorGreen 'Red

Do While True

    objselection.Find.Execute

    If objselection.Find.Found Then

        Debug.Print objselection.Text 'Wscript.Echo objSelection.Text

    Else

        Exit Do

    End If

Loop
End Sub
 
macropod, guess I was writing while you submitted your code and since I hadn't refreshed my browser window did not see it yet. Should I delete my post as obviously looks like what you have is more complete. As to the folder question, I know the base folder, not which subfolders within it.
 
Looping through sub-folders would require additional code but, since you've found the file, there's no need to bother with that now...

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top