Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting words from a list in WORD

Status
Not open for further replies.

Stampertje

Technical User
Oct 28, 2005
13
NL
Hi all ...

I have a problem that some of you may be able to help me with.

Currently I sort words in a document based on the frequency with wich these words are used.
What I would like to do is to be able to create a list of words that I don't need (this I already have) and then to have all these words deleted in my documents (so this could be a list of over 1000 words)

Anyone have any idea on how to do this?

If you need more details feel free to say so .

Thanks in advance!!!

Ralf
 
More details please.

If I understand correctly, you have a list of words you want deleted out of a document. You want to run this list against every word in a document, and if found, the word is deleted.

Correct?

In what form is the list? A Word document, a text file?

Gerry
 
Hi Ralf,

Something like this (but I don't know where you have your list of words)
Code:
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With

Dim WordsToDelete
WordsToDelete = Array("alpha", "beta", "gamma")
For i = LBound(WordsToDelete) To UBound(WordsToDelete)
    Selection.Find.Execute FindText:=WordsToDelete(i), Replace:=wdReplaceAll
Next

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Or, if your list is in the form of SINGLE paragraphs - that is, each word to delete is its own paragraph, then the following may help.
Code:
Sub RemoveWords()
Dim CheckMeDoc As Document
Dim ListDoc As Document
Dim oListRange As Word.Range
Dim oWord As Word.Paragraph

Set CheckMeDoc = ActiveDocument

Documents.Open FileName:="DumpWords.doc"
Set ListDoc = ActiveDocument
Set oListRange = ListDoc.Range
    CheckMeDoc.Activate
For Each oWord In oListRange.Paragraphs()
    Selection.HomeKey unit:=wdStory
    With Selection.Find
        Do While (.Execute(findtext:=Left(oWord.Range.Text, Len(oWord.Range.Text) - 1), _
                Forward:=True) = True) = True
            Selection.Delete
        Loop
    End With
Next oWord
Set oListRange = Nothing
Set ListDoc = Nothing
Set CheckMeDoc = Nothing
End Sub

This checks each word in the document against a list (from a file DumpWords.doc), and if found, deletes it.

Gerry
 
If I understand correctly, you have a list of words you want deleted out of a document. You want to run this list against every word in a document, and if found, the word is deleted.

Correct :)


Let's say I have a text file that contains the words:
hello
there
where
are
you

And I have a word document that is like this:
Hello there where are you now?

Then I would like the text list to remove all the words from that list leaving only "now?" in the active word document.

I tried the above code but it made my Word freeze up.
I use Word XP (if that matters)

And a BIG THANKS for all the help so far....
 
Hi Ralf,

Not sure which code you tried but if you have a text file you need a different technique to read it.
Code:
Sub DeleteWords()

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With

Dim WordToDelete As String
[green]' Put your text file name below[/green]
Open "C:\Documents and Settings\Tony\Desktop\WordsToDelete.txt" For Input As #1

Do While Not EOF(1)
    Input #1, WordToDelete
    Selection.Find.Execute FindText:=WordToDelete, Replace:=wdReplaceAll
Loop

Close #1

[green]' Tidy up[/green]
Selection.Find.Execute FindText:=" {2,}", MatchWildcards:=True, Replace:=wdReplaceAll

End Sub

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Works like a charm...

The Tidy up gives an error thou but if I disable that part it still does what I need :)

Thanks all for the help!
 
What error does it give? It's just meant to delete the multiple spaces which accrue from deleting words - actually it's not quite correct but I'm interested as to what error it gives.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
It states that there is a incorrect selection criteria.

(it's in dutch so this is the best translation I could come up with)
 
Hi Ralf,

Ah, yes, your international settings - I think you may need to change the comma to a semi-colon - FindText:=" {2;}"



Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Yep.... Works now :)

Once again thanks!!!

You helped me a lot!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top