使用vb.net在指定的行号Word处插入分页符

时间:2012-05-09 06:28:01

标签: wpf vb.net ms-word

我正在尝试从vb.net构建一个报告,这里是

Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table

    For Each dc As BSPLib.Bills.BillDeliverychallan In Bill.BillDCs

        Dim dcpara As Word.Paragraph
        dcpara = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
        dcpara.Range.Font.Bold = 5
        dcpara.Range.Text = "Delivery Challan: " & dc.DCName
        dcpara.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
        dcpara.Format.SpaceBefore = 12
        dcpara.Format.SpaceAfter = 2   '24 pt spacing after paragraph.
        dcpara.Range.InsertParagraphAfter()

        Dim count As Integer = dc.BillItems.Count

        Dim r As Integer
        oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, count + 1, 7)
        oTable.Range.ParagraphFormat.SpaceBefore = 2
        oTable.Range.ParagraphFormat.SpaceAfter = 0
        oTable.Columns(1).Width = oWord.CentimetersToPoints(0.89)
        oTable.Columns(2).Width = oWord.CentimetersToPoints(5.58)
        oTable.Columns(3).Width = oWord.CentimetersToPoints(2)
        oTable.Columns(4).Width = oWord.CentimetersToPoints(0.6)
        oTable.Columns(5).Width = oWord.CentimetersToPoints(1.7)
        oTable.Columns(6).Width = oWord.CentimetersToPoints(0.5)
        oTable.Columns(7).Width = oWord.CentimetersToPoints(2.01)
        For r = 1 To count
            'BQT TABLES 15  NOS @   25.00   =   375.00
            oTable.Cell(r, 1).Range.Text = dc.BillItems(r - 1).ItemSerial & "."
            oTable.Cell(r, 1).Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
            oTable.Cell(r, 1).Range.Font.Bold = 0
            oTable.Cell(r, 2).Range.Text = dc.BillItems(r - 1).ItemName
            oTable.Cell(r, 2).Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
            oTable.Cell(r, 2).Range.Font.Bold = 0
            oTable.Cell(r, 3).Range.Text = dc.BillItems(r - 1).Quantity & " " & dc.BillItems(r - 1).ItemMeasure
            oTable.Cell(r, 3).Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
            oTable.Cell(r, 3).Range.Font.Bold = 0

            oTable.Cell(r, 4).Range.Text = "@"
            oTable.Cell(r, 4).Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
            oTable.Cell(r, 4).Range.Font.Bold = 0
            oTable.Cell(r, 5).Range.Text = dc.BillItems(r - 1).PricePerUnit.ToString("F")
            oTable.Cell(r, 5).Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
            oTable.Cell(r, 5).Range.Font.Bold = 0
            oTable.Cell(r, 6).Range.Text = "="
            oTable.Cell(r, 6).Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
            oTable.Cell(r, 6).Range.Font.Bold = 0
            oTable.Cell(r, 7).Range.Text = dc.BillItems(r - 1).TotalFinal.ToString("F")
            oTable.Cell(r, 7).Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
            oTable.Cell(r, 7).Range.Font.Bold = 0
        Next
        oTable.Cell(r + 1, 7).Range.Text = (dc.Total / dc.Days).ToString("F")
        oTable.Cell(r + 1, 7).Range.Font.Bold = 0
        oTable.Cell(r, 7).Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
        oTable.Rows.Alignment = Word.WdRowAlignment.wdAlignRowLeft

        'Using a line break before my next table
        Dim oLineBreak1 As Word.Paragraph
        oLineBreak1 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
        oLineBreak1.Format.SpaceAfter = 0
        oLineBreak1.Format.SpaceBefore = 0
        oLineBreak1.Range.Font.Size = 2

        oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 1, 2)
        oTable.Range.ParagraphFormat.SpaceAfter = 0
        oTable.Range.ParagraphFormat.SpaceBefore = 0
        If dc.Days > 1 Then
            oTable.Cell(1, 1).Range.Text = "    From " & dc.FromDate.ToShortDateString & " To " & dc.ToDate & " --- " & dc.Days & " days" & " For: " & dc.ForDate
        Else
            oTable.Cell(1, 1).Range.Text = "    From " & dc.FromDate.ToShortDateString & " To " & dc.ToDate & " --- " & dc.Days & " day" & " For:" & dc.ForDate
        End If
        oTable.Cell(1, 1).Width = oWord.CentimetersToPoints(12.44)
        oTable.Cell(1, 2).Width = oWord.CentimetersToPoints(2.67)
        oTable.Cell(1, 2).Range.Text = dc.Total.ToString("F")
        oTable.Cell(1, 2).Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
        oTable.Rows.Alignment = Word.WdRowAlignment.wdAlignRowCenter




        'Dim break As Word.WdBreakType
        'End If
    Next

问题是,如果for each循环中的数据扩展到下一页,我希望for循环中的整个数据移动到下一页。我想添加一个页数整数,并用开始和结束验证它来确定和更改页面。问题是我不知道如何在指定的行号插入分页符。分页符是Word.WdBreakType.wdPageBreak,但如何将其插入行号?

谢谢。

1 个答案:

答案 0 :(得分:2)

您可以将段落格式化为“保持在一起”:

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.paragraph.keeptogether.aspx

  

确定当Microsoft Word重新分隔文档时,指定段落中的所有行是否仍保留在同一页面上。

这可以确保段落的各行不会在两个页面之间分开。

您还需要使用“保持下一个”:

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.paragraph.keepwithnext.aspx

  

确定当Microsoft Word重新分隔文档时,指定的段落是否与其后面的段落保持在同一页面上。

这可以确保各个段落在两个页面之间不会相互分离。