Ehtisham Ali
Member
Hi guys,
I have a requirement and code as well but it requires some modification.
I have more than 500 text files containing nearly same text, and a keyword/s which I need to search in each file and export it into a excel cell. Need to parse all files one by one and search for the word.
I am attaching the sample text file for your reference.
Keyword1: System image file is "flash:c3845-advipservicesk9-mz.124-15.T3.bin"
Keyword2: Processor board ID FHK1205F2ZX
in combination.
Would be great if we can export "System image file" in colA and respective "Processor board ID" in colB. And where ever system image file or board ID not found row remains blank.
My Solution:
I have compiled all 500 files into one single file and pasted the text in a word document, then run the below mentioned MACRO.
Limitation of this solution is that we can search for 1 keyword at a time, so later we get puzzled which System Image is related to which Board ID.
Please suggest a better solution as this is not helping.
I have a requirement and code as well but it requires some modification.
I have more than 500 text files containing nearly same text, and a keyword/s which I need to search in each file and export it into a excel cell. Need to parse all files one by one and search for the word.
I am attaching the sample text file for your reference.
Keyword1: System image file is "flash:c3845-advipservicesk9-mz.124-15.T3.bin"
Keyword2: Processor board ID FHK1205F2ZX
in combination.
Would be great if we can export "System image file" in colA and respective "Processor board ID" in colB. And where ever system image file or board ID not found row remains blank.
My Solution:
I have compiled all 500 files into one single file and pasted the text in a word document, then run the below mentioned MACRO.
Limitation of this solution is that we can search for 1 keyword at a time, so later we get puzzled which System Image is related to which Board ID.
Code:
Option Explicit
Sub FindWordCopySentence()
Dim appExcel As Object
Dim objSheet As Object
Dim aRange As Range
Dim intRowCount As Integer
intRowCount = 1
Set aRange = ActiveDocument.Range
With aRange.Find
Do
.Text = "shall" ' the word I am looking for
.Execute
If .Found Then
aRange.Expand Unit:=wdSentence
aRange.Copy
aRange.Collapse wdCollapseEnd
If objSheet Is Nothing Then
Set appExcel = CreateObject("Excel.Application")
'Change the file path to match the location of your test.xls
Set objSheet = appExcel.workbooks.Open("C:\temp\test.xls").Sheets("Sheet1")
intRowCount = 1
End If
objSheet.Cells(intRowCount, 1).Select
objSheet.Paste
intRowCount = intRowCount + 1
End If
Loop While .Found
End With
If Not objSheet Is Nothing Then
appExcel.workbooks(1).Close True
appExcel.Quit
Set objSheet = Nothing
Set appExcel = Nothing
End If
Set aRange = Nothing
End Sub
Please suggest a better solution as this is not helping.
Attachments
Last edited: