Results 1 to 6 of 6
  1. #1
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Find by format, list only unique (Word 2003, Off 2

    before I start flailing about, I thought I'd ask here. I have a tracking project in Word and when new entries come in I opted to format them so as to find all new entries. Since the tracking document will have multiple new entries for different sections, I'd like to programmatically go thru the word doc, compile a list of all matches by format and get a list of only each unique item on the list. Any advice on this? FWIW, the document is a series of tables. Output from VBA doesn't have to be fancy - a notepad file would do - even debug.print will suffice...

    TIA

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find by format, list only unique (Word 2003, Off 2

    Could you post a sample document with dummy data so that we have an idea of what to work with?

  3. #3
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find by format, list only unique (Word 2003, O

    here it is, zipped to conform to lounge requirements. I stripped out the content and added some dummy categories to find and list. the basic nut is "find all paragraphs formatted as bold, italic, red across all tables in the document, list only the unique ones", if at all possible...

    i know there's reasons to do this in Excel but there's more compelling reasons to keep it in word.

    Anyhow, I'll be fooling around with this problem too, but if there's a known good solution that'd help!
    Attached Files Attached Files

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find by format, list only unique (Word 2003, O

    Try this; it lists the red, bold, italic text in a new document (as unformatted text):

    Sub ListFormattedParagraphs()
    Dim docSrc As Document
    Dim docTrg As Document
    Application.ScreenUpdating = False
    Set docSrc = ActiveDocument
    Set docTrg = Documents.Add
    docSrc.Activate
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .ClearFormatting
    .Font.Bold = True
    .Font.Italic = True
    .Font.Color = wdColorRed
    .Text = ""
    .MatchWildcards = False
    Do While .Execute
    If Selection.Information(wdWithInTable) Then
    docTrg.Content.InsertAfter Selection.Text
    docTrg.Content.InsertParagraphAfter
    End If
    Loop
    End With
    Application.ScreenUpdating = True
    docTrg.Activate
    End Sub

  5. #5
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Find by format, list only unique (Word 2003, O

    As usual, Hans has come up with an excellent solution but I thought that perhaps it would also be nice to know on what pages the entries appeared. If the solution used index entries then this would be simple enough to do (when armed with the code developed by Hans). Note that I needed to search backwards because the code was terminal looping where the end of cell marker also included the found formatting as this was applied to the XE entry as well.
    <pre>Sub BuildIndex()
    Dim docSrc As Document, aXE As Field
    Application.ScreenUpdating = False
    Set docSrc = ActiveDocument
    'remove the existing index entries
    For Each aXE In docSrc.Fields
    Debug.Print aXE.Code
    If Left(Trim(aXE.Code), 2) = "XE" Or Left(Trim(aXE.Code), 5) = "INDEX" Then
    aXE.Delete
    End If
    Next aXE

    Selection.EndKey Unit:=wdStory
    With Selection.Find
    .ClearFormatting
    .Font.Bold = True
    .Font.Italic = True
    .Font.Color = wdColorRed
    .Text = ""
    .Forward = False
    .MatchWildcards = False
    Do While .Execute
    Debug.Print Selection.Text
    If Selection.Information(wdWithInTable) Then
    docSrc.Indexes.MarkEntry Range:=Selection.Range, _
    Entry:=Selection.Text
    End If
    Selection.Collapse Direction:=wdCollapseStart
    Loop
    End With
    Application.ScreenUpdating = True

    Selection.EndKey Unit:=wdStory
    Selection.TypeParagraph
    With docSrc
    .Indexes.Add Range:=Selection.Range, HeadingSeparator:= _
    wdHeadingSeparatorNone, Type:=wdIndexIndent, _
    RightAlignPageNumbers:=False, NumberOfColumns:=3, _
    IndexLanguage:=wdEnglishAUS
    .Indexes(1).TabLeader = wdTabLeaderDots
    End With

    End Sub</pre>

    Andrew Lockton, Chrysalis Design, Melbourne Australia

  6. #6
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find by format, list only unique (Word 2003, O

    this works just fine. I was looking for a solution that doesn't involve selecting on the screen (although it looks cool) or one requiring for-next loops. If the action is expected to take a long time I'd probably allow the screen updating so the user has some feedback while the thing is running.

    And thanks for the addition, Andrew but this project does not require listing the location of each found element.

Posting Permissions

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