darkhangelsk
New Member
- Joined
- Feb 10, 2013
- Messages
- 27
Hi,
i've been trying to create a macro that will look to all sub folders. sub folders have word documents and notepads, but i'll just be looking into notepad files. within the notepad, it will be looking for a certain word. Then if the word found in the notepad, it will populate the subfolder names and full path in the excel.
i have sample but something wrong into it:
i've been trying to create a macro that will look to all sub folders. sub folders have word documents and notepads, but i'll just be looking into notepad files. within the notepad, it will be looking for a certain word. Then if the word found in the notepad, it will populate the subfolder names and full path in the excel.
i have sample but something wrong into it:
Code:
Sub FindText()
Dim FileName As String
Dim FolderPath As String
Dim FSO As Object
Dim I As Integer
Dim SearchForWords As Variant
Dim Text As String
Dim TextFile As Object
'Change these arrays to word you want to find
SearchForWords = Array("Note", "noTe", "notE")
'Change the folder path to where your text files are.
FolderPath = "C:\Project\"
Set FSO = CreateObject("Scripting.FileSystemObject")
FolderPath = IIf(Right(FolderPath, 1) <> "\", FolderPath & "\", FolderPath)
FileName = Dir(FolderPath & "\*.txt")
Do While FileName <> ""
Filespec = FolderPath & FileName
Set TextFile = FSO.OpenTextFile(Filespec, 1, False)
Text = TextFile.ReadAll
TextFile.Close
Set TextFile = FSO.OpenTextFile(Filespec, 2, False)
For I = 0 To UBound(SearchForWords)
With ActiveSheet
Range("A" & Range("A" & Rows.Count).End(xlUp).Row + 1) = I
I = I + 1
End With
Next I
TextFile.Write Text
TextFile.Close
FileName = Dir()
Loop
End Sub