Delete Line of Bookmark if the Text is Empty
Hello,
I have several bookmarks in a word document that I am populating via a method using content control and pulling data from excel. A dropdown at the top of the document contains 20 different account names, and when the user chooses one, the bookmarks populate from excel data specific to each account.
Some of the bookmarks do not have any corresponding data in Excel, and are therefore blank. When this happens, I would like to delete the bookmark's line, because it looks as if there is an extra blank line.
However, the bookmark must be reinserted when a new dropdown selection is chosen, because it will hold data for the other accounts.
I have tried the following code:
Dim oBMName As Range
Set oBMName = ActiveDocument.Bookmarks("Bookmark1").Range
ActiveDocument.Bookmarks("Bookmark1").Select
If Selection.Range = "" Then
Selection.Bookmarks("\Line").Range.Delete
ActiveDocument.Bookmarks.Add "Bookmark1", oBMName
End If
This doesn't work. The document has bookmarks set up in the following way;
Bookmark1
Bookmark2
Bookmark3
The code when run does delete Bookmark1 if it is empty. However, when I change the dropdown to an account that has a populated Bookmark1, the text looks like this:
Bookmark2Bookmark1
Bookmark3
So, it is inserting Bookmark1 within Bookmark2. I know this is happening because when I delete the line, it looks like this
Bookmark2
Bookmark3
and when I re-insert Bookmark1 with the code where it was previously, it now goes right after Bookmark2.
Sorry if this is confusing!
|