选择所有表格和在 MS Word 中拆分

发布于 2025-01-12 22:32:13 字数 793 浏览 5 评论 0原文

我希望我的代码选择 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰之心 2025-01-19 22:32:13

对于您希望实现的任务来说,您的代码过于复杂。话虽如此,这项任务本身就是一项棘手的任务。

拆分表格时,会增加文档中表格的数量。因此,您需要小心引用正确的表。因此,与 Office 应用程序中对集合的许多操作一样,您必须逆向操作。

下面的代码应该可以实现您的任务(至少它可以在我的电脑上运行)。我唯一需要注意的是,如果文档中存在垂直合并的单元格,因为此代码将拆分合并的单元格,但会将文本保留在合并序列的第一行中

Option Explicit

Sub SplitTables()
    
    Dim myTableCounter As Long
    For myTableCounter = ActiveDocument.StoryRanges(wdMainTextStory).Tables.Count To 1 Step -1
    
        SplitTableToRows myTableCounter
        
        
    Next
    
End Sub


Public Sub SplitTableToRows(ByVal ipTableIndex As Long)

    With ActiveDocument.StoryRanges(wdMainTextStory).Tables.Item(ipTableIndex)
    
        Dim myRowIndex As Long
        For myRowIndex = .Rows.Count To 2 Step -1
        
            .Cell(myRowIndex, 1).Range.Select
            Selection.SplitTable
            Selection.MoveUp
            

        Next
        
    End With

End Sub

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

Option Explicit

Sub SplitTables()
    
    Dim myTableCounter As Long
    For myTableCounter = ActiveDocument.StoryRanges(wdMainTextStory).Tables.Count To 1 Step -1
    
        SplitTableToRows myTableCounter
        
        
    Next
    
End Sub


Public Sub SplitTableToRows(ByVal ipTableIndex As Long)

    With ActiveDocument.StoryRanges(wdMainTextStory).Tables.Item(ipTableIndex)
    
        Dim myRowIndex As Long
        For myRowIndex = .Rows.Count To 2 Step -1
        
            .Cell(myRowIndex, 1).Range.Select
            Selection.SplitTable
            Selection.MoveUp
            

        Next
        
    End With

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文