prefect
01-13-2016, 01:49 AM
Hello,
User of MS Office 2010 here. I am currently working on an assignment where I have multiple text files and need to extract key information onto a separate file.
I have identified the necessary keywords in files by using a macro which highlights keywords in yellow in all word files within the folder.
So now I have many documents with key information highlighted in yellow in the same folder, and I want to extract those highlighted words onto a separate file.
I have a working code for extracting yellow-highlighted words from one open document onto a newly-created word file, but I can't get it to extract yellow-highlighted words from multiple documents (from same folder) onto the same newly created file.
Here is the code that works for extracting yellow-highlighted words from one document onto a separate file.
```
Sub ExtractHighlight()
Dim objWord As Word.Application
Dim doc As Word.Document
Dim oneword
Dim i As Integer
i = 0
'Create Word doc object
Set objWord = CreateObject("Word.Application")
With objWord
' Ensure the MS Word object is visible
.Visible = True
' Add a new word document and save the file prior to adding text
Set doc = .Documents.Add
doc.SaveAs "C:\test\output.docx"
' Or open an existing document
'Set doc = wrdApp.Documents.Open("C:\Foldername\Filename.doc")
End With
'Construct document
With objWord.Selection
' Set the font type
.Font.Name = "Trebuchet MS"
' Set the font size
.Font.Size = 16
' Set the format, depending on the value of i
' Add text
For Each char In ActiveDocument.Characters
If char.HighlightColorIndex = wdYellow Then
i = 1
oneword = char.Text
'oneword = oneword & "##"
.TypeText oneword
oneword = ""
Else
If i = 1 Then
.TypeText vbCrLf
i = 0
End If
End If
Next
End With
' Save the file
doc.Save
' Bring the MS Word window to the front
doc.Activate
End Sub
```
Can I change it to search multiple documents (i.e. the entire folder) and export yellow-highlighted words from them into one file? Alternatively, should I open a new empty document and then run a macro on it instead of running a macro on the doc with text?
In short, here's what I need:
1) identify yellow-highlighted words/text in all docs within a selected folder
2)export yellow-highlighted words to a separate file
Any help is much appreciated,
Thanks,
Prefect
User of MS Office 2010 here. I am currently working on an assignment where I have multiple text files and need to extract key information onto a separate file.
I have identified the necessary keywords in files by using a macro which highlights keywords in yellow in all word files within the folder.
So now I have many documents with key information highlighted in yellow in the same folder, and I want to extract those highlighted words onto a separate file.
I have a working code for extracting yellow-highlighted words from one open document onto a newly-created word file, but I can't get it to extract yellow-highlighted words from multiple documents (from same folder) onto the same newly created file.
Here is the code that works for extracting yellow-highlighted words from one document onto a separate file.
```
Sub ExtractHighlight()
Dim objWord As Word.Application
Dim doc As Word.Document
Dim oneword
Dim i As Integer
i = 0
'Create Word doc object
Set objWord = CreateObject("Word.Application")
With objWord
' Ensure the MS Word object is visible
.Visible = True
' Add a new word document and save the file prior to adding text
Set doc = .Documents.Add
doc.SaveAs "C:\test\output.docx"
' Or open an existing document
'Set doc = wrdApp.Documents.Open("C:\Foldername\Filename.doc")
End With
'Construct document
With objWord.Selection
' Set the font type
.Font.Name = "Trebuchet MS"
' Set the font size
.Font.Size = 16
' Set the format, depending on the value of i
' Add text
For Each char In ActiveDocument.Characters
If char.HighlightColorIndex = wdYellow Then
i = 1
oneword = char.Text
'oneword = oneword & "##"
.TypeText oneword
oneword = ""
Else
If i = 1 Then
.TypeText vbCrLf
i = 0
End If
End If
Next
End With
' Save the file
doc.Save
' Bring the MS Word window to the front
doc.Activate
End Sub
```
Can I change it to search multiple documents (i.e. the entire folder) and export yellow-highlighted words from them into one file? Alternatively, should I open a new empty document and then run a macro on it instead of running a macro on the doc with text?
In short, here's what I need:
1) identify yellow-highlighted words/text in all docs within a selected folder
2)export yellow-highlighted words to a separate file
Any help is much appreciated,
Thanks,
Prefect