EzySetup
03-07-2016, 12:52 PM
I'm using the following macro to generate an acronyms list. It creates a new Word document with the acronym and the page number the acronym appears upon. I'd like to have the macro also return the text of the sentence in which the acronym appears. But the best I've been able to accomplish is for it to just repeat the sentence that appears after the first acronym found. I'm not a coder by any stretch of the imagination, which is likely why I can't figure out what needs to be modified in order to accomplish what I need.
Any help will be greatly appreciated.
Thanks in advance,
Andrew
Sub GetAcronyms()
'A basic Word macro coded by Greg Maxey
'link suppressed due to forum rules
Dim oCol As New Collection
Dim oColPN As New Collection
Dim oColTxt As New Collection 'new
Dim strTxt As String 'new
Dim oRng As Word.Range
Dim oDoc As Word.Document
Dim lngIndex As Long
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "<[A-Z]{1,4}>"
.MatchWildcards = True
'new
With Selection
' Expand selection to current sentence.
.Expand Unit:=wdSentence
End With
strTxt = Selection.Text
While .Execute
On Error Resume Next
oCol.Add oRng.Text, oRng.Text
'Uncomment the following to add the page number of the acronym. Also swap commented row near bottom.
If Err.Number = 0 Then
oColPN.Add oRng.Information(wdActiveEndPageNumber)
oColTxt.Add strTxt 'new
End If
On Error GoTo 0
oRng.Collapse wdCollapseEnd
Wend
End With
Set oDoc = Documents.Add
For lngIndex = 1 To oCol.Count
'oDoc.Range.InsertAfter oCol(lngIndex) & " " & oColPN(lngIndex) & vbCr 'original
oDoc.Range.InsertAfter oCol(lngIndex) & vbTab & oColPN(lngIndex) & vbTab & oColTxt(lngIndex) & vbCr
'oDoc.Range.InsertAfter oCol(lngIndex) & vbCr
Next lngIndex
End Sub
Any help will be greatly appreciated.
Thanks in advance,
Andrew
Sub GetAcronyms()
'A basic Word macro coded by Greg Maxey
'link suppressed due to forum rules
Dim oCol As New Collection
Dim oColPN As New Collection
Dim oColTxt As New Collection 'new
Dim strTxt As String 'new
Dim oRng As Word.Range
Dim oDoc As Word.Document
Dim lngIndex As Long
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "<[A-Z]{1,4}>"
.MatchWildcards = True
'new
With Selection
' Expand selection to current sentence.
.Expand Unit:=wdSentence
End With
strTxt = Selection.Text
While .Execute
On Error Resume Next
oCol.Add oRng.Text, oRng.Text
'Uncomment the following to add the page number of the acronym. Also swap commented row near bottom.
If Err.Number = 0 Then
oColPN.Add oRng.Information(wdActiveEndPageNumber)
oColTxt.Add strTxt 'new
End If
On Error GoTo 0
oRng.Collapse wdCollapseEnd
Wend
End With
Set oDoc = Documents.Add
For lngIndex = 1 To oCol.Count
'oDoc.Range.InsertAfter oCol(lngIndex) & " " & oColPN(lngIndex) & vbCr 'original
oDoc.Range.InsertAfter oCol(lngIndex) & vbTab & oColPN(lngIndex) & vbTab & oColTxt(lngIndex) & vbCr
'oDoc.Range.InsertAfter oCol(lngIndex) & vbCr
Next lngIndex
End Sub