vbams-wordbookmarksblank-line

Remove extra line after adding/deleting a bookmark in VBA


In My Word Template I have few bullet points, and I will select the required points based on selection from the GUI and remove the rest of the points that are not selected from the code. So it depends on the user how many points that he would select.

I have created bookmarks for all these points as shown in the braces above.

Now the problem is, the user selects few number of bullet points from the above list and output would leave an extra blank line below.

I have tried many approaches from changing the selection of the bookmarks to adding bullet point form the code. I need to get rid of the extra blank line.
But nothing seems to work. I would appreciate the help in this. TIA

If .cboChanges1.Value = "No" Then
     DeleteBookmark ("bookmark1")
Else
     AddBullet ("bookmark1")
End If
If .cboChanges2.Value = "No" Then
     DeleteBookmark ("bookmark2")
Else
     AddBullet("bookmark2")     
End If

etc.. where cboChanges1 and cboChanges2 are combo boxes to choose Yes/no.

Sub DeleteBookmark(strBookmark As String)
    Dim rng As Range
    Set rng = ActiveDocument.Bookmarks(strBookmark).Range
    rng.Delete
    Set rng = Nothing
End Sub

Before Screenshot enter image description here

Output screen after first 3 options, you can see extra blank line as highlighted enter image description here

I am adding the bullet point dynamically, as follows

Sub AddBullet(strBookmark As String)
    ActiveDocument.Bookmarks(strBookmark).Select
    Selection.Range.ListFormat.ApplyBulletDefault
End Sub

This is how the bookmarks are arranged, enter image description here


Solution

  • A table cell always contains an end of cell marker which is a combination of paragraph and end of cell mark. This means that an empty cell always contains at least one paragraph.

    As the end of cell marker cannot be deleted when you delete the text in the last bookmark you will get an empty paragraph because the previous bookmarked range ends with a paragraph mark. enter image description here

    You can deal with this in your DeleteBookmark routine by checking the length of the final paragraph of the cell. If it only contains two characters then it has no text and the preceding paragraph mark can be deleted.

    Sub DeleteBookmark(strBookmark As String)
       Dim rng As Range
       Set rng = ActiveDocument.Bookmarks(strBookmark).Range
       rng.Delete
       Set rng = rng.Cells(1).Range.Paragraphs.Last.Range
       If Len(rng.Text) = 2 Then
          'paragraph contains no text so delete preceding paragraph mark
          rng.Collapse wdCollapseStart
          rng.MoveStart wdCharacter, -1
          rng.Delete
       End If
       Set rng = Nothing
    End Sub
    

    If you are going to write code for Word you need to learn what the formatting marks are so that you can understand how a document is constructed.

    FYI It is not necessary to select a range to apply bullets.

    Sub AddBullet(strBookmark As String)
        ActiveDocument.Bookmarks(strBookmark).Range.ListFormat.ApplyBulletDefault
    End Sub