操纵微软词

时间:2013-01-22 05:43:12

标签: c# .net vb.net

我目前正在编写此应用程序以打印并创建一个新word文档的报告。但是我遇到了一个绊脚石。当我将数据写入这个新的Microsoft word doc以检查它是否是一个新页面时,他们是否有任何方式,例如报告占用10页,是否可以检查应用程序何时开始在新页面上书写? / p>


代码:

Dim objWord 
As Word.Application
Dim objDoc As Word.Document
Dim objTable As Word.Table
Dim r As Integer, c As Integer
Dim pages1 As Pages
Dim pagecountbefore As Integer

objWord = CreateObject("Word.Application")
objWord.Visible = True
objDoc = objWord.Documents.Add

pagecountbefore = pages1.Count

objTable = objDoc.Tables.Add(objDoc.Bookmarks.Item("\endofdoc").Range, 3, 5)
objTable.Range.ParagraphFormat.SpaceAfter = 6
For r = 1 To 1000
    For c = 1 To 5
         If r = 1 Then
            Select Case c
                Case "1"
                    objTable.Cell(r, c).Range.Text = "PCI DSS Requirements "
                Case "2"
                    objTable.Cell(r, c).Range.Text = "Testing Procedures "
                Case "3"
                    objTable.Cell(r, c).Range.Text = "In Place"
                Case "4"
                    objTable.Cell(r, c).Range.Text = "Not in Place"
                Case "5"
                    objTable.Cell(r, c).Range.Text = "Target Date/ Comments"
            End Select
        Else
            If pagecountbefore<pages1.Count Then
                pagecountbefore = pages1.Count
                Select Case c
                    Case "1"
                        objTable.Cell(r, c).Range.Text = "PCI DSS Requirements "
                    Case "2"
                        objTable.Cell(r, c).Range.Text = "Testing Procedures "
                    Case "3"
                        objTable.Cell(r, c).Range.Text = "In Place"
                    Case "4"
                        objTable.Cell(r, c).Range.Text = "Not in Place"
                    Case "5"
                        objTable.Cell(r, c).Range.Text = "Target Date/Comments"
                End Select
            Else
              'print resultrs from database
            End If
        End If
    Next
Next

2 个答案:

答案 0 :(得分:0)

您可以每次检查单词文档的页数,然后创建一个if子句,然后执行某些操作。

您可以使用以下网址对页面进行计数:

myPageObject.Count //returns int

然后在每次写入数据时检查新页面。

//Writing Data 
if(pagecountbefore < myObjectPage.Count){
    //Do Something 
}

或者你可以用while子句来做:

  while(pagecountbefore == myObjectPage.Count){
       //Do Something 
  }

答案 1 :(得分:0)

为什么不这样做;在插入所有行后,只需将表的第一行设置为标题行即可 objTable.Rows(1).HeadingFormat = True

如果然后显示在每个页面的顶部,表格跨越