montanaheather
New Member
- Joined
- Oct 31, 2011
- Messages
- 25
Hello,
I am reading through pages of transcripts in MS word. I am looking for key words that I need for research purposes. I would like to copy and paste not only the key word, but text before and after the keyword to give the key word context in my research notes. The problem I have is that the amount of text before and after each key word may should be able to change as I run each search. So, instead of search for a keyword and always finding, copying, and pasting the keyword and 10 words before and after it, I would like the ability to tell the macro how many words to copy and paste before and after the key word each time the search is run. Below is a list of what I want the macro to do, along with what I have so far (sorry that it's not much!).
Your help is MUCH appreciated!
To do:
1. find a key word that I input each time I run the search. I would like to choose a different key word each time the macro is run.
2. define the number of words (not just characters) both before and after the key word for the macro to copy and paste, along with the keyword, to give the search results context.
3. I would like the option to change the number of words each time the macro is run. For example, I would like to choose 5 words before and 10 words after on the first search, 8 words before, 5 words after on the second search, etc.
4. I need the macro to find all instances of the keyword, and not just the first instance, before ending. So, running the macro once should return all words that match the keyword, not just the first one found.
5. I would like to use some sort of wild card to find "similar" words, in case the word is not a 100% perfect match.
6. Search word should not be case sensitive.
7. I would like the copy/paste to happen in a new document.
8. If at all possible, I would like to "skip" a copy and paste if a keyword found is not what I want (not every keyword match would be something I would like to copy and paste for later).
Code, both what I wrote and strings I tried to piece together from a replace macro I found online. I wanted to tweak the replace code, but I don't know how (sorry this is so bad...I am new at macros!):
'variables
Dim searchTerm As String
Dim searchTermStringLength As Integer
Dim numberCharacterToSearch As Integer
Dim MyRange As Object
'user asked for search term to begin search subroutine
searchTerm = InputBox("Enter your search terms, Maria Cancro! Then, sit back, relax, and let this macro do some heavy lifting. It's okay - it works out!")
numberCharacterToSearch = InputBox("Enter the number of chaaracters before and after your search term to find, copy, and paste, along with your search term.")
searchTermStringLength = Len(searchTerm)
'the search begins using the searchTerm string entered by user
If Len(searchTerm) <> 0 Then
With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "(*)(searchTerm)(*)"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End With
' user canceled
Exit Sub
End If
End Sub
I am reading through pages of transcripts in MS word. I am looking for key words that I need for research purposes. I would like to copy and paste not only the key word, but text before and after the keyword to give the key word context in my research notes. The problem I have is that the amount of text before and after each key word may should be able to change as I run each search. So, instead of search for a keyword and always finding, copying, and pasting the keyword and 10 words before and after it, I would like the ability to tell the macro how many words to copy and paste before and after the key word each time the search is run. Below is a list of what I want the macro to do, along with what I have so far (sorry that it's not much!).
Your help is MUCH appreciated!
To do:
1. find a key word that I input each time I run the search. I would like to choose a different key word each time the macro is run.
2. define the number of words (not just characters) both before and after the key word for the macro to copy and paste, along with the keyword, to give the search results context.
3. I would like the option to change the number of words each time the macro is run. For example, I would like to choose 5 words before and 10 words after on the first search, 8 words before, 5 words after on the second search, etc.
4. I need the macro to find all instances of the keyword, and not just the first instance, before ending. So, running the macro once should return all words that match the keyword, not just the first one found.
5. I would like to use some sort of wild card to find "similar" words, in case the word is not a 100% perfect match.
6. Search word should not be case sensitive.
7. I would like the copy/paste to happen in a new document.
8. If at all possible, I would like to "skip" a copy and paste if a keyword found is not what I want (not every keyword match would be something I would like to copy and paste for later).
Code, both what I wrote and strings I tried to piece together from a replace macro I found online. I wanted to tweak the replace code, but I don't know how (sorry this is so bad...I am new at macros!):
'variables
Dim searchTerm As String
Dim searchTermStringLength As Integer
Dim numberCharacterToSearch As Integer
Dim MyRange As Object
'user asked for search term to begin search subroutine
searchTerm = InputBox("Enter your search terms, Maria Cancro! Then, sit back, relax, and let this macro do some heavy lifting. It's okay - it works out!")
numberCharacterToSearch = InputBox("Enter the number of chaaracters before and after your search term to find, copy, and paste, along with your search term.")
searchTermStringLength = Len(searchTerm)
'the search begins using the searchTerm string entered by user
If Len(searchTerm) <> 0 Then
With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "(*)(searchTerm)(*)"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End With
' user canceled
Exit Sub
End If
End Sub