#1
|
|||
|
|||
VBA to solve duplicate words in one line
Hello,
I have a lots of word documents and in any of them I have a lots of lines which finish with paragraph. My problem is that in lines I have duplicate words. I need VBA to find and delete duplicate words in one line. It has to look just in one line and if it finds duplicate words it has to delete them and to leave just one of them. Then it has to go to next line, to look for duplicate words and delete them,.... Looking forward for any reply. BM |
#2
|
||||
|
||||
What are your criteria for duplicates? After all, in:
The quick brown fox jumped over a lazy dog. The flat cat ate a brown mouse. the words 'The', 'a' and 'brown' are all repeated on the same line, but I'd hardly call any of them duplicates. Also Word doesn't work in lines - it works in paragraphs. That's because what constitutes a line in a multi-line paragraph can vary according to what printer is attached, plus to simple things like changes to margins, etc. Word VBA also doesn't work in grammatical sentences.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Hello and thanks for reply.
My lines in word looks like this: Many years ago many years ago I did nice job - that is one of lines that I have duplicate words. So I want my line to be Many years ago I did nice job I am not so familiar with English, but I hope so that will help about my criteria. BM |
#4
|
||||
|
||||
The problem with what you're asking is that there is no reliable way of determining what a duplicate word or phrase is. Even in your example, 'many' is not a literal duplicate of 'Many'. Also, what happens if there are other words between your duplicates, for example:
Many years ago I did many years ago a nice job. For all anyone except the author knows, the correct expression after duplicate removal might be: Many years ago I did many a nice job. or: Many years ago I did a nice job. Both are grammatically and linguistically valid.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Thanks again for quick reply, but I don't need any word to be moved from its position in line. So if line is "Many many years ago ago I did good job", I just need vba to remove many and ago and to leave line like this "Many years ago I did good job". There is no need for grammar or any other spelling. Just simple remove duplicates in one line.
If that is not possible, thank you again for ur time. BM |
#6
|
||||
|
||||
Quote:
The quick brown fox jumped over a lazy dog. The flat cat ate a brown mouse. into: The quick brown fox jumped over a lazy dog. flat cat ate mouse. ??? This isn't about vba checking spelling or grammar - or about words being moved - it's a question of logic. And, as I said: Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Yes please, I want to have exactly what u wrote. In English language that may look very stupid and funny, but for may language that makes sense.
Looking forward for ur reply |
#8
|
||||
|
||||
Try the following macro - it doesn't find 'duplicates' where one word uses capitals and the other doesn't, but it should deal with all the rest.
Code:
Sub Demo() Dim i As Long Application.ScreenUpdating = False With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "(<[A-Za-z]@>)([!^13]@)<\1>" .Replacement.Text = "\1\2" .Format = False .Forward = True .Wrap = wdFindContinue .MatchWildcards = True End With Do While i <> Len(.Text) i = Len(.Text) .Find.Execute Replace:=wdReplaceAll Loop With .Find .Text = "[ ]{2,}" .Replacement.Text = " " .Execute Replace:=wdReplaceAll End With End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Word 2013 words cut at end of line and other decalations | Alex.g | Word | 4 | 06-20-2022 07:50 PM |
How to find and delete duplicate words in doc | cinvest | Word | 1 | 09-29-2014 08:34 PM |
Can anyone solve this problem? | ChrisHoppyBot | Office | 0 | 02-20-2013 01:08 AM |
Creating different spacing for words on the same line | Jenjen80 | Word | 1 | 08-12-2010 05:52 AM |
please help me to solve this issue...... | middo1022 | PowerPoint | 0 | 08-28-2009 04:47 AM |