Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-13-2019, 03:51 AM
maceslin maceslin is offline Acronym finder Windows 2K Acronym finder Office 2007
Novice
Acronym finder
 
Join Date: Jul 2012
Posts: 8
maceslin is on a distinguished road
Default Acronym finder

I review long documents with thousands of acronyms and need to create a list of all the acronyms. I found this code on another site but am unable to run it against a small Word doc I get "Compile error User-defined type not defined" Not sure what I need to change

Code:
 
Sub Acronyms()
    Dim dict, k, tmp
    Dim regExp, Match, Matches
    Dim rngRange As Range
    Set regEX = New regExp
    Set dict = CreateObject("scripting.dictionary")
    regEX.Pattern = "[A-Z]{2,}" '2 or more upper-case letters
    regEX.IgnoreCase = False
    regEX.Global = True
    Set Matches = regEX.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
    For Each k In dict.Keys
        Debug.Print k, dict(k)
    Next k
End Sub
Thanks in advance for your help, I am very new to VBA



Dave
Reply With Quote
  #2  
Old 06-13-2019, 04:01 AM
gmayor's Avatar
gmayor gmayor is offline Acronym finder Windows 10 Acronym finder Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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

It doesn't work because the syntax is wrong. e.g. you have declared regExp but called regEX. The following version should work

Code:
Sub Acronyms()
Dim dict, k, tmp
Dim regExp, Match, Matches
Dim rngRange As Range
    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
    For Each k In dict.Keys
Debug.Print k, dict(k)
    Next k
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
  #3  
Old 06-13-2019, 04:10 AM
maceslin maceslin is offline Acronym finder Windows 2K Acronym finder Office 2007
Novice
Acronym finder
 
Join Date: Jul 2012
Posts: 8
maceslin is on a distinguished road
Default

Works great, get expected results now I need to take it one step further.

How do I write the results to a Word file in alphabetical order?
Reply With Quote
  #4  
Old 06-13-2019, 04:23 AM
gmayor's Avatar
gmayor gmayor is offline Acronym finder Windows 10 Acronym finder Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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
  #5  
Old 06-13-2019, 10:10 AM
maceslin maceslin is offline Acronym finder Windows 2K Acronym finder Office 2007
Novice
Acronym finder
 
Join Date: Jul 2012
Posts: 8
maceslin is on a distinguished road
Default

I have modified the code slightly as I realized it was not drawing in acronyms that contained a number and am getting an error on the Set Matches line that state "application-defined or object define error. I suspect it is because I am no longer searching for just text. What is the member of range that include both text and numbers? Nothing jumps out at me

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][A-Za-z0-9]@\" '2 or more upper case letters/numbers
    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
Reply With Quote
  #6  
Old 06-13-2019, 04:56 PM
macropod's Avatar
macropod macropod is offline Acronym finder Windows 7 64bit Acronym finder Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

See: https://www.msofficeforums.com/word-...generator.html
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 06-14-2019, 09:03 AM
maceslin maceslin is offline Acronym finder Windows 2K Acronym finder Office 2007
Novice
Acronym finder
 
Join Date: Jul 2012
Posts: 8
maceslin is on a distinguished road
Default

WOW- I can not even begin to understand what all this code means but it does what I need and will use almost daily
Thanks
Dave
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Acronym finder Acronym Finder Macro for Microsoft Word mars1886 Word VBA 15 03-30-2022 06:56 AM
Synonym Finder subrota Word VBA 3 04-29-2019 12:13 AM
Using a product key finder to transfer MS from PC to Mac - risky? loza890 Office 1 11-02-2014 10:43 PM
Acronym finder Acronym Finder Cray_Z Word VBA 14 09-22-2014 11:42 PM
Function Finder Kevin18014 Excel 3 01-02-2012 04:47 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:28 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft