如何使用 VBA 在 word 文档中的 table 之后设置光标
How to set cursor after a table in word document using VBA
我必须在没有模板的情况下在 Word 文档中创建报告。此报告包含来自 MS Access 的记录 - 将有一些文本,然后是 table,基于记录数的迭代(我将使用 VBA 动态创建 table在记录数上)。
我可以开始使用书签作为起点在 word 文档中插入文本,然后能够添加 table 并填写单元格。完成填写 table 后的问题如何将光标放在 table 之后的下一行以开始插入文本。
以下是我的代码,任何有提示或示例的人都会感激 - 谢谢!
Set wordObj = CreateObject("Word.Application")
Set wordDoc = wordObj.Documents.Open(fileName:=wrdTMPLT, Visible:=True)
wordDoc.Bookmarks("rptdate").Range.Text = Format(DATE, "dd-mmm-yyyy")
Set wordrange = wordDoc.GoTo(what:=wdGoToBookmark, Name:="startpoint") 'set cursor to start point
wordrange.Text = Me.Text3_CHK
Set wordrange = wordDoc.GoTo(what:=wdGoToBookmark, Name:="tblpoint") 'set cursor to location to insert table
Set tbl = wordDoc.Tables.Add(Range:=wordrange, numrows:=4, numcolumns:=2)
tbl.CELL(1, 1).Merge MergeTo:=tbl.CELL(1, 2)
tbl.CELL(3, 1).Merge MergeTo:=tbl.CELL(3, 2)
tbl.CELL(4, 1).Merge MergeTo:=tbl.CELL(4, 2)
tbl.CELL(1, 1).Range.InsertAfter "Title: "
tbl.CELL(2, 1).Range.InsertAfter "Coordinator: "
tbl.CELL(2, 2).Range.InsertAfter "Engineer: "
tbl.CELL(3, 1).Range.InsertAfter "Vendor 1: "
tbl.CELL(3, 2).Range.InsertAfter "Vendor 2: "
tbl.CELL(4, 1).Range.InsertAfter "Contractor: "
tbl.Borders.Enable = False
'Following text to enter after the table above
wordrange.Text = "HellO"
'continue with next table ... n text/table cycle based # of records
要到达 table 之后的点(段),请将 table 的范围分配给 Range
对象,然后将其折叠到终点:
Dim rng as Word.Range
'Do things here until table is finished
Set rng = tbl.Range
rng.Collapse wdCollapseEnd
'Now the Range is after the table, so do things with it, for example:
rng.Text = "more text"
我必须在没有模板的情况下在 Word 文档中创建报告。此报告包含来自 MS Access 的记录 - 将有一些文本,然后是 table,基于记录数的迭代(我将使用 VBA 动态创建 table在记录数上)。 我可以开始使用书签作为起点在 word 文档中插入文本,然后能够添加 table 并填写单元格。完成填写 table 后的问题如何将光标放在 table 之后的下一行以开始插入文本。 以下是我的代码,任何有提示或示例的人都会感激 - 谢谢!
Set wordObj = CreateObject("Word.Application")
Set wordDoc = wordObj.Documents.Open(fileName:=wrdTMPLT, Visible:=True)
wordDoc.Bookmarks("rptdate").Range.Text = Format(DATE, "dd-mmm-yyyy")
Set wordrange = wordDoc.GoTo(what:=wdGoToBookmark, Name:="startpoint") 'set cursor to start point
wordrange.Text = Me.Text3_CHK
Set wordrange = wordDoc.GoTo(what:=wdGoToBookmark, Name:="tblpoint") 'set cursor to location to insert table
Set tbl = wordDoc.Tables.Add(Range:=wordrange, numrows:=4, numcolumns:=2)
tbl.CELL(1, 1).Merge MergeTo:=tbl.CELL(1, 2)
tbl.CELL(3, 1).Merge MergeTo:=tbl.CELL(3, 2)
tbl.CELL(4, 1).Merge MergeTo:=tbl.CELL(4, 2)
tbl.CELL(1, 1).Range.InsertAfter "Title: "
tbl.CELL(2, 1).Range.InsertAfter "Coordinator: "
tbl.CELL(2, 2).Range.InsertAfter "Engineer: "
tbl.CELL(3, 1).Range.InsertAfter "Vendor 1: "
tbl.CELL(3, 2).Range.InsertAfter "Vendor 2: "
tbl.CELL(4, 1).Range.InsertAfter "Contractor: "
tbl.Borders.Enable = False
'Following text to enter after the table above
wordrange.Text = "HellO"
'continue with next table ... n text/table cycle based # of records
要到达 table 之后的点(段),请将 table 的范围分配给 Range
对象,然后将其折叠到终点:
Dim rng as Word.Range
'Do things here until table is finished
Set rng = tbl.Range
rng.Collapse wdCollapseEnd
'Now the Range is after the table, so do things with it, for example:
rng.Text = "more text"