PDA

View Full Version : Abbreviations in Tables



dfoster
06-29-2016, 09:48 PM
Hi all,

I'm trying to figure out a way to find all abbreviations in a selected table and then list the abbreviations directly beneath that table. I feel like I'm close, but for some reason all abbreviations for all tables are listed. How do I specify the range as the selected table?

Here is what I have so far:

Dim oCol As New Collection
Dim oRng As Range
Dim oDoc As Word.Document
Dim lngIndex As Long
Set oRng = Selection.Range
Selection.Collapse WdCollapseDirection.wdCollapseEnd
With oRng.Find
.Text = "<[A-Z]{1,8}>"
.MatchWildcards = True
While .Execute
On Error Resume Next
oCol.Add oRng.Text, oRng.Text
On Error GoTo 0
oRng.Collapse wdCollapseEnd
Wend
End With
Set oDoc = ActiveDocument
Set oRng = Selection.Range
For lngIndex = 1 To oCol.Count
Selection.InsertAfter oCol(lngIndex) & vbCr
Next lngIndex
End Sub

Thanks in advance for any help you can provide. Thanks also to Greg Maxey for helping me get started.

gmayor
06-29-2016, 11:21 PM
To process the current table


Sub Macro1()
Dim oCol As New Collection
Dim oTable As Table
Dim oRng As Range
Dim lngIndex As Long
Dim sList As String: sList = ""
Set oTable = Selection.Tables(1)
Set oRng = oTable.Range
oRng.Select
With oRng.Find
Do While .Execute(FindText:="<[A-Z]{1,8}>", MatchWildcards:=True)
If oRng.InRange(oTable.Range) Then
On Error Resume Next
oCol.Add oRng.Text, oRng.Text
On Error GoTo 0
oRng.Collapse wdCollapseEnd
End If
Loop
End With
Set oRng = oTable.Range
oRng.MoveEndUntil Chr(13)
oRng.Collapse wdCollapseEnd

For lngIndex = 1 To oCol.Count
sList = sList & oCol(lngIndex)
If lngIndex < oCol.Count Then
sList = sList & vbCr
End If
Next lngIndex
oRng.Text = sList
lbl_Exit:
Set oTable = Nothing
Set oRng = Nothing
Exit Sub
End Sub

dfoster
06-30-2016, 12:05 AM
Oh my gosh, thank you so much!!! This will save me so much time in my editing work.

Was I close at all? I sort of see where I went wrong and what I was missing, but I'm trying to improve my VBA/macro skills.

Thanks again.

gmayor
06-30-2016, 04:55 AM
Essentially where you went awry is that you didn't indicate which table you wanted to work with (Selection.Tables(1))
You then need to set a range to that table and ensure the search is limited to that range (as the search will want to continue).
Perhaps the less obvious part is ensuring that the list is printed after the table and not either in the last cell (nor the first cell of any table that immediately follows it).
Incidentally on reviewing the code I notice I included oRng.Select near the top. That line is superfluous. You don't need to select a range in order to work with it.

dfoster
06-30-2016, 08:12 PM
Ahh, I see. Thank you for the feedback; that helps a lot. Thanks again for your help. This will be so helpful.