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..

Re: Delete tables within a bookmark by Jean-Guy

Jean-Guy
Tue May 22 11:28:29 CDT 2007

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
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org



Re: Delete tables within a bookmark by jj

jj
Tue May 22 15:16:42 CDT 2007

On May 22, 12:28 pm, "Jean-Guy Marcil" <DontEvenTry@NoSpam> wrote:
> 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
> jmarcilREM...@CAPSsympatico.caTHISTOO
> 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