Consulting

Results 1 to 5 of 5

Thread: Abbreviations in Tables

  1. #1
    VBAX Regular
    Joined
    Jun 2016
    Posts
    7
    Location

    Question Abbreviations in Tables

    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.

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Jun 2016
    Posts
    7
    Location

    Thumbs up

    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.

  4. #4
    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.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Regular
    Joined
    Jun 2016
    Posts
    7
    Location
    Ahh, I see. Thank you for the feedback; that helps a lot. Thanks again for your help. This will be so helpful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •