Word VBA to identify a noun/pronoun/proper noun and look back for THE

Kuljack

Active Member
Joined
Aug 14, 2015
Messages
327
Hello all,

I'm looking to write a macro within Word that skims the document for all nouns, pronouns, and proper nouns and looks back 4 characters from the front of the word for the following text "the " including the space, and highlight the section trimmed if it does not match "the ", and include the noun/pronoun/proper noun within the highlighting.

Ideally, the scenario would look something like this...

"Cat walked into room and the cat sniffed McDonald's food, cat was center of attention for everyone else in room"

The result would look something like this, underlining to represent sections highlighted from the macro.

"Cat walked into room and the cat sniffed McDonald's food, cat was center of attention for everyone else in room."

Tried to capture each scenario where a the would be needed, but purposely excluded it.


Currently, this is the macro I have scriptted that is working but want to add to it's effort, it's a little messy so any advice would be greatly appreciated.


Code:
Sub ReviewDocs()
'
' ReviewDocs Macro
'
'
Application.ScreenUpdating = False
Dim arrWords, i As Long

With ActiveDocument.Range.Find
Options.DefaultHighlightColorIndex = wdBrightGreen
arrWords = Array(" ", " ", "!", "+", "=", ">", "<", "'", "*", _
"coworker", "construct", "input", "key", "because", "utilized", _
"you", "We", "our", "Can't", "could've", "should've", "Don't", _
"doesn't", "couldn't", "Won't", "aren't", "Will", "Should", _
"jr.", "sr.", "mod", "doc", "i.e.", "e.g.", "4506t", "double click", _
"drop down", "right click", "auto commit", "auto populate", "co borrower", _
"coborrower", "cosigner", "co signer", "Deed in lieu", "face to face", _
"j day", "non profit", "owner occupied", "non owner occupied", "non recordable", _
"w2", "hit", "cost efficient", "cost effictive", "in order to", "utilize", _
"must", "in general", "FYI", "please", "notate", "click on", "&", "1", "1/", "2", _
"2/", "3", "3/", "4", "4/", "5", "5/", "6", "6/", "7", "7/", "8", "8/", "9", "9/", _
"10/", "11/", "12/", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", _
"24/7", "jan", "feb", "mar", "apr", "jun", "jul", "aug", "sept", "e mail", "e-mail", _
"digest", "depress", "cease", "AM", "PM", "min", "and/or")
.ClearFormatting
.Replacement.ClearFormatting
.MatchWholeWord = True
.Replacement.Text = ""
.Replacement.Highlight = True
For i = 0 To UBound(arrWords)
.Text = arrWords(i)
.Execute Replace:=wdReplaceAll
Next

arrWords = Array("oct", "nov", "dec", "mon", "tues", "wed", _
"thurs", "fri", "sat", "sun", "account holder", "implied", _
"till", "prior to", "provided that", "reiterate", "irregardless", _
"n/a", "inferred", "first come", "utilize", "forwards", "whom", _
"first served", "first-come", "first-served", "fixed rate", "he/she", _
"s/he", "his/her", "h/er", "etc", "et al", "et cetera", "via", "lob", _
"log on", "log off", "log-on", "log-off")
Options.DefaultHighlightColorIndex = wdBrightGreen
.Replacement.Highlight = True
For i = 0 To UBound(arrWords)
.Text = arrWords(i)
.Execute Replace:=wdReplaceAll
Next

