Acronym management by Word style rather than text content or minimal length

Posted on 2010-01-07
Last Modified: 2013-11-25
This question is related to the following:<>

Instead of searching for particular acronym content, or a minimal length of uppercase characters, can modified code search for any string that has a particular character Style?  For example, capture every string that has the "Acronym" style applied to it?  I'm not a VBA guy, though I suspect that only the search technique needs to change.

The final code from the prior question is attached.

Thanks for your consideration,

Question by:whitneybroach

    Author Comment

    Well, I tried to embed the code to make it easier to access, but that didn't work.  Here's another attempt:

    Sub ExtractAcronymsToNewDocument()
    'Finds all words consisting of 2 or more uppercase letters
    'in active document document and inserts the words
    'in column 1 of a 3-column table in a new document
    'Each acronym is added only once
    'Room for definition in column 2
    'Page number of first occurrence is added in column 3
    Dim oDoc_Source As Document
    Dim oDoc_Target As Document
    Dim strListSep As String
    Dim strAcronym As String
    Dim oTable As Table
    Dim oRange As Range
    Dim n As Long
    Dim strAllFound As String
    'Find the list separator from international settings
    'In some countries it is comma, in other semicolon
    strListSep = Application.International(wdListSeparator)
    strAllFound = "#"
    Set oDoc_Source = ActiveDocument
    'Create new document for acronyms
    Set oDoc_Target = Documents.Add
    With oDoc_Target
    'Make sure document is empty
    .Range = ""
    'Insert a table with room for acronym and definition
    Set oTable = .Tables.Add(Range:=.Range, NumRows:=2, NumColumns:=3)
    With oTable
    'Format the table a bit
    'Insert headings
    .Cell(1, 1).Range.Text = "Acronym"
    .Cell(1, 2).Range.Text = "Definition"
    .Cell(1, 3).Range.Text = "Page"
    'Set row as heading row
    .Rows(1).HeadingFormat = True
    .Rows(1).Range.Font.Bold = True
    .PreferredWidthType = wdPreferredWidthPercent
    .Columns(1).PreferredWidth = 20
    .Columns(2).PreferredWidth = 70
    .Columns(3).PreferredWidth = 10
    End With
    End With
    With oDoc_Source
    Set oRange = .Range
    n = 1 'used to count below
    With oRange.Find
    .Text = "<[A-Z]{2" & strListSep & "}>"
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = True
    .MatchWildcards = True
    Do While .Execute
    'Continue while found
    strAcronym = oRange
    'Insert in target doc
    'If strAcronym is already in strAllFound, do not add again
    If InStr(1, strAllFound, "#" & strAcronym & "#") = 0 Then
    'Add new row in table from second acronym
    If n > 1 Then oTable.Rows.Add
    'Was not found before
    strAllFound = strAllFound & strAcronym & "#"
    'Insert in column 1 in oTable
    'Compensate for heading row
    With oTable
    .Cell(n + 1, 1).Range.Text = strAcronym
    'Insert page number in column 3
    .Cell(n + 1, 3).Range.Text = oRange.Information(wdActiveEndPageNumber)
    End With
    n = n + 1
    End If
    'If acronym
    End With
    End With
    'Sort the acronyms alphabetically
    With Selection
    .Sort ExcludeHeader:=True, FieldNumber:="Column 1", SortFieldType _
    :=wdSortFieldAlphanumeric, SortOrder:=wdSortOrderAscending
    .HomeKey (wdStory)
    End With
    'Clean up
    Set oDoc_Source = Nothing
    Set oDoc_Target = Nothing
    Set oTable = Nothing
    MsgBox "Finished extracting " & n - 1 & " acronymn(s) to a new document."
    End Sub
    LVL 37

    Accepted Solution

    I'm not so sure about VBA in this instance, but you can always select all instances of text at once which have a character style applied.  Here's how:

    1.  Tools, Options, Edit, be sure "Keep track of formatting" is checked.
    2.  Go to your document
    3.  Format, Styles and Formatting, click on the style "Acronym"
    4.  In the dropdown box to the right of style, select "Select All # Instances"

    Hope this helps a little.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
    This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now