Thread: [Solved] Acronym finder
View Single Post
 
Old 06-13-2019, 04:23 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Instead of writing to the immediate window, write to a new document e.g.
Code:
Sub Acronyms()
Dim dict, k, tmp
Dim regExp, Match, Matches
Dim oDoc As Document
    Set regExp = CreateObject("vbscript.regexp")
    Set dict = CreateObject("scripting.dictionary")
    regExp.Pattern = "[A-Z]{2,}"    '2 or more upper-case letters
    regExp.IgnoreCase = False
    regExp.Global = True
    Set Matches = regExp.Execute(ActiveDocument.Range.Text)
    For Each Match In Matches
        tmp = Match.value
        If Not dict.Exists(tmp) Then dict.Add tmp, 0
        dict(tmp) = dict(tmp) + 1
    Next
    Set oDoc = Documents.Add
    For Each k In dict.Keys
        'Debug.Print k, dict(k)
        oDoc.Range.InsertAfter k & ", " & dict(k) & vbCr
    Next k
    oDoc.Range.Paragraphs.Last.Range.Delete
    oDoc.Range.Sort
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote