选择所有表格和在 MS Word 中拆分
我希望我的代码选择 MS Word 文档的所有表格并拆分所有行,但目前使用此宏,我可以拆分最后一个表格。谁能帮我修改这段代码,它现在也没有选择表格?
以下是代码:
Sub selecttables()
Dim i As Integer
i = ActiveDocument.Tables.Count
Set Tbl = ActiveDocument.Tables(i)
Dim mytable As Table
For Each mytable In ActiveDocument.Tables
mytable.Range.Editors.Add wdEditorEveryone
Next
ActiveDocument.SelectAllEditableRanges (wdEditorEveryone)
ActiveDocument.DeleteAllEditableRanges (wdEditorEveryone)
For Each mytable In ActiveDocument.Tables
Next
Do While Tbl.Rows.Count > 1
Tbl.Cell(2, 1).Range.Select
Selection.InsertBreak Type:=wdColumnBreak '
Set Tbl = ActiveDocument.Tables(ActiveDocument.Tables.Count)
Loop
End Sub
I want my code to select all the tables of a MS word document and split all the rows but currently with this Macro, I am able to split the last table. Can anyone help me with modifying this code, It's not selecting the tables too now?
Following is the code:
Sub selecttables()
Dim i As Integer
i = ActiveDocument.Tables.Count
Set Tbl = ActiveDocument.Tables(i)
Dim mytable As Table
For Each mytable In ActiveDocument.Tables
mytable.Range.Editors.Add wdEditorEveryone
Next
ActiveDocument.SelectAllEditableRanges (wdEditorEveryone)
ActiveDocument.DeleteAllEditableRanges (wdEditorEveryone)
For Each mytable In ActiveDocument.Tables
Next
Do While Tbl.Rows.Count > 1
Tbl.Cell(2, 1).Range.Select
Selection.InsertBreak Type:=wdColumnBreak '
Set Tbl = ActiveDocument.Tables(ActiveDocument.Tables.Count)
Loop
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于您希望实现的任务来说,您的代码过于复杂。话虽如此,这项任务本身就是一项棘手的任务。
拆分表格时,会增加文档中表格的数量。因此,您需要小心引用正确的表。因此,与 Office 应用程序中对集合的许多操作一样,您必须逆向操作。
下面的代码应该可以实现您的任务(至少它可以在我的电脑上运行)。我唯一需要注意的是,如果文档中存在垂直合并的单元格,因为此代码将拆分合并的单元格,但会将文本保留在合并序列的第一行中
Your code is overly complicated for the task you wish to achieve. Having said that, the task itself is a tricky one.
When you split a Table you increase the number of tables in the document. For this reason you need to be careful that you are referencing the correct table. Thus, like many operations on collections in Office apps, you have to work backwards.
The code below should achieve your task (at least it works on my PC). The only caveat I would have is if there are vertically merged cells in your document as this code will split the merged cell but will leave the text in the first row of the merged sequence