Results 1 to 5 of 5
  1. #1
    Star Lounger
    Join Date
    Aug 2001
    Location
    Hampshire, England
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts

    deleting words from document according to substrin (Word 2003)

    This is probably dead easy, but I've spent ages writing loops that run so slowly that Word effectively stops responding.
    I've got a document that is essentially just a word list. I want to delete all the words that do not have a certain substring (actually "er" or "um" to be precice) in them. So, leave in "erratum" and "herbal" and "gumbo" but delete "the" and "hippopotamus" etc.
    How do I get this to run at a reasonable rate? (with just a space between each word this is 157 A4 pages of 10/10pt Courier!)

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

    Re: deleting words from document according to substrin (Word 2003)

    Does this work?

    Sub Deletem()
    Dim n As Long
    Application.ScreenUpdating = False
    For n = ActiveDocument.Words.Count To 1 Step -1
    If Not ActiveDocument.Words(n) Like "*er*" And _
    Not ActiveDocument.Words(n) Like "*um*" Then
    ActiveDocument.Words(n).Text = " "
    End If
    Next n
    Application.ScreenUpdating = True
    End Sub

  3. #3
    Star Lounger
    Join Date
    Aug 2001
    Location
    Hampshire, England
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: deleting words from document according to substrin (Word 2003)

    Well, I've had this running in the background for five hours now (which is more patience than I'd had with any of my own attempts, I must admit). If it doesn't finish in the next three I'll have to force quit (am running this at work <img src=/S/doh.gif border=0 alt=doh width=15 height=15> )

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

    Re: deleting words from document according to substrin (Word 2003)

    Don't know if this will work any better. It dumps the words containing "er" or "um" into a text file (which can be opened in Word afterwards).

    Sub Alternative()
    Dim f As Integer

    On Error GoTo ErrHandler

    Application.ScreenUpdating = False
    f = FreeFile
    Open "C:Words.txt" For Output As #f
    With Selection.Find
    .ClearFormatting
    .MatchCase = False
    .MatchWildcards = False
    .MatchWholeWord = False
    Selection.HomeKey Unit:=wdStory
    Do While .Execute(FindText:="er")
    Print #f, Selection.Words(1)
    Loop
    Selection.HomeKey Unit:=wdStory
    Do While .Execute(FindText:="um")
    Print #f, Selection.Words(1)
    Loop
    End With

    ExitHandler:
    On Error Resume Next
    Close #f
    Application.ScreenUpdating = True
    Exit Sub

    ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub

  5. #5
    Star Lounger
    Join Date
    Aug 2001
    Location
    Hampshire, England
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: deleting words from document according to substrin (Word 2003)

    "Don't know if this will work any better."
    Waaaaaaay better: this churned through the whole document in about 40secs.
    Many, many thanks Hans.

Posting Permissions

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