Avatar of Andreas Hermle
Andreas HermleFlag for Germany

asked on 

Delete duplicate entries in a word list

Dear Experts:

I got a document that has a long list of sorted font names with a lot of duplicates in it. Each new line is a separate paragraph. Is it possible to delete the redundant entries by running a VBA-Code so that only one entry per listed font remains?

Help is appreciated. Thank you very much in advance.

Regards, Andreas

Example:

Arial
Arial
Garamond
Tahoma
Tahoma
Tahoma
Times New Roman
Times New Roman
Times New Roman



Microsoft WordVisual Basic Classic

Avatar of undefined
Last Comment
Andreas Hermle
Avatar of Blaise Fournier
Blaise Fournier
Flag of Switzerland image

Hi, you could use excel to do it.

http://office.microsoft.com/en-us/excel/HA010346261033.aspx

Hope it helps.

Blaise
Avatar of akahan
akahan
Flag of United States of America image

You could use the free uniq.exe program, available here:

http://openetwork.com/berk.html
ASKER CERTIFIED SOLUTION
Avatar of irudyk
irudyk
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of aikimark
aikimark
Flag of United States of America image

@irudyk

I've seen/experience problems trying to iterate a collection while deleting items(s) from the collection.

==========================
I think the fastest method uses a collection.  (see snippet)

There are a few methods of replacing the text with the ordered font names.  I've included two flavors to illustrate.  Depending on the size and number of strings, you might find that concatenation slows noticably.  The second routine substitutes I/O for concatenation.  I've mitigated this effect by suspending screen refreshing.

Sub UniqueFonts()
    Dim colFonts As New Collection
    Dim pThing As Paragraph
    Dim vUniqueFont As Variant
    Dim sFonts As String
    
    On Error Resume Next
 
    For Each pThing In Selection.Paragraphs
        colFonts.Add pThing.Range.Text, pThing.Range.Text
    Next
 
    For Each vUniqueFont In colFonts
        sFonts = sFonts & vUniqueFont
    Next
    Selection.Text = sFonts
End Sub
 
=====================================
Sub UniqueFonts_NoConcat()
    Dim colFonts As New Collection
    Dim pThing As Paragraph
    Dim vUniqueFont As Variant
    Dim sFonts As String
    
    On Error Resume Next
    
    For Each pThing In Selection.Paragraphs
        colFonts.Add pThing.Range.Text, pThing.Range.Text
    Next
 
    Application.ScreenUpdating = False
    Selection.Text = vbNullString
    For Each vUniqueFont In colFonts
        Selection.InsertAfter vUniqueFont
    Next
    Application.ScreenUpdating = True
End Sub

Open in new window

Avatar of Andreas Hermle

ASKER

Hey irudyk: Great code, superfast and just a couple of lines.
Thank you very much. Regards, Andreas
Visual Basic Classic
Visual Basic Classic

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.

165K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo