PDA

View Full Version : Pls Help! Make VBA Macro work in Word



chris1993
07-23-2015, 11:46 AM
Hi, I have a macro that I run in excel. It finds words from a list (inside the macro), then makes them bold and red. Right now the macro is set to run through an excel workbook. I need it to do the exact same thing, but through a word doc.

Can someone help me adjust the macro to do so?

Thanks!


here is what I have:


Sub ambiguitycheckv5()

Dim fnd, r As Range, i&, wsh As Worksheet, wrd
word_list1 = Array("above", "below", "it", "such", "the previous", "them", "these", "they", "this", "those", "all", "any", "appropriate", "custom", "efficient", "every", "few", "frequent", "improved", "infrequent", "intuitive", "invalid", "many", "most", "normal", "orginary", "rare", "same", "some", "the complete", "the entire", "transparent", "typical", "usual", "standard", "valid", "accordingly", "almost", "approximately", "by and large", "commonly", "customarily", "efficiently", "frequently", "generally", "hardly ever", "in general", "seamless", "several", "infrequently", "intuitively", "just about", "more often than not", "more or less", "mostly", "nearly", "normally", "not quite", "often", "on the odd occasion", "ordinarily", "rarely", "roughly", "seamlessly", "seldom", "similarily", "sometime", "somewhat", "transparently", "typically", "usually", "the application", "the component", "the date", "the database", "derive", "the field", "determine", "edit", "the file", "the frame", "enable")
word_list2 = Array("the information", "improve", "the message", "indicate", "the module", "the page", "manipulate", "match", "the rule", "maximize", "the screen", "may", "the status", "might", "minimize", "the system", "the table", "modify", "the value", "optimize", "the window", "perform", "adjust", "process", "produce", "provide", "alter", "amend", "calculate", "support", "update", "validate", "verify", "change", "compare", "compute", "convert", "create", "customize")


word_list = Split(Join(word_list1, Chr(1)) & Chr(1) & Join(word_list2, Chr(1)), Chr(1))


Application.ScreenUpdating = False


With CreateObject("VBScript.RegExp")
.Global = True: .IgnoreCase = True


For Each wsh In ThisWorkbook.Sheets
For Each wrd In word_list
.Pattern = "\b" & wrd & "\b"

For Each r In wsh.Range("B2:G9").Cells
If .Test(r) Then
With .Execute(r)
For i = 0 To .Count - 1
With r.Characters(.Item(i).FirstIndex + 1, .Item(i).Length).Font
.Bold = True: .Color = vbRed
End With
Next
End With
End If
Next r


Next wrd
Next wsh

End With
Application.ScreenUpdating = True
End Sub

gmayor
07-24-2015, 12:28 AM
What is "similarily"?

Apart from that (and assuming you want to run the macro from Word and not Excel) the following should do a similar trick for a word document.


Sub ambiguitycheckv6()
Dim oRng As Range
Dim i As Long
Dim word_list1 As Variant
Dim word_list2 As Variant
Dim word_list As Variant

word_list1 = Array("above", "below", "it", "such", "the previous", "them", "these", "they", "this", "those", "all", "any", "appropriate", "custom", "efficient", "every", "few", "frequent", "improved", "infrequent", "intuitive", "invalid", "many", "most", "normal", "orginary", "rare", "same", "some", "the complete", "the entire", "transparent", "typical", "usual", "standard", "valid", "accordingly", "almost", "approximately", "by and large", "commonly", "customarily", "efficiently", "frequently", "generally", "hardly ever", "in general", "seamless", "several", "infrequently", "intuitively", "just about", "more often than not", "more or less", "mostly", "nearly", "normally", "not quite", "often", "on the odd occasion", "ordinarily", "rarely", "roughly", "seamlessly", "seldom", "similarily", "sometime", "somewhat", "transparently", "typically", "usually", "the application", "the component", "the date", "the database", "derive", "the field", "determine", "edit", "the file", "the frame", "enable")
word_list2 = Array("the information", "improve", "the message", "indicate", "the module", "the page", "manipulate", "match", "the rule", "maximize", "the screen", "may", "the status", "might", "minimize", "the system", "the table", "modify", "the value", "optimize", "the window", "perform", "adjust", "process", "produce", "provide", "alter", "amend", "calculate", "support", "update", "validate", "verify", "change", "compare", "compute", "convert", "create", "customize")
word_list = Split(Join(word_list1, Chr(1)) & Chr(1) & Join(word_list2, Chr(1)), Chr(1))
Application.ScreenUpdating = False
For i = 0 To UBound(word_list)
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(FindText:=word_list(i), MatchWholeWord:=True)
oRng.Font.Bold = True
oRng.Font.Color = wdColorRed
oRng.Collapse 0
Loop
End With
Next i
Application.ScreenUpdating = True
End Sub

chris1993
07-24-2015, 05:49 AM
This works perfectly!! Thank you!