VBA: Deleting a Bookmarked Table Without Deleting the Bookmark

I have written enough Visual Basic for Applications (VBA) code in Microsoft Office applications that I know Word VBA functionality is lacking. Sometimes it is just plain difficult compared to Excel, for example.

While writing the code for a form which is meant to update a payment schedule on a construction contract, I realized that I need to address the possibility of the table being corrupt or missing. I Bookmark all of my Word tables to give them a distinct location and name which is easy to reference in the code. (The form for which I was coding is shown in the following image.)

Payment Schedule form in VBA

Deleting the corrupted table does not work as it also removes the associated Bookmark. So I had to look for another method. I will admit that I found the answer to my problem on the StackOverflow website: Delete contents of bookmark without deleting bookmark in ms word using c#. It was an answer written in C#, not VBA. However, it was very easy to translate to my chosen language. Besides the language difference, the only change I made was to leave out the setting and using of variables for the start and end of the bookmark. I think that this is an unnecessary waste of code and one Range variable handled this alone.

So simply put, the following code deletes a table which is the only content within the bookmark, “Schedule_of_Payments”, which in turn deletes the bookmark. Then the bookmark is reinserted using it’s previously saved range (as in the following outlined pseudocode.)

  1. Save the Bookmark’s range Start and End point variable to variable declared as a Range.
  2. Delete the table (which deletes the bookmark.)
  3. Reinsert the bookmark at the saved range location (saved in step 1.)
  4. Set a range variable to use as a location for adding the new table.
  5. Add a new table to the new bookmark using the range variable.
  6. Set a varible to the new table for simplicity in the code that follows, which is not shown (formatting the table.)
 With ThisDocument

' Set the bookmark's location markers in a range variable
Set rngBkMrk = .Range(.Bookmarks("Schedule_of_Payments").Start, .Bookmarks("Schedule_of_Payments").End)

' Delete the table

' Reinsert the bookmark
.Bookmarks.Add Name:="Schedule_of_Payments", Range:=rngBkMrk

Set rng = .Bookmarks("Schedule_of_Payments").Range

' Insert a new table with 3 rows
.Tables.Add Range:=rng, NumRows:=3, NumColumns:=2

Set tblPayments = rng.Tables(1)

End With

Using this code you can delete the bookmark safely and still have a reference to its location within the document. This could be handy for other tasks, but I have yet to encounter any. I have reused so much VBA code that many times my biggest programming problem is the infamous Cut and Paste error. That is when you forget to change or remove references to other projects after you paste some code in a new one.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.