Options.DefaultHighlightColorIndex = wdYellow
arrWords = Array("absolute necessity", "accounted for the fact that", _
"actual fact", "advance planning", "afix a signature to", _
"after the conclusion of", "as a result of", "as per your request", _
"as soon as", "at a much greater rate than", "at all times", "at the time of", _
"at this time", "at which time", "at your earliest convenience", _
"based on the fact that", "be of assistance to", "by way of", _
"call your attention to the fact that", "collaborate together", "consensus of opinion", _
"despite the fact that", "downward adjustment", "enclosed please find", _
"Feel free to check back", "few in number", "for the purpose of", "for this reason", _
"For your viewing pleasure", "great deal of", "hassle-free", "head of", _
"I am writing this letter to inform you that", "I was unaware of the fact that", _
"In the amount of", "in a hasty manner", "in accordance with", "in lieu of", _
"in order to", "in receipt of", "in reference to", "in spite of the fact that")
.Replacement.Highlight = True
For i = 0 To UBound(arrWords)
.Text = arrWords(i)
.Execute Replace:=wdReplaceAll
Next

Options.DefaultHighlightColorIndex = wdYellow
arrWords = Array("in the near future", "interact with each other", _
"Is dependent upon", "it came to my attention", "it is incumbent upon us", _
"it is necessary that you", "majority of", "mix together", "new innovation", _
"on account of", "One-stop shopping", "owing to the fact that", _
"pause for a moment", "please be advised", "prior to", "provided that", _
"reach a conclusion", "refer back", "regarding", "reiterate", "so as to", _
"still continues to", "subsequent to", "the fact that I arrived", _
"the question as to", "there is no doubt that", "until such time as")
.Replacement.Highlight = True
For i = 0 To UBound(arrWords)
.Text = arrWords(i)
.Execute Replace:=wdReplaceAll
Next

End With
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
There is no VBA code in word that identify words as any given part of grammar. Accordingly, you'd need to build an array of all the words you want to test and add them to the macro. I think your code you be improved by not trying to re-use the same arrays:
Code:
Sub ReviewDocs()
Application.ScreenUpdating = False
Dim arrWordsGrn, arrWordsYlw, arrWordsPrn, i As Long
'
arrWordsGrn = Array("!", "+", "=", ">", "<", "'", "*", _
"coworker", "construct", "input", "key", "because", "utilized", _
"you", "We", "our", "Can't", "could've", "should've", "Don't", _
"doesn't", "couldn't", "Won't", "aren't", "Will", "Should", _
"jr.", "sr.", "mod", "doc", "i.e.", "e.g.", "4506t", "double click", _
"drop down", "right click", "auto commit", "auto populate", "co borrower", _
"coborrower", "cosigner", "co signer", "Deed in lieu", "face to face", _
"j day", "non profit", "owner occupied", "non owner occupied", "non recordable", _
"w2", "hit", "cost efficient", "cost effictive", "in order to", "utilize", _
"must", "in general", "FYI", "please", "notate", "click on", "&", "1", "1/", "2", _
"2/", "3", "3/", "4", "4/", "5", "5/", "6", "6/", "7", "7/", "8", "8/", "9", "9/", _
"10/", "11/", "12/", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", _
"24/7", "jan", "feb", "mar", "apr", "jun", "jul", "aug", "sept", "e mail", "e-mail", _
"digest", "depress", "cease", "AM", "PM", "min", "and/or", _
"" & _
"oct", "nov", "dec", "mon", "tues", "wed", _
"thurs", "fri", "sat", "sun", "account holder", "implied", _
"till", "prior to", "provided that", "reiterate", "irregardless", _
"n/a", "inferred", "first come", "utilize", "forwards", "whom", _
"first served", "first-come", "first-served", "fixed rate", "he/she", _
"s/he", "his/her", "h/er", "etc", "et al", "et cetera", "via", "lob", _
"log on", "log off", "log-on", "log-off")
'
arrWordsYlw = Array("absolute necessity", "accounted for the fact that", _
"actual fact", "advance planning", "afix a signature to", _
"after the conclusion of", "as a result of", "as per your request", _
"as soon as", "at a much greater rate than", "at all times", "at the time of", _
"at this time", "at which time", "at your earliest convenience", _
"based on the fact that", "be of assistance to", "by way of", _
"call your attention to the fact that", "collaborate together", "consensus of opinion", _
"despite the fact that", "downward adjustment", "enclosed please find", _
"Feel free to check back", "few in number", "for the purpose of", "for this reason", _
"For your viewing pleasure", "great deal of", "hassle-free", "head of", _
"I am writing this letter to inform you that", "I was unaware of the fact that", _
"In the amount of", "in a hasty manner", "in accordance with", "in lieu of", _
"in order to", "in receipt of", "in reference to", "in spite of the fact that", _
"in the near future", "interact with each other", _
"" & _
"Is dependent upon", "it came to my attention", "it is incumbent upon us", _
"it is necessary that you", "majority of", "mix together", "new innovation", _
"on account of", "One-stop shopping", "owing to the fact that", _
"pause for a moment", "please be advised", "prior to", "provided that", _
"reach a conclusion", "refer back", "regarding", "reiterate", "so as to", _
"still continues to", "subsequent to", "the fact that I arrived", _
"the question as to", "there is no doubt that", "until such time as")
'
arrWordsPrn = Array("cat", "McDonald", "center", "room")
'
With ActiveDocument.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .MatchWholeWord = True
  .MatchCase = False
  .Replacement.Text = ""
  .Format = True
  .Forward = True
  .Wrap = wdFindStop
  .Replacement.Highlight = True
  Options.DefaultHighlightColorIndex = wdBrightGreen
  For i = 0 To UBound(arrWordsGrn)
    .Text = arrWordsGrn(i)
    .Execute Replace:=wdReplaceAll
  Next
  Options.DefaultHighlightColorIndex = wdYellow
  For i = 0 To UBound(arrWordsYlw)
    .Text = arrWordsYlw(i)
    .Execute Replace:=wdReplaceAll
  Next
  Options.DefaultHighlightColorIndex = wdPink
  For i = 0 To UBound(arrWordsPrn)
    .Text = arrWordsPrn(i)
    .Execute Replace:=wdReplaceAll
  Next
  Options.DefaultHighlightColorIndex = wdNoHighlight
  For i = 0 To UBound(arrWordsPrn)
    .Text = "the " & arrWordsPrn(i)
    .Execute Replace:=wdReplaceAll
  Next
  For i = 0 To UBound(arrWordsPrn)
    .Text = "a " & arrWordsPrn(i)
    .Execute Replace:=wdReplaceAll
  Next
