tags:

views:

447

answers:

3

I am generating tables and writing them to word on the fly. I do not know how many tables there will be each time i write the data to word and the problem I am having is the second table is written inside the first cell of my first table. If there was a third table it is put inside the first cell of my second table.

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

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

The closest I came was using the Relocate method, but this only worked for two tables.

Thanks Ben

A: 

Hi,

The easiest way to insert tables into word is to generate html tables, and then insert this into the file at the point where your cursor is.

It allows for easy creation of arbitrarily complex nested tables without using most of the ridiculously difficult word interop functions.

Toad
I already have all the code in place to add the tables, the problem is moving the cursor outside of the table once it has been constructed. I want each table to be separate, not nested
Ben
A: 

Where is it that you want to put each new table? At the end of the document? Start your new table at the end of Document.Content.

Tom Winter
Underneath the previous table. I open a new word document, and then start adding tables. I would just like them to be one under the other.
Ben
A: 

I've had this exact same issue and learned that you have to collapse the Range to the end of the table range, then insert a line break, collapse again and then insert your new table.

Here's some code that uses tables and bookmarks - it is meant to show how to use native vs. VSTO host bookmarks (and adding a click handler to the VSTO one) - but you may just need part of the code instead. Look for

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

below - that's what you'll need to disallow table-within-table nesting.

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
Otaku