Create tables in Word programmatically

I create tables and write them word-by-word on the fly. I don't know how many tables there will be each time I write data to word, and the problem I am facing is the second table written inside the first cell of my first table. If there was a third table, it fits in the first cell of my second table.

Is there a way to move the cursor out of the table? I tried to create a new range with each table, but the same thing happens.

I have also tried things like tbl.Range.InsertParagraphAfter()

The closest I came to was with a method Relocate

, but this only worked for two tables.

Thanks Ben

+2


source to share


3 answers


I had the same problem and found out that you need to collapse the range to the end of the table range, then insert a line break, collapse again and insert a new table.

Here's some code that uses tables and bookmarks - it's meant to show you how to use native vs. vSTO (and add a click handler to VSTO), but you might want some code instead. Find

With tbRange
.Collapse(Direction:=Word.WdCollapseDirection.wdCollapseEnd)
.InsertParagraphAfter()
.Collapse(Direction:=Word.WdCollapseDirection.wdCollapseEnd).Select()
End With

      



below is what you will need to disallow table nesting within a table.

Sub Assign3TablesToNativeBookmarks()
        'this is the native Word bookmark
        Dim bm As Word.Bookmark
        Dim tb As Word.Table
        Dim tbRange As Word.Range
        Dim i As Integer
        For i = 1 To 3
            bm = Me.Bookmarks.Add(Name:="nestedBookmark" & CStr(i), _
                                  Range:=ThisApplication.Selection.Range)
            tb = bm.Range.Tables.Add(Range:=bm.Range, NumRows:=2, NumColumns:=2)
            With tb
                .Style = "Table Grid"
                tbRange = .Range
                With tbRange
                    .Collapse(Direction:=Word.WdCollapseDirection.wdCollapseEnd)
                    .InsertParagraphAfter()
                    .Collapse(Direction:=Word.WdCollapseDirection.wdCollapseEnd).Select()
                End With
                bm = Me.Bookmarks.Add(Name:="nestedbookmark" & CStr(i), Range:=.Range)
            End With
        Next
        Dim bmMain As Word.Bookmark
        Dim mainBookmarkRange As Word.Range
        Dim mainBookmarkRangeStart As Integer
        Dim mainBookmarkRangeEnd As Integer
        mainBookmarkRangeStart = Me.Bookmarks(1).Start
        mainBookmarkRangeEnd = Me.Bookmarks(Me.Bookmarks.Count).End
        mainBookmarkRange = Me.Range(Start:=mainBookmarkRangeStart, End:=mainBookmarkRangeEnd)
        bmMain = Me.Bookmarks.Add(Name:="mainBookmark", Range:=mainBookmarkRange)
    End Sub
    Sub Assign3TablesToHostControlBookmarks()
        'Word host control of Bookmark
        'bookmarks must be destroyed before resetting the object 
        'added handler
        Dim bm As Microsoft.Office.Tools.Word.Bookmark
        'different from the interop one 
        Dim tb As Word.Table
        Dim tbRange As Word.Range
        Dim i As Integer
        For i = 1 To 3
            bm = Me.Controls.AddBookmark(range:=ThisApplication.Selection.Range, _
                                         Name:="nestedBookmark" & CStr(i))
            tb = bm.Range.Tables.Add(Range:=bm.Range, NumRows:=2, NumColumns:=2)
            With tb
                .Style = "Table Grid"
                tbRange = .Range
                With tbRange
                    .Collapse(Direction:=Word.WdCollapseDirection.wdCollapseEnd)
                    .InsertParagraphAfter()
                    .Collapse(Direction:=Word.WdCollapseDirection.wdCollapseEnd).Select()
                End With
                bm.Delete()
                'this deletes the bookmark before it can be recreated
                bm = Me.Controls.AddBookmark(range:=.Range, Name:="nestedBookmark" & CStr(i))
                AddHandler bm.Selected, AddressOf bm_Selected
                'handler added 
            End With
        Next
        Dim bmMain As Microsoft.Office.Tools.Word.Bookmark
        Dim mainBookmarkRange As Word.Range
        Dim mainBookmarkRangeStart As Integer
        Dim mainBookmarkRangeEnd As Integer
        mainBookmarkRangeStart = Me.Bookmarks(1).Start
        mainBookmarkRangeEnd = Me.Bookmarks(Me.Bookmarks.Count).End
        mainBookmarkRange = Me.Range(Start:=mainBookmarkRangeStart, End:=mainBookmarkRangeEnd)
        bmMain = Me.Controls.AddBookmark(range:=mainBookmarkRange, Name:="mainBookmark")
    End Sub
    Private Sub bm_Selected(ByVal sender As Object, ByVal e As Microsoft.Office.Tools.Word.SelectionEventArgs)
        MessageBox.Show("Hey, you have selected bookmark: " & sender.Name & ". " & _
                        "You did this at " & FormatDateTime(Date.Now(), DateFormat.LongTime))
    End Sub

      

+2


source


The easiest way to insert tables into a word is to generate html tables and then insert them into the file where your cursor is.



This makes it easy to create arbitrarily complex nested tables without using most of the ridiculously complex word interaction functions.

+1


source


Where do you want to place each new table? At the end of the document? Start a new table at the end Document.Content

.

0


source







All Articles