Word vba find all words

  • Remove From My Forums
  • Question

  • Hi everyone.

    i want to get all words in a Word Document and List all by ascending order then check the words finally replace wrong word with correct word…

    can everyone help me… solutions…suggestions…?

    • Moved by

      Sunday, January 23, 2011 6:21 AM
      Word, not VSTO-specific (From:Visual Studio Tools for Office)

Answers

  • Use:

    Sub WordFrequency()
             Dim SingleWord As String           ‘Raw word pulled from doc
            Const maxwords = 9000              ‘Maximum unique words allowed
            Dim Words(maxwords) As String      ‘Array to hold unique words
            Dim Freq(maxwords) As Integer      ‘Frequency counter for Unique Words
            Dim WordNum As Integer             ‘Number of unique words
            Dim ByFreq As Boolean              ‘Flag for sorting order
            Dim ttlwds As Long                 ‘Total words in the document
            Dim Excludes As String             ‘Words to be excluded
            Dim Found As Boolean               ‘Temporary flag
            Dim j, k, l, Temp As Integer       ‘Temporary variables
            Dim tword As String                ‘
             ‘ Set up excluded words
    ‘         Excludes = «[the][a][of][is][to][for][this][that][by][be][and][are]»
            Excludes = «»
            Excludes = InputBox$(«Enter words that you wish to exclude, surrounding each word with [ ].», «Excluded Words», «»)
    ‘        Excludes = Excludes & InputBox$(«The following words are excluded: » & Excludes & «. Enter words that you wish to exclude, surrounding each word with [ ].», «Excluded Words», «»)
    ‘ Find out how to sort
    ByFreq = True
    Ans = InputBox$(«Sort by WORD or by FREQ?», «Sort order», «FREQ»)
    If Ans = «» Then End
    If UCase(Ans) = «WORD» Then
       ByFreq = False
    End If
    Selection.HomeKey Unit:=wdStory
    System.Cursor = wdCursorWait
    WordNum = 0
    ttlwds = ActiveDocument.Words.Count
    Totalwords = ActiveDocument.BuiltInDocumentProperties(wdPropertyWords)
            ‘ Control the repeat
            For Each aword In ActiveDocument.Words
                SingleWord = Trim(aword)
                If SingleWord < «A» Or SingleWord > «z» Then SingleWord = «» ‘Out of range?
                If InStr(Excludes, «[» & SingleWord & «]») Then SingleWord = «» ‘On exclude list?
                If Len(SingleWord) > 0 Then
                    Found = False
                    For j = 1 To WordNum
                        If Words(j) = SingleWord Then
                            Freq(j) = Freq(j) + 1
                            Found = True
                            Exit For
                        End If
                    Next j
                    If Not Found Then
                        WordNum = WordNum + 1
                        Words(WordNum) = SingleWord
                        Freq(WordNum) = 1
                    End If
                    If WordNum > maxwords — 1 Then
                        j = MsgBox(«The maximum array size has been exceeded. Increase maxwords.», vbOKOnly)
                        Exit For
                    End If
                End If
                ttlwds = ttlwds — 1
                StatusBar = «Remaining: » & ttlwds & »     Unique: » & WordNum
            Next aword
             ‘ Now sort it into word order
            For j = 1 To WordNum — 1
                k = j
                For l = j + 1 To WordNum
                    If (Not ByFreq And Words(l) < Words(k)) Or (ByFreq And Freq(l) > Freq(k)) Then k = l
                Next l
                If k <> j Then
                    tword = Words(j)
                    Words(j) = Words(k)
                    Words(k) = tword
                    Temp = Freq(j)
                    Freq(j) = Freq(k)
                    Freq(k) = Temp
                End If
                StatusBar = «Sorting: » & WordNum — j
            Next j
             ‘ Now write out the results
            tmpName = ActiveDocument.AttachedTemplate.FullName
            Documents.Add Template:=tmpName, NewTemplate:=False
            Selection.ParagraphFormat.TabStops.ClearAll
            With Selection
                For j = 1 To WordNum
                    .TypeText Text:=Words(j) & vbTab & Trim(Str(Freq(j))) & vbCrLf
                Next j
            End With
            ActiveDocument.Range.Select
            Selection.ConvertToTable
            Selection.Collapse wdCollapseStart
            ActiveDocument.Tables(1).Rows.Add BeforeRow:=Selection.Rows(1)
            ActiveDocument.Tables(1).Cell(1, 1).Range.InsertBefore «Word»
            ActiveDocument.Tables(1).Cell(1, 2).Range.InsertBefore «Occurrences»
            ActiveDocument.Tables(1).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
            ActiveDocument.Tables(1).Rows.Add
            ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count, 1).Range.InsertBefore «Total words in Document»
            ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count, 2).Range.InsertBefore Totalwords
            ActiveDocument.Tables(1).Rows.Add
            ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count, 1).Range.InsertBefore «Number of different words in Document»
            ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count, 2).Range.InsertBefore Trim(Str(WordNum))
            System.Cursor = wdCursorNormal
            j = MsgBox(«There were » & Trim(Str(WordNum)) & » different words «, vbOKOnly, «Finished»)
        Selection.HomeKey wdStory

    End Sub
     — Hope this helps.

    Doug Robbins — Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    «Adnan Ebrahimi» wrote in message news:8ddb02da-d954-40f4-b4a5-403fa8fa5a69@communitybridge.codeplex.com…

    Hi everyone.

    i want to get all words in a Word Document and List all by ascending order then check the words finally replace wrong word with correct word…

    can everyone help me… solutions…suggestions…?


    Doug Robbins — Word MVP dkr[atsymbol]mvps[dot]org

    • Marked as answer by
      Bessie Zhao
      Monday, February 7, 2011 10:02 AM

  • Hi Abdan

    If you’re working with the Word object model, going through the .NET/COM, Word OLE interface, and given the speed of processing in that interface, I don’t think you can get anything faster than what you’ve got. It’s the nature of the thing you’re doing.

    Possibly, if the file is in the Office 2007/2010 Open XML file format (docx, for example) you can work with that file as you would any XML file. But we can’t help you here with that. You’ll find more information on Open XML file format at openXMLDeveloper.org


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by
      Bessie Zhao
      Monday, February 7, 2011 10:02 AM

