Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 12-09-2014, 04:31 PM
macropod's Avatar
macropod macropod is offline remove repeated words with " macro " or " wild cards " in texts with parentheses and commas Windows 7 64bit remove repeated words with " macro " or " wild cards " in texts with parentheses and commas Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,359
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try the following macro:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim Para As Paragraph, i As Long, j As Long
Dim StrTxt As String, StrTmp As String, StrFnd As String
With ActiveDocument
  With .Range.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Format = False
    .MatchCase = False
    .Wrap = wdFindContinue
    .MatchWildcards = False
    .Text = " )"
    .Replacement.Text = ")"
    .Execute Replace:=wdReplaceAll
    .Text = "( "
    .Replacement.Text = "("
    .Execute Replace:=wdReplaceAll
    .Text = " ,"
    .Replacement.Text = ","
    .Execute Replace:=wdReplaceAll
    .Text = ", "
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
    .Text = ",{2,}"
    .Execute Replace:=wdReplaceAll
    .Text = "^13{2,}"
    .Replacement.Text = "^p"
    .Execute Replace:=wdReplaceAll
  End With
  For Each Para In .Paragraphs
    StrTxt = Para.Range.Text: StrFnd = ""
    StrTxt = Replace(Replace(Replace(StrTxt, "(", ","), ")", ","), vbCr, "")
    Do While InStr(StrTxt, ", ") > 0
      StrTxt = Replace(StrTxt, ", ", ",")
    Loop
    Do While InStr(StrTxt, " ,") > 0
      StrTxt = Replace(StrTxt, " ,", ",")
    Loop
    Do While InStr(StrTxt, ",,") > 0
      StrTxt = Replace(StrTxt, ",,", ",")
    Loop
    For i = 1 To UBound(Split(StrTxt, ","))
      j = Len(StrTxt)
      StrTmp = Split(StrTxt, ",")(i)
      If StrTmp <> "" Then
        StrTxt = Replace(StrTxt, StrTmp, "")
        If (j - Len(StrTxt)) > Len(StrTmp) Then StrFnd = StrFnd & "," & StrTmp
      End If
    Next
    If StrFnd <> "" Then
      For i = 1 To UBound(Split(StrFnd, ","))
        StrTmp = Split(StrFnd, ",")(i)
        With Para.Range
          .Start = .Start + InStr(.Text, StrTmp) + Len(StrTmp)
          With .Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Format = False
            .Forward = True
            .MatchCase = False
            .Wrap = wdFindStop
            .MatchWholeWord = True
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Text = StrTmp & ","
            .Replacement.Text = ""
            .Execute Replace:=wdReplaceAll
            .Text = StrTmp & "^p"
            .Replacement.Text = "^p"
            .Execute Replace:=wdReplaceAll
          End With
        End With
      Next
    End If
  Next
  With .Range.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Format = False
    .MatchCase = False
    .Wrap = wdFindContinue
    .MatchWildcards = True
    .Text = " ,"
    .Replacement.Text = ","
    .Execute Replace:=wdReplaceAll
    .Text = ", "
    .Execute Replace:=wdReplaceAll
    .Text = ",{2,}"
    .Execute Replace:=wdReplaceAll
    .Text = ","
    .Replacement.Text = ", "
    .Execute Replace:=wdReplaceAll
  End With
End With
Application.ScreenUpdating = True
End Sub
Note: Three times in the code, you will see {2,}. Depending on your system's regional settings you may need to change these to {2;}.

The code also does some tidying-up of your text.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
 

Tags
delete duplicates, remove repeated words



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error: "Changes made were lost...reconnect with server", when switching "from" field randhurrle Outlook 2 02-25-2015 06:51 PM
When composing an e-mail how do I add "Page Layout" and "View" tabs to the ribbon CensorTilSin Outlook 1 12-11-2013 12:05 PM
'Linking' entered information to other "cells" from an original "cell" in MS Word Wade Word 6 09-03-2012 05:22 PM
How to edit the "Format" and the "show level" of an EXISTING table of content? Jamal NUMAN Word 2 08-14-2011 10:46 AM
remove repeated words with &quot; macro &quot; or &quot; wild cards &quot; in texts with parentheses and commas How to choose a "List" for certain "Heading" from "Modify" tool? Jamal NUMAN Word 2 07-03-2011 03:11 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:15 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft