在Word文档中连继用代码添两个表的方法

来源:互联网 发布:python 字符串 函数 编辑:程序博客网 时间:2024/04/20 04:41
【要点】:在Word中添加两个表时中间必须空出一行,否则这两个表会合并成一个表。另外,当用代码添加完第一个表后必须将光标移出表格,否则第二个表格会套嵌在第一个表格中。

以下是代码:

Public Class ThisDocument    Private Sub ThisDocument_Startup() Handles Me.Startup        Dim R1 As Word.Range = Globals.ThisDocument.Application.ActiveDocument.Bookmarks("TestBookMark").Range        Dim S1 As Word.Selection = Globals.ThisDocument.Application.Selection        Dim d As Word.WdUnits = Word.WdUnits.wdLine        Dim c As Integer        R1.Select()        With S1.Tables.Add(S1.Range, 2, 3)            .ApplyStyleColumnBands = True            .ApplyStyleRowBands = True            With .Borders(Word.WdBorderType.wdBorderBottom)                .LineStyle = Word.WdLineStyle.wdLineStyleDoubleWavy            End With            With .Borders(Word.WdBorderType.wdBorderTop)                .LineStyle = Word.WdLineStyle.wdLineStyleSingle            End With            With .Borders(Word.WdBorderType.wdBorderHorizontal)                .LineStyle = Word.WdLineStyle.wdLineStyleSingle            End With            With .Borders(Word.WdBorderType.wdBorderVertical)                .LineStyle = Word.WdLineStyle.wdLineStyleSingle            End With            With .Borders(Word.WdBorderType.wdBorderRight)                .LineStyle = Word.WdLineStyle.wdLineStyleSingle            End With            With .Borders(Word.WdBorderType.wdBorderLeft)                .LineStyle = Word.WdLineStyle.wdLineStyleSingle            End With        End With        '光标下移,这个非常重要        c = S1.Tables(1).Rows.Count        S1.MoveDown(Unit:=d, Count:=c)        '空开一行,避免两个表格合并        S1.TypeParagraph()        With R1.Tables.Add(S1.Range, 2, 2)            .ApplyStyleColumnBands = True            .ApplyStyleRowBands = True            With .Borders(Word.WdBorderType.wdBorderBottom)                .LineStyle = Word.WdLineStyle.wdLineStyleSingle            End With            With .Borders(Word.WdBorderType.wdBorderTop)                .LineStyle = Word.WdLineStyle.wdLineStyleDoubleWavy            End With            With .Borders(Word.WdBorderType.wdBorderHorizontal)                .LineStyle = Word.WdLineStyle.wdLineStyleSingle            End With            With .Borders(Word.WdBorderType.wdBorderVertical)                .LineStyle = Word.WdLineStyle.wdLineStyleSingle            End With            With .Borders(Word.WdBorderType.wdBorderRight)                .LineStyle = Word.WdLineStyle.wdLineStyleSingle            End With            With .Borders(Word.WdBorderType.wdBorderLeft)                .LineStyle = Word.WdLineStyle.wdLineStyleSingle            End With        End With    End Sub    Private Sub ThisDocument_Shutdown() Handles Me.Shutdown    End SubEnd Class




欢迎访问《许阳的红泥屋
原创粉丝点击