search for file with text

S

sals

Hi,

I'm trying to go into a folder to look for files with the words
"summary" and "report" in their file names.

I would like to then insert the summary file before the report file.

Would any one give pointers or have sample codes?

thanks,

S
 
H

Helmut Weber

Hi,
have a look at this one.
It inserts the files at the docs end.
It is assumed, that there is only one file
with "summary" in it's name. The same applies
to "report". It is further assumed, that these
words do not appear in the file path.
---
Sub Test555()
Dim i As Integer
Selection.WholeStory
Selection.Collapse direction:=wdCollapseEnd
With Application.FileSearch
.NewSearch
.LookIn = "c:\test"
.FileName = "*.doc"
.Execute
For i = 1 To .FoundFiles.Count
If InStr(.FoundFiles(i), "summary") Then
Selection.InsertFile FileName:=.FoundFiles(i)
End If
Next
For i = 1 To .FoundFiles.Count
If InStr(.FoundFiles(i), "report") Then
Selection.InsertFile FileName:=.FoundFiles(i)
End If
Next
End With
End Sub
 
S

sals

Thanks very much Helmut.

Which part of the code tells the program to insert the rest of the
files in the folder after the summary and report files are inserted?

Thanks,

S

________
 
H

Helmut Weber

Hi,
Which part of the code tells the program to insert the
rest of the files in the folder after the summary and
report files are inserted?
None, as you didn't ask for that feature.
As a result, I'd say, the code doesn't look superb,
but should do, what you wanted.
Sub Test555()
Dim i As Integer
Dim s As String ' a filename including path
Selection.WholeStory
Selection.Collapse direction:=wdCollapseEnd
With Application.FileSearch
.NewSearch
.LookIn = "c:\test"
.FileName = "*.doc"
.Execute
For i = 1 To .FoundFiles.Count
If InStr(.FoundFiles(i), "summary") Then
Selection.InsertFile FileName:=.FoundFiles(i)
End If
Next
For i = 1 To .FoundFiles.Count
If InStr(.FoundFiles(i), "report") Then
Selection.InsertFile FileName:=.FoundFiles(i)
End If
Next
For i = 1 To .FoundFiles.Count
s = .FoundFiles(i)
If InStr(s, "summary") = 0 _
And InStr(s, "report") = 0 _
And InStr(s, "~") = 0 Then
Debug.Print .FoundFiles(i) ' for testing
'Selection.InsertFile FileName:=.FoundFiles(i)
' remove ' for inserting
' InStr(s, "~") is there to avoid trying
' to insert temporary files, which may cause
' problems
End If
Next
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top