Consulting

Results 1 to 3 of 3

Thread: Pls Help! Make VBA Macro work in Word

  1. #1

    Pls Help! Make VBA Macro work in Word

    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

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    This works perfectly!! Thank you!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •