Delete Word table based on text immediately above...

J

JulieJinky

Hi all...

I am so very stuck... This is a weird request, but a necessary one...

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

Example:

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

Note that the red text can appear anywhere and there is always a table
immediately after...

Ideas!?!?! Direction?!?!?

Thanks as always...

J
 
B

Bob Lawn

Can't you record a macro wherein you search for red text and then move the
cursor forward past the table?
In the search dialog, don't alter anything to do with font name or size or
anything - just change font color to red.

bob
 
J

JulieJinky

Hi Bob..

Thanks for replying...

I have done that (see below; it ain't pretty).... but it keeps crashing Code
5941 Requested member of the collection does not exist.

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

Any ideas?!?!?

Bob Lawn wrote:
Can't you record a macro wherein you search for red text and then move the
cursor forward past the table?
In the search dialog, don't alter anything to do with font name or size or
anything - just change font color to red.

bob
 
B

Bob Lawn

Is the table directly below the red text?
As I imagined it, the Find selects the line of red text.
Then delete the text.
Move the cursor down one line so it is then inside the table.
And go to Table -> Delete -> Table. Stop the macro at that point and assign
it to a key.
Maybe your examples are more complicated than that, though.

bob
 
L

Lene Fredborg

The following macro should do what you describe. See the comments in the code.
Note that I made the code so that only the first character of the paragraph
immediately above the table is checked since I imagine that it could be that
e.g. the paragraph mark isn’t red:

Sub KillTables()
Dim oTable As Table

'Iterate through all tables
For Each oTable In ActiveDocument.Tables
'Check the font color of the first character in the paragraph above
'If color is Red, delete table
If
oTable.Range.Paragraphs.First.Previous.Range.Characters(1).Font.Color =
wdColorRed Then
oTable.Delete
End If
Next oTable
End Sub

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
J

JulieJinky via OfficeKB.com

Thanks to both of you....

Lene, your code worked great.... Thanks

J






Lene Fredborg wrote:
The following macro should do what you describe. See the comments in the code.

Note that I made the code so that only the first character of the paragraph
immediately above the table is checked since I imagine that it could be that
e.g. the paragraph mark isn’t red:

Sub KillTables()
Dim oTable As Table

'Iterate through all tables
For Each oTable In ActiveDocument.Tables
'Check the font color of the first character in the paragraph above
'If color is Red, delete table
If
oTable.Range.Paragraphs.First.Previous.Range.Characters(1).Font.Color =
wdColorRed Then
oTable.Delete
End If
Next oTable
End Sub
 
L

Lene Fredborg

You are welcome. I am glad I could help.

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
J

JulieJinky via OfficeKB.com

As mentioned to macropod earlier...

There was absolutely no intent to be rude/impolite/unappreciative of
everyone's efforts.

My apologies to anyone who was offended...

It will not happen again...

J
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top