Delete tables within a bookmark

J

jj

I am trying to delete everything within a bookmark. I can delete the
text by setting the text property of the bookmark's range to an empty
string. This, however, doesn't remove the tables. If I use the
Tables property of the range and iterate through to delete them, it
works to delete all the tables inside, but only if the range is not
entirely inside of a table. If a range is inside of a table, the
Tables property returns that parent table and it gets deleted instead.

Does anyone know how to get the tables that are inside of a range,
without getting the table that the range is inside of. Even better
would be a mechanism to delete everything within a Range, including
text, tables, drawing objects, etc., but I have pretty much given up
on that approach and labeled it impossible without iterating through
each of these individually..
 
J

Jean-Guy Marcil

jj was telling us:
jj nous racontait que :
I am trying to delete everything within a bookmark. I can delete the
text by setting the text property of the bookmark's range to an empty
string. This, however, doesn't remove the tables. If I use the
Tables property of the range and iterate through to delete them, it
works to delete all the tables inside, but only if the range is not
entirely inside of a table. If a range is inside of a table, the
Tables property returns that parent table and it gets deleted instead.

Does anyone know how to get the tables that are inside of a range,
without getting the table that the range is inside of. Even better
would be a mechanism to delete everything within a Range, including
text, tables, drawing objects, etc., but I have pretty much given up
on that approach and labeled it impossible without iterating through
each of these individually..

Don't replace the range by something else, just plain delete it.
For example, on my machine, the following code deletes everything in the
rage:

'_______________________________________
Sub DelRange()

Dim rgeDelete As Range

Set rgeDelete = ActiveDocument.Bookmarks("Test").Range

rgeDelete.Delete

End Sub
'_______________________________________

The only problem is if the range is within a table, in which case it deletes
the text, but not the actual rows.
So you would need code to see if Range.Start *and* Range.End are inside the
same table, if so, delete the rows concerned by the range

Assuming you have already run some code to see whether this code is
necessary, use something like

'_______________________________________
Dim rgeDelete As Range
Dim rwDel As Row

Set rgeDelete = ActiveDocument.Bookmarks("Test").Range

'Code to test if range within a table

For Each rwDel In rgeDelete.Rows
rwDel.Delete
Next
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

jj

jj was telling us:
jj nous racontait que :



Don't replace the range by something else, just plain delete it.
For example, on my machine, the following code deletes everything in the
rage:

'_______________________________________
Sub DelRange()

Dim rgeDelete As Range

Set rgeDelete = ActiveDocument.Bookmarks("Test").Range

rgeDelete.Delete

End Sub
'_______________________________________

The only problem is if the range is within a table, in which case it deletes
the text, but not the actual rows.
So you would need code to see if Range.Start *and* Range.End are inside the
same table, if so, delete the rows concerned by the range

Assuming you have already run some code to see whether this code is
necessary, use something like

'_______________________________________
Dim rgeDelete As Range
Dim rwDel As Row

Set rgeDelete = ActiveDocument.Bookmarks("Test").Range

'Code to test if range within a table

For Each rwDel In rgeDelete.Rows
rwDel.Delete
Next
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:http://www.word.mvps.org

Thanks a lot, I could have sworn that I tried that with no luck, but
it worked like a charm. After working for this long with the Word API
though, it could just be that I am going insane.

jj
 

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