我正在生成表格并在运行中将它们写入单词。我不知道每次将数据写入word时会有多少个表,而我遇到的问题是第二个表写在我第一个表的第一个单元格内。如果有第三张表,则将其放入第二张表的第一个单元格中。
有没有办法将光标移出表格?我尝试过为每个表创建一个新范围,但同样的事情发生了。
我也尝试了tbl.Range.InsertParagraphAfter()
我最接近的是使用Relocate
方法,但这仅适用于两个表。
答案 0 :(得分:2)
我遇到了同样的问题,并了解到必须将Range折叠到表格范围的末尾,然后插入换行符,再次折叠然后插入新表格。
以下是一些使用表和书签的代码 - 它旨在展示如何使用本机与VSTO主机书签(以及向VSTO添加点击处理程序) - 但您可能只需要部分代码。寻找
With tbRange
.Collapse(Direction:=Word.WdCollapseDirection.wdCollapseEnd)
.InsertParagraphAfter()
.Collapse(Direction:=Word.WdCollapseDirection.wdCollapseEnd).Select()
End With
下面的- 这就是你需要禁止表内表格嵌套的方法。
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
答案 1 :(得分:1)
将表插入word的最简单方法是生成html表,然后将其插入到光标所在的文件中。
它允许轻松创建任意复杂的嵌套表,而无需使用大多数难以理解的单词互操作函数。
答案 2 :(得分:0)
你想把每张新桌放在哪里?在文件的最后?在Document.Content
结尾处开始新表。