title keywords f1_keywords ms.prod api_name ms.assetid ms.date ms.localizationpriority

Find.Execute method (Word)

vbawd10.chm162529724

vbawd10.chm162529724

word

Word.Find.Execute

3b607955-0e82-aa13-dad1-7a5069a57b9d

06/08/2017

medium

Find.Execute method (Word)

Runs the specified find operation. Returns True if the find operation is successful. Boolean.

Syntax

expression.Execute (FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace, MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl)

expression Required. A variable that represents a Find object.

Parameters

Name Required/Optional Data type Description
FindText Optional Variant The text to be searched for. Use an empty string («») to search for formatting only. You can search for special characters by specifying appropriate character codes. For example, «^p» corresponds to a paragraph mark and «^t» corresponds to a tab character.
MatchCase Optional Variant True to specify that the find text be case-sensitive. Corresponds to the Match case check box in the Find and Replace dialog box (Edit menu).
MatchWholeWord Optional Variant True to have the find operation locate only entire words, not text that is part of a larger word. Corresponds to the Find whole words only check box in the Find and Replace dialog box.
MatchWildcards Optional Variant True to have the find text be a special search operator. Corresponds to the Use wildcards check box in the Find and Replace dialog box.
MatchSoundsLike Optional Variant True to have the find operation locate words that sound similar to the find text. Corresponds to the Sounds like check box in the Find and Replace dialog box.
MatchAllWordForms Optional Variant True to have the find operation locate all forms of the find text (for example, «sit» locates «sitting» and «sat»). Corresponds to the Find all word forms check box in the Find and Replace dialog box.
Forward Optional Variant True to search forward (toward the end of the document).
Wrap Optional Variant Controls what happens if the search begins at a point other than the beginning of the document and the end of the document is reached (or vice versa if Forward is set to False). This argument also controls what happens if there is a selection or range and the search text is not found in the selection or range. Can be one of the WdFindWrap constants.
Format Optional Variant True to have the find operation locate formatting in addition to, or instead of, the find text.
ReplaceWith Optional Variant The replacement text. To delete the text specified by the Find argument, use an empty string («»). You specify special characters and advanced search criteria just as you do for the Find argument. To specify a graphic object or other nontext item as the replacement, move the item to the Clipboard and specify «^c» for ReplaceWith.
Replace Optional Variant Specifies how many replacements are to be made: one, all, or none. Can be any WdReplace constant.
MatchKashida Optional Variant True if find operations match text with matching kashidas in an Arabic-language document. This argument may not be available to you, depending on the language support (U.S. English, for example) that you have selected or installed.
MatchDiacritics Optional Variant True if find operations match text with matching diacritics in a right-to-left language document. This argument may not be available to you, depending on the language support (U.S. English, for example) that you have selected or installed.
MatchAlefHamza Optional Variant True if find operations match text with matching alef hamzas in an Arabic-language document. This argument may not be available to you, depending on the language support (U.S. English, for example) that you have selected or installed.
MatchControl Optional Variant True if find operations match text with matching bidirectional control characters in a right-to-left language document. This argument may not be available to you, depending on the language support (U.S. English, for example) that you have selected or installed.
MatchPrefix Optional Variant True to match words beginning with the search string. Corresponds to the Match prefix check box in the Find and Replace dialog box.
MatchSuffix Optional Variant True to match words ending with the search string. Corresponds to the Match suffix check box in the Find and Replace dialog box.
MatchPhrase Optional Variant True ignores all white space and control characters between words.
IgnoreSpace Optional Variant True ignores all white space between words. Corresponds to the Ignore white-space characters check box in the Find and Replace dialog box.
IgnorePunct Optional Variant True ignores all punctuation characters between words. Corresponds to the Ignore punctuation check box in the Find and Replace dialog box.