End With
Application.ScreenUpdating = True
End Sub
Note the additional code for processing your example. Instead of highlighting the preceding characters, though, it highlights just the offending words.
 
Upvote 0
Thank you Paul!

I didn't even realize I was reusing the array name like that. I appreciate the suggestions for revision, and the insight of how to work around this. I actually like that it just highlights the offending words, that looks great in action.

Looks like I will have to begin investing some time into building a library of nouns :
 
Upvote 0
My guess is that buried somewhere in Word are a set of nouns and the like that are already identified because grammar check highlights incorrect word usage when it checks the grammar and it could only really do that if it had a list of some sort available. Whether it's accessible or not is another matter. Probably, but it may be involved. Still may be worth investigating. It could save you some time.
 
Upvote 0
I may have found something, I've been poking at the libararies of MS Word for a few hours now, but I'm not sure how to tap into this for Word to use it, if it's even an option but here it is.

WdPartOfSpeech.wdNoun

You can also select from this Enumerated Constant list Adjectives, Adverbs, Conjunctions, Idioms, Interjections, Other (whatever that is), Preposition, Pronouns, and Verbs.

There seems to be the potential here, I'm just not savy enough yet to utilize it.
 
Upvote 0
While it's true Word's VBA model has PartOfSpeech properties, those are part of the thesaurus, not the spell-checker. As such it won't tell you what part of speech a word is; all it will let you do is specify that any synonyms returned for a given are to be limited to a particular part of speech. For example, with a sentence like:
I saw the chicken run and I saw the chicken run.
it won't tell you that the first 'chicken' is a noun but the second is an adjective, or that the first 'run' is a verb and the second is a noun - or is that the other way around ...?
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,517
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top