gpt4 book ai didi

vb.net - 以编程方式在 Word 中创建表格

转载 作者:行者123 更新时间:2023-12-04 07:03:33 26 4
gpt4 key购买 nike

我正在生成表格并将它们写成文字。我不知道每次将数据写入 word 时会有多少个表格,我遇到的问题是第二个表格写入第一个表格的第一个单元格内。如果有第三张 table ,它会放在我第二张 table 的第一个单元格内。

有没有办法将光标移出表格?我也尝试为每个表创建一个新范围,但发生了同样的事情。

我也尝试过诸如 tbl.Range.InsertParagraphAfter() 之类的东西

我最接近的是使用 Relocate方法,但这仅适用于两个表。

最佳答案

我遇到了完全相同的问题,并了解到您必须将范围折叠到表格范围的末尾,然后插入换行符,再次折叠,然后插入新表格。

这是一些使用表格和书签的代码 - 它旨在展示如何使用 native 与 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

关于vb.net - 以编程方式在 Word 中创建表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1453215/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com