Return value

Boolean

Remarks

If MatchWildcards is True, you can specify wildcard characters and other advanced search criteria for the FindText argument. For example, «*(ing)» finds any word that ends in «ing».

To search for a symbol character, type a caret (^), a zero (0), and then the symbol’s character code. For example, «^0151» corresponds to an em dash (—).

Unless otherwise specified, replacement text inherits the formatting of the text it replaces in the document. For example, if you replace the string «abc» with «xyz», occurrences of «abc» with bold formatting are replaced with the string «xyz» with bold formatting.

Also, if MatchCase is False, occurrences of the search text that are uppercase will be replaced with an uppercase version of the replacement text, regardless of the case of the replacement text. Using the previous example, occurrences of «ABC» are replaced with «XYZ».

Example

This example finds and selects the next occurrence of the word «library».

With Selection.Find 
    .ClearFormatting 
    .MatchWholeWord = True 
    .MatchCase = False 
    .Execute FindText:="library" 
End With

This example finds all occurrences of the word «hi» in the active document and replaces each occurrence with «hello».

Set myRange = ActiveDocument.Content 
myRange.Find.Execute FindText:="hi", _ 
    ReplaceWith:="hello", Replace:=wdReplaceAll

[!includeSupport and feedback]

So I have a large list of words and phrases that should not be used in a word document. I would like to write a macro to search a document for these words and phrases and highlight them.

So far I have found and edited some code to search for specific words and it’ll highlight those:

Sub Find_Liability_Words()
'
' Find_Liability_Words Macro
'
'
Application.ScreenUpdating = False
Options.DefaultHighlightColorIndex = wdBrightGreen
Dim StrFnd As String, i As Long, StrRpt As String

' Words to search for
StrFnd = "actually,administer,advise,affirmative,all,always"

With ActiveDocument.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Replacement.Highlight = True
  .Forward = True
  .Wrap = wdFindContinue
  .Format = True
  .MatchCase = False
  .MatchWholeWord = True
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = True
  For i = 0 To UBound(Split(StrFnd, ","))
    .Text = Split(StrFnd, ",")(i)
    .Replacement.Text = "^&"
    .Execute Replace:=wdReplaceAll
    If .Found = True Then StrRpt = StrRpt & vbCr & Split(StrFnd, ",")(i)
  Next
End With
If StrRpt = "" Then
  MsgBox "None of the terms were found.", vbOKOnly
Else
  MsgBox "The following terms were found:" & StrRpt, vbInformation
End If
Application.ScreenUpdating = True

End Sub

Now the issue is that this can only do words and shows an error when trying to find to words together (such as «adequate number»). I am wondering how to make it search for a phrase as well.

In addition to this, the list of words and phrases I’d like to search for is long and could change so editing the code for this could be tedious. Is it possible to have this code refer to a text file or an excel sheet in order to find it’s words and phrases?

Thank you so much for any help!!

Word VBA Find

This example is a simple word macro find the text “a”:

Sub SimpleFind()

    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "a"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
End Sub

Find and Replace

This simple macro will search for the word “their” and replace it with “there”:

Sub SimpleReplace()

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "their"
        .Replacement.Text = "there"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Find and Replace Only in Selection

This VBA macro will find and replace text in a selection. It will also italicize the replaced text.

Sub ReplaceInSelection()
'replaces text JUST in selection . in adittion it makes replaced text italic
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "their"

        With .Replacement
            .Font.Italic = True
            .Text = "there"
        End With

        .Forward = True
        .Wrap = wdFindStop    'this prevents Word from continuing to the end of doc
        .Format = True 'we want to replace formatting of text as well
        .MatchCase = False
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

This line of code prevents VBA from continuing to the end of the Word document:

.Wrap = wdFindStop    'this prevents Word from continuing to the end of doc

This line of code indicates to replace the formatting of the text as well:

.Format = True 'we want to replace formatting of text as well

Find and Replace Only In Range

Instead of replacing text throughout the entire document, or in a selection, we can tell VBA to find and replace only in range.  In this example we defined the range as the first paragraph:

Dim oRange As Range
Set oRange = ActiveDocument.Paragraphs(1).Range
Sub ReplaceInRange()
'replaces text JUST in range [in this example just in the first paragraph]
Dim oRange As Range
Set oRange = ActiveDocument.Paragraphs(1).Range
 oRange.Find.ClearFormatting
    oRange.Find.Replacement.ClearFormatting
    With oRange.Find
        .Text = "their"
        .Replacement.Text = "there"
        .Forward = True
        .Wrap = wdFindStop 'this prevent Word to continue to the end of doc
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    oRange.Find.Execute Replace:=wdReplaceAll
End Sub

No, it doesn’t ‘have’ to be so complicated, but leaving out some parameters might lead to unexpected results …

Re:

Quote:

change it’s style to Body Text and make it bold face

That’s not what the code I posted does. Based on what you had posted, the code I posted looks for ‘Synopsis’ that is already in the ‘Body Text’ Style. Furthermore, you can’t re-format only a single word of many in a paragraph with the ‘Body Text’ Style, as that Style is a paragraph Style. Of course, that doesn’t matter if ‘Synopsis’ is the only word in the paragraphs concerned.

As for changing the font to bold, if ‘Synopsis’ is the only word in the paragraphs concerned, you’d do better to change the ‘Body Text’ Style’s font to bold than to override the Style format. Similarly, if ‘Synopsis’ is only one word of many in a paragraph, you’d do better to apply the ‘Strong’ character Style to ‘Synopsis’ than to override the Style format. For example:

Code:

Sub Demo()
Application.ScreenUpdating = False
With Selection
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "Synopsis"
    .Replacement.Text = "^&"
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = True
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Replacement.Style = "Body Text"
    .Execute Replace:=wdReplaceAll
    .Replacement.Style = "Strong"
    .Execute Replace:=wdReplaceAll
  End With
End With
Application.ScreenUpdating = True
End Sub
Sub Выделить_между()
    Dim MyRange As Range, rStart&, rEnd&
    Set MyRange = ActiveDocument.Content
    With MyRange
        With .Find
            .ClearFormatting
            .Text = "Структура активов"
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchCase = False
            .MatchWholeWord = True
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute
            If .Found Then rStart = MyRange.End: rEnd = rStart
        End With
    End With
    Set MyRange = ActiveDocument.Content
    With MyRange
        With .Find
            .Text = "Внеоборотные активы"
            .Execute
            If .Found Then rEnd = MyRange.Start
        End With
    End With
    If rEnd > rStart Then
        ActiveDocument.Range(rStart, rEnd).Select
        Selection.Copy
    End If
End Sub
 
Sub Выделить_между2()
Dim r
Set r = ActiveDocument.Range
With r.Find
    .ClearFormatting
    .Text = "Структура активов*Внеоборотные активы"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
    If .Execute Then
        ActiveDocument.Range(r.Start + 4, r.End - 4).Select
        Selection.Copy
    Else
        MsgBox "Текст не найден!", vbExclamation
    End If
End With
End Sub
Sub Макрос1()
'
' Макрос1 Макрос
 
For Each dd In ActiveDocument.Tables
    asq = asq + 1
    dd.Name
Next
 
End Sub

Like this post? Please share to your friends:
  • Word vba document close
  • Word vba debug print
  • Word vba column width
  • Word vba close file
  • Word vba close all documents