在页面顶部的Word表格上方插入一行

时间:2012-12-07 13:39:53

标签: vb.net vba word-vba

我创建了一个表。该表从文档的第一行开始。我的问题是我无法在表格上方插入一行。每次我尝试添加段落时,段落都会在最后一个表格之后插入。有没有办法将lin插入第一个表格之上?

说明我的问题:

http://www.techrepublic.com/blog/msoffice/insert-a-line-above-a-word-table-at-the-top-of-the-page/846

我的代码到目前为止:

Dim oApp As Word.Application
    Dim oDoc As Word.Document


    oApp = CType(CreateObject("Word.Application"), Word.Application)
    oDoc = oApp.Documents.Add()


    Dim rng As Word.Range = oDoc.Range(0, 0)
    rng.Font.Name = "Verdana"
    rng.Font.Size = 16


    Dim para As Word.Paragraph = oDoc.Paragraphs.Add()
    para.Range.Text = "Factsheet"



    Dim tlb6 As Word.Table = oDoc.Tables.Add(Range:=rng, NumRows:=1, NumColumns:=4)


    Dim CurrentDateTime As Date = Date.Now
    Dim CurrentDate As Date = New Date(CurrentDateTime.Year, CurrentDateTime.Month, CurrentDateTime.Day)

    tlb6.Cell(1, 1).Range.Text = "Date"
    tlb6.Cell(1, 1).Shading.BackgroundPatternColor = Word.WdColor.wdColorGray20
    tlb6.Cell(1, 1).Borders.OutsideLineStyle = 1
    tlb6.Cell(1, 2).Range.Text = CurrentDate
    tlb6.Cell(1, 2).Borders.OutsideLineStyle = 1
    tlb6.Cell(1, 3).Range.Text = "Issued by"
    tlb6.Cell(1, 3).Shading.BackgroundPatternColor = Word.WdColor.wdColorGray20
    tlb6.Cell(1, 3).Borders.OutsideLineStyle = 1
    tlb6.Cell(1, 4).Range.Text = ""
    tlb6.Cell(1, 4).Borders.OutsideLineStyle = 1

2 个答案:

答案 0 :(得分:2)

如果您使用的是Word 2010,那么这应该可以完成这项工作(尽管它很丑陋且使用了Select和ActiveDocument):

If ActiveDocument.Paragraphs(1).Range.Information(wdWithInTable) = True Then
    tbl6.Rows(1).Cells(1).Range.Collapse direction:=wdLeft
    Selection.SplitTable
End If

检查以确保第一个Paragraph对象在表格内,如果是,则在其上方添加一行文本。

答案 1 :(得分:0)

根据凯文的回答,提出了一个更清洁的解决方案:

ActiveDocument.Range(0, 0).Select
If Selection.Information(wdWithInTable) = True Then
    Selection.InsertBreak Type:=wdColumnBreak
End If