Find Table + Delete Table if row above is Empty and......

M

Martin

Hello everybody,

I have a document in which there are several Tables with the text “Responseâ€
and want to produce a macro to do the following:

I want the Macro to find all the Tables with the text “Response†and if the
Row immediately above is Empty the Macro should delete both the Table and the
Empty Row.

Really appreciate help with this one.
 
P

Peter Hewett

Hi Martin

Since only tables have rows, when you say <I want the Macro to find all the Tables with
the text “Response” and if the Row immediately above is Empty> to mean the paragraph
preceding the table. If that's the case then this should do the trick:

Public Sub SearchInTables()
Dim tblTemp As Word.Table
Dim rngParaBeforeTable As Word.Range

' Do this for ALL tables in the document
For Each tblTemp In ActiveDocument.Tables

' Setup for the search
With tblTemp.Range.Find
.ClearFormatting
.Text = "Response"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False

' Find the required text
If .Execute Then

' Locate the paragraph before the table
Set rngParaBeforeTable = tblTemp.Range
rngParaBeforeTable.Collapse wdCollapseStart
rngParaBeforeTable.MoveStart wdParagraph, -1
rngParaBeforeTable.MoveEnd wdCharacter, -1

' Is the paragraph before the table empty (or contains just white space)
If LenB(Trim$(rngParaBeforeTable.Text)) = 0 Then

' Set range to include preceding paragraph, table and
' terminating paragraph mark
rngParaBeforeTable.End = tblTemp.Range.End
rngParaBeforeTable.MoveEnd wdParagraph, 1

' Delete paragraph before table and the table itself
rngParaBeforeTable.Text = vbNullString
End If
End If
End With
Next
End Sub ' SearchInTables

I've actually coded it, so that if the paragraph preceding the table is empty or contains
"white space" it will be deleted.

HTH + Cheers - Peter
 
H

Helmut Weber

Hi Martin,
what do you mean by row above?
The row above the word you are looking for,
would then be in the table, the word is in.
the Macro should delete both the Table
and the Empty Row.
After deleting the table, the row has gone, too,
and can't therefore be deleted. (?)
 
H

Helmut Weber

Hi Martin,
first step:
----------------------------------
Sub Test444()
Dim oTbl As Table
ResetSearch
For Each oTbl In ActiveDocument.Tables
With oTbl.Range.Find
.Text = "response"
If .Execute Then
oTbl.Select ' for testing only
oTbl.Delete
End If
End With
Next
ResetSearch
End Sub
'--------------------------------
Private Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
---
As soon as I know, what you mean by "row",
I think, I could help you with that, too.
A row is a part of a table, maybe called line, too,
but that would lead to still more confusion.
A row, as Word understands it, cannot exist
outside a table. A row immediately above a table
is impossible, there must be something in between.
Otherwise it is a part of the table. Do you mean
a paragraph above the table, or a line?
 
M

Martin

Thanks a lot Helmut,

The "First Step" is working fine.

Yes you are right, I should have said LINE instead of row. Below is my
previous reply where I have replaced row with LINE.

"The code below is removing one Table and the LINE immediately above the
Table
that was deleted. I would like the macro to also search through the document
and determine if the table and the LINE above are to be deleted or not.

The document contains several tables with the text "Response:". The text
"Response:" is within each table. If the LINE above the table contains any
kind of text I want to keep both the table and the text in the LINE above.
However if the LINE above the table is empty I want the macro to remove both
table and the empty LINE above table."

Kind regards,

Martin
 
P

Peter Hewett

Hi Martin

Did you bother to try the code I posted. I believe it does exactly what you want.

Cheers - Peter

Thanks a lot Helmut,

The "First Step" is working fine.

Yes you are right, I should have said LINE instead of row. Below is my
previous reply where I have replaced row with LINE.

"The code below is removing one Table and the LINE immediately above the
Table
that was deleted. I would like the macro to also search through the document
and determine if the table and the LINE above are to be deleted or not.

The document contains several tables with the text "Response:". The text
"Response:" is within each table. If the LINE above the table contains any
kind of text I want to keep both the table and the text in the LINE above.
However if the LINE above the table is empty I want the macro to remove both
table and the empty LINE above table."

Kind regards,

Martin

HTH + Cheers - Peter
 
P

Peter Hewett

Hi Martin

The code is earlier in the thread, in my news reader it's the very first response to your
post. Here's the post again (including the code) in full:

Since only tables have rows, when you say <I want the Macro to find all the Tables with
the text “Response” and if the Row immediately above is Empty> to mean the paragraph
preceding the table. If that's the case then this should do the trick:

Public Sub SearchInTables()
Dim tblTemp As Word.Table
Dim rngParaBeforeTable As Word.Range

' Do this for ALL tables in the document
For Each tblTemp In ActiveDocument.Tables

' Setup for the search
With tblTemp.Range.Find
.ClearFormatting
.Text = "Response"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False

' Find the required text
If .Execute Then

' Locate the paragraph before the table
Set rngParaBeforeTable = tblTemp.Range
rngParaBeforeTable.Collapse wdCollapseStart
rngParaBeforeTable.MoveStart wdParagraph, -1
rngParaBeforeTable.MoveEnd wdCharacter, -1

' Is the paragraph before the table empty (or contains just white space)
If LenB(Trim$(rngParaBeforeTable.Text)) = 0 Then

' Set range to include preceding paragraph, table and
' terminating paragraph mark
rngParaBeforeTable.End = tblTemp.Range.End
rngParaBeforeTable.MoveEnd wdParagraph, 1

' Delete paragraph before table and the table itself
rngParaBeforeTable.Text = vbNullString
End If
End If
End With
Next
End Sub ' SearchInTables

I've actually coded it, so that if the paragraph preceding the table is empty or contains
"white space" it will be deleted.

HTH + Cheers - Peter

Hi Peter,

I have searched, tested and amended a lot of the suggestions posted. Please
direct me where I may find your code.

Kind regards,

Martin

HTH + Cheers - Peter
 
P

Peter Hewett

Hi Martin

I've just retested the code using Word XP and it works ok. So it could be down to
document structure. The code's well documented so you should be able to step through the
code and find out what's going on. If you set a break point at the "
rngParaBeforeTable.Text = vbNullString" statement and then in the IDE's Immediate window
type "rngParaBeforeTable.Select" and press enter, if you toggle back to the Word document
window you will actually see the text it's going to delete selected.

I did not allow for nested tables!

Just double check that you have all of the code I posted as I can't think why it's
failing, but then again I could be being real dumb.

HTH + Cheers - Peter
 
M

Martin

Hi Peter,

Thanks again for your help and patience.

I did what you asked me to do - the "Break" and the "Select" command. By
doing that I discovered that your code is deleting the paragraph immediately
below the table as well as the paragraph above.

I have tried to amend your code but am not clever enough to make it work
properly.

Kind regards,

Martin
 
P

Peter Hewett

Hi Martin

Yes, I designed it so that it deleted the paragraph mark that contains the table otherwise
you tend to end up with empty paragraphs which just create generally unwanted vertical
white space in the document. If you want to leave the tables paragraph mark just remove
the following line of code:
rngParaBeforeTable.MoveEnd wdParagraph, 1

That should do the trick.

Well done, for getting this far using break points and the Immediate window!

HTH + Cheers - Peter
 
M

Martin

Hi Peter,

We are nearly there. Now it is selecting exactly what I want to delete
however I get an error message on "rngParaBeforeTable.Text = vbNullString":

Run time error '6028':

The range cannot be deleted.

Kind regards,

Martin
 
P

Peter Hewett

Hi Martin

I didn't expect that, but I can reproduce it! So a little bit of lateral thinking was
required! So add back in the line of code you deleted and change the following line
from:
rngParaBeforeTable.Text = vbNullString

to:
rngParaBeforeTable.Text = vbCrLf

so the whole thing should now look like this:

Public Sub SearchInTables()
Dim tblTemp As Word.Table
Dim rngParaBeforeTable As Word.Range

' Do this for ALL tables in the document
For Each tblTemp In ActiveDocument.Tables

' Setup for the search
With tblTemp.Range.Find
.ClearFormatting
.Text = "Response"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False

' Find the required text
If .Execute Then

' Locate the paragraph before the table
Set rngParaBeforeTable = tblTemp.Range
rngParaBeforeTable.Collapse wdCollapseStart
rngParaBeforeTable.MoveStart wdParagraph, -1
rngParaBeforeTable.MoveEnd wdCharacter, -1

' Is the paragraph before the table empty (or contains just white space)
If LenB(Trim$(rngParaBeforeTable.Text)) = 0 Then

' Set range to include preceding paragraph, table and
' terminating paragraph mark
rngParaBeforeTable.End = tblTemp.Range.End
rngParaBeforeTable.MoveEnd wdParagraph, 1

' Delete paragraph before table and the table itself,
' but effectively leave the final paragraph mark after the table
rngParaBeforeTable.Text = vbCrLf
End If
End If
End With
Next
End Sub ' SearchInTables

HTH + Cheers - Peter
 

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