Consulting

Results 1 to 5 of 5

Thread: Solved: Delete Word table based on text immediately above...

  1. #1

    Solved: Delete Word table based on text immediately above...

    Hi all...

    I wish to delete any table that occur immediately after a line of text
    (paragraph) that is red.

    Example:

    paragraph 1
    table
    paragraph 2
    table <== DELETE
    paragraph 3
    table
    paragraph 4
    table <== DELETE

    Note that the red text can appear anywhere (could be any of the four paragraphs) and there is always a table immediately after...

    I've tried recording a macro for direction (not pretty)... It seems to work, but it keeps crashing at the last table selection Code 5941 Requested member of the collection does not exist.

    [VBA]Sub killTables()

    Selection.Find.ClearFormatting
    With Selection.Find
    .Font.Color = wdColorRed
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    End With
    Selection.Find.Execute
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.Tables(1).Select
    Selection.Cut
    With Selection.Find
    .Font.Color = wdColorRed
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    End With
    Selection.Find.Execute
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.Tables(1).Select
    Selection.Cut
    With Selection.Find
    .Font.Color = wdColorRed
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    End With
    Selection.Find.Execute
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.Tables(1).Select
    Selection.Cut
    With Selection.Find
    .Font.Color = wdColorRed
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    End With
    Selection.Find.Execute
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.Tables(1).Select 'crashes here
    Selection.Cut
    Selection.Find.ClearFormatting
    End Sub[/VBA]

    Any suggestions... direction.... would be appreciated...

    Thanks as always...

    J

  2. #2
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    992
    Location
    [VBA]Sub DelAfterRed()
    Dim Rng As Range


    Set Rng = ActiveDocument.Range


    Do
    With Rng.Find
    .ClearFormatting
    .Text = ""
    .Replacement.Text = ""
    .Font.Color = wdColorRed
    .Execute
    End With

    If Rng.Find.Found Then
    Rng.MoveStart wdTable
    Rng.MoveEnd wdTable
    Rng.Cut
    End If

    Loop Until Not Rng.Find.Found
    End Sub
    [/VBA]

    Hope this helps!

    David


  3. #3
    Tinbendr,

    You Rock!!! Works great!!!! Thank you so very much!!!

    Some of the things you used, I've never seen before... I learn so much from you guys...

    Best regards.

    Julie

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,710
    Location
    Julie, you will improve even more if you really start to do something that has been suggested to you a number of times. Try to stop using Selection. Try to understand and use Range.

  5. #5
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    Hi JulieJinky,

    Please see: http://www.excelguru.ca/node/7

    It isn't polite to ask for the same problem to be solved in multiple forums when you don't let on in each that that's what you're doing.

    For anyone else, see the discussion in microsoft.public.word.programming
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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