![]() |
|
|
|
#1
|
|||
|
|||
|
It worked here with your sample text. However, if the first paragraph was not a list type and contain a AAA, then it would get stuck in a loop. This fixes:
Code:
Sub GetAcronyms() 'A basic Word macro coded by Greg Maxey http://gregmaxey.com/word_tips.html Dim oTbl As Table Dim oRng As Word.Range, oRngLP As Range Dim oDoc As Word.Document Dim lngIndex As Long, lngVal As Long Dim arrVals() As String ReDim arrVals(3, 0) Set oRng = ActiveDocument.Range With oRng.Find .Text = "<[A-Z]{1,4}>" .MatchWildcards = True While .Execute arrVals(0, UBound(arrVals, 2)) = oRng.Text arrVals(3, UBound(arrVals, 2)) = oRng.Sentences(1).Text If Not oRng.Paragraphs(1).Range.ListFormat.ListString = vbNullString Then Err_FirstPara: arrVals(1, UBound(arrVals, 2)) = oRng.Information(wdActiveEndPageNumber) arrVals(2, UBound(arrVals, 2)) = oRng.Paragraphs(1).Range.ListFormat.ListString Else Set oRngLP = oRng.Duplicate Do On Error GoTo Err_FirstPara Set oRngLP = oRngLP.Paragraphs(1).Previous.Range Loop Until oRngLP.Paragraphs(1).Range.ListFormat.ListString <> vbNullString arrVals(1, UBound(arrVals, 2)) = oRngLP.Information(wdActiveEndPageNumber) arrVals(2, UBound(arrVals, 2)) = oRngLP.Paragraphs(1).Range.ListFormat.ListString End If On Error GoTo 0 ReDim Preserve arrVals(3, UBound(arrVals, 2) + 1) oRng.Collapse wdCollapseEnd Wend End With ReDim Preserve arrVals(3, UBound(arrVals, 2) - 1) Set oDoc = Documents.Add Set oTbl = oDoc.Tables.Add(oDoc.Range, 1, 4) With oTbl .Columns(1).Width = CentimetersToPoints(2.5) .Columns(2).Width = CentimetersToPoints(1.5) .Columns(3).Width = CentimetersToPoints(2.5) .Columns(4).Width = CentimetersToPoints(9) End With For lngIndex = 0 To UBound(arrVals, 2) For lngVal = 0 To 3 oTbl.Rows.Last.Cells(lngVal + 1).Range.Text = arrVals(lngVal, lngIndex) Next lngVal If lngIndex < UBound(arrVals, 2) Then oTbl.Rows.Add Next lngIndex 'oTbl.Sort ExcludeHeader:=False, FieldNumber:="Column 1", _ SortFieldType:=wdSortFieldAlphanumeric, SortOrder:=wdSortOrderAscending, _ FieldNumber2:="Column 3", SortFieldType2:=wdSortFieldNumeric, SortOrder2 _ :=wdSortOrderAscending lbl_Exit: Set oDoc = Nothing: Set oTbl = Nothing End Sub |
|
#2
|
|||
|
|||
|
Thank you. Please pardon my delay in responding. No excuses; I just got overly busy. I certainly appreciate your work.
The revised macro still throws a runtime error #91, object variable or with block variable not set error when there is more than one line containing an acronym that comes before the first list. For example, in your demo document, add a second line that says, "New line BBB." When the macro is run it errors out at, "Set oRngLP = oRngLP.Paragraphs(1).Previous.Range". My workaround is just to make the first line a list and change it back after the macro runs, but I thought I'd let you know in case others are following this thread. Then a couple of last thoughts/requests, which are by no means important but would make the macro pretty slick. (1) Sometimes I want listed the appearance of every acronym regardless of how many times it appears. Sometimes (most often) I only want to output the first appearance of an acronym. I wonder if there is an easy way to add code that would restrict the output to only the first instance of the acronym and then comment out the code if I want to output every instance. If it's not easy to do then I can just sort and delete the extras, which is what I have been doing. (2) In using your macro I noted that where I have a sentence or paragraph where every character is in uppercase then all the words 'n' characters in length will be output as acronyms. So I replaced the .text code you had with: .Text = "\([A-Z]{2,}\)" This allows me to only capture acronyms contained within parenthesis. Do you see any issue with this modification? |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| adding tables based on a number given in a form | coba | Word VBA | 5 | 12-26-2015 07:05 PM |
Acronym Finder
|
Cray_Z | Word VBA | 14 | 09-22-2014 11:42 PM |
How to create a table with a number of line depending a number entered by user
|
Cellendhyll | Word Tables | 3 | 07-10-2014 05:49 AM |
List Style Numbering picks up out of order number from LATER list
|
spthomas | Word | 12 | 12-16-2013 05:23 PM |
Adding Characters to Page Number
|
Daved2424 | Word | 6 | 01-22-2011 07:11 PM |