Search code examples
vbams-wordmailmerge

Delete Text and Tables in Word after their creation with VBA


I have some issues programming a mail merge because every content created by my code has to be deleted in order to be not in the way for the next letter in the merging process.

So I wrote a test Macro just to create some tables and delete them.

Here is my progress - It can be run in an new empty Word-File with a Bookmark called "test"

Sub TabsNText()

    Const k As Integer = 2

    Dim doc As Document
    Dim rng As Range
    Dim tab_rngs(k) As Range
    Dim txt_rngs(k) As Range
    Dim tbl As Table

    Set doc = Word.ActiveDocument
    Set rng = doc.Bookmarks("test").Range

    Dim i As Integer

    For i = 1 To k
        Set txt_rngs(i) = rng
        rng.Text = "Title " & i

        rng.Collapse Direction:=wdCollapseEnd
        rng.InsertParagraphAfter
        rng.Collapse Direction:=wdCollapseEnd

        Set tab_rngs(i) = rng

        Set tbl = doc.Tables.Add(rng, 3, 3)
        tbl.Cell(1, 1).Range.Text = "Table" & i
        tbl.Borders.Enable = True

        Set rng = tbl.Range

        rng.Collapse Direction:=wdCollapseEnd
        rng.InsertParagraphAfter
        rng.Collapse Direction:=wdCollapseEnd
    Next i

    rng.Select

    MsgBox ("Now, let's delete that!")

    For i = 1 To k
        txt_rngs(i).Text=""
        tab_rngs(i).Tables(1).Delete
        doc.Bookmarks.Add Name:="test", Range:=rng
    Next i

End Sub

You see. Tables will be deleted properly. But what is about the text and the paragraphs? Is there a method to just make a selection from a start point to some end point and delete all of its content.


Solution

  • Ok - I found an easy solution using the ability to define a range from an start point to an end point of other ranges. But as the position of Ranges is quite unstable (especially when mixing text and tables) one can use an helper bookmark to define the starting point.

    Sub TabsNText()
    
        Const k As Integer = 2
    
        Dim doc As Document
        Dim rng As Range
        Dim tbl As Table
        Const startbkm As String = "test_start"
        Const insertbkm As String = "test"
    
        Set doc = Word.ActiveDocument
        Set rng = doc.Bookmarks(insertbkm).Range
    
        doc.Bookmarks.Add Name:=startbkm, Range:=rng
    
        Dim i As Integer
    
        For i = 1 To k
            rng.Text = "Title " & i
    
            rng.Collapse Direction:=wdCollapseEnd
            rng.InsertParagraphAfter
            rng.Collapse Direction:=wdCollapseEnd
    
            Set tbl = doc.Tables.Add(rng, 3, 3)
            tbl.Cell(1, 1).Range.Text = "Table" & i
            tbl.Borders.Enable = True
    
            Set rng = tbl.Range
    
            rng.Collapse Direction:=wdCollapseEnd
            rng.InsertParagraphAfter
            rng.Collapse Direction:=wdCollapseEnd
        Next i
    
        MsgBox ("Now, let's delete that!")
    
        doc.Range(doc.Bookmarks(startbkm).Range.start, _
                  rng.End).Delete
    
        doc.Bookmarks.Add Name:=insertbkm, Range:=rng
        doc.Bookmarks(startbkm).Delete
    
    
    End Sub