vb.net将表添加到Word文档

时间:2011-08-03 15:16:18

标签: vb.net ms-word

我正在尝试创建一个应用程序,它将输入的信息输入程序并使用Microsoft word生成报价表。现在我在word文档中创建表时遇到问题。用户可以选择引用1到4种不同的产品。如果引用了1个产品,那么它将创建一个6x3表。如果引用了2个产品,那么它将创建一个6x5表,第3列将2个产品分开。如果引用了3个或4个产品,那么它将在第一个表下创建另一个表,并遵循与第一个表相同的规则。如果我引用3或4个产品,但当我尝试引用1或2个产品时,该程序可以正常工作,那么它会导致错误。

我仍然是编程的新手,所以我的代码可能看起来很草率。我知道我的错误可能与我如何使用我的If语句有关,但这就是我所拥有的。感谢

    Dim QuoteNumber As String
    Dim RowsNeeded As Integer

    QuoteNumber = NumberOfProducts.Text

    'Choosing the number of Columns Needed from number of carpets quoted
    Select Case QuoteNumber
        Case "1"
            RowsNeeded = 2
        Case "2"
            RowsNeeded = 5
        Case "3"
            RowsNeeded = 5
        Case "4"
            RowsNeeded = 5
    End Select

    Dim PricePer As String = "Product", PerLabor As String = "Labor"
    Dim PShipping As String = "Shipping"
    Dim PUpgrade As String = "Upgrade", PRegular As String = "Regular"

    ProductTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 6, RowsNeeded)
    ProductTable.Range.Font.Bold = False
    ProductTable.Range.Font.Underline = False

    ProductTable.Cell(1, 1).Range.Font.Underline = True 'Underline the Product

    'Only 1 Product being Quoted
    ProductTable.Cell(1, 1).Range.Text = cboxProductColor.SelectedItem.ToString
    ProductTable.Cell(2, 1).Range.Text = PricePer
    ProductTable.Cell(3, 1).Range.Text = PerLabor
    ProductTable.Cell(4, 1).Range.Text = PShipping
    ProductTable.Cell(5, 1).Range.Text = PUpgrade
    ProductTable.Cell(6, 1).Range.Text = PRegular

    ProductTable.Cell(2, 2).Range.Text = txtProductPrice.Text
    ProductTable.Cell(3, 2).Range.Text = txtProductLabor.Text
    ProductTable.Cell(4, 2).Range.Text = txtShippingPrice.Text
    ProductTable.Cell(5, 2).Range.Text = txtUpgrade.Text
    ProductTable.Cell(6, 2).Range.Text = txtRegular.Text
    ProductTable.Columns.Item(1).Width = oWord.InchesToPoints(1.8)   'Change width of columns 1 & 2
    ProductTable.Columns.Item(2).Width = oWord.InchesToPoints(0.8)

    '2 Products being Quoted
    If RowsNeeded = 5 Then

        'Spacing Column 3
        ProductTable.Columns.Item(3).Width = oWord.InchesToPoints(1)

        ProductTable.Cell(1, 4).Range.Font.Underline = True 'Underline the Product

        ProductTable.Cell(1, 4).Range.Text = cboxProductColor2.SelectedItem.ToString
        ProductTable.Cell(2, 4).Range.Text = PricePer
        ProductTable.Cell(3, 4).Range.Text = PerLabor
        ProductTable.Cell(4, 4).Range.Text = PShipping
        ProductTable.Cell(5, 4).Range.Text = PUpgrade
        ProductTable.Cell(6, 4).Range.Text = PRegular

        ProductTable.Cell(2, 5).Range.Text = txtProductPrice2.Text
        ProductTable.Cell(3, 5).Range.Text = txtProductLabor2.Text
        ProductTable.Cell(4, 5).Range.Text = txtShippingPrice2.Text
        ProductTable.Cell(5, 5).Range.Text = txtUpgrade2.Text
        ProductTable.Cell(6, 5).Range.Text = txtRegular2.Text
        ProductTable.Columns.Item(4).Width = oWord.InchesToPoints(1.8)   'Change width of columns 1 & 2
        ProductTable.Columns.Item(5).Width = oWord.InchesToPoints(0.8)
        ProductTable.Range.ParagraphFormat.SpaceAfter = 1

    End If

    'Using a line break before my next table
    oLineBreak = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
    oLineBreak.Range.InsertParagraphBefore()
    oLineBreak.Range.Text = ""
    oLineBreak.Format.SpaceAfter = 0.5
    oLineBreak.Range.InsertParagraphAfter()

    '3 Products being quoted
    'I'm creating a new table for Products 3 and 4
    If NumberOfProducts.SelectedIndex = 2 Or 3 Then
        Dim ProductTable2 As Word.Table

        'Display the Product Style/Color
        Dim QuoteNumber2 As String
        Dim RowsNeeded2 As Integer

        QuoteNumber2 = NumberOfProducts.Text

        'Choosing the number of Columns Needed from number of Products quoted
        Select Case QuoteNumber2
            Case "1"
                RowsNeeded2 = 1
            Case "2"
                RowsNeeded2 = 1
            Case "3"
                RowsNeeded2 = 2
            Case "4"
                RowsNeeded2 = 5
        End Select

        Dim PricePer2 As String = "Product", PerLabor2 As String = "Labor"
        Dim PShipping2 As String = "Shipping"
        Dim PUpgrade2 As String = "Upgrade", PRegular2 As String = "Regular"

        ProductTable2 = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 6, RowsNeeded2)
        ProductTable2.Range.Font.Bold = False
        ProductTable2.Range.Font.Underline = False

        ProductTable2.Cell(1, 1).Range.Font.Underline = True 'Underline the Product Color

        'This is for the 3rd Product
        ProductTable2.Cell(1, 1).Range.Text = cboxProductColor3.SelectedItem.ToString
        ProductTable2.Cell(2, 1).Range.Text = PricePer2
        ProductTable2.Cell(3, 1).Range.Text = PerLabor2
        ProductTable2.Cell(4, 1).Range.Text = PShipping2
        ProductTable2.Cell(5, 1).Range.Text = PUpgrade2
        ProductTable2.Cell(6, 1).Range.Text = PRegular2

        ProductTable2.Cell(2, 2).Range.Text = txtProductPrice3.Text
        ProductTable2.Cell(3, 2).Range.Text = txtProductLabor3.Text
        ProductTable2.Cell(4, 2).Range.Text = txtShippingPrice3.Text
        ProductTable2.Cell(5, 2).Range.Text = txtUpgradeTotal3.Text
        ProductTable2.Cell(6, 2).Range.Text = txtRegular3.Text
        ProductTable2.Columns.Item(1).Width = oWord.InchesToPoints(1.8)   'Change width of columns
        ProductTable2.Columns.Item(2).Width = oWord.InchesToPoints(0.8)

        '4 Products being Quoted
        If RowsNeeded2 = 5 Then

            'Spacing Column 3
            ProductTable2.Columns.Item(3).Width = oWord.InchesToPoints(1)

            ProductTable2.Cell(1, 4).Range.Font.Underline = True 'Underline the Product Color

            ProductTable2.Cell(1, 4).Range.Text = cboxProductColor4.SelectedItem.ToString
            ProductTable2.Cell(2, 4).Range.Text = PricePer2
            ProductTable2.Cell(3, 4).Range.Text = PerLabor2
            ProductTable2.Cell(4, 4).Range.Text = PShipping2
            ProductTable2.Cell(5, 4).Range.Text = PUpgrade2
            ProductTable2.Cell(6, 4).Range.Text = PRegular2

            ProductTable2.Cell(2, 5).Range.Text = txtProductPrice4.Text
            ProductTable2.Cell(3, 5).Range.Text = txtProductLabor4.Text
            ProductTable2.Cell(4, 5).Range.Text = txtShippingPrice4.Text
            ProductTable2.Cell(5, 5).Range.Text = txtUpgrade4.Text
            ProductTable2.Cell(6, 5).Range.Text = txtRegular4.Text
            ProductTable2.Columns.Item(4).Width = oWord.InchesToPoints(1.8)   'Change width of columns 1 & 2
            ProductTable2.Columns.Item(5).Width = oWord.InchesToPoints(0.8)
            ProductTable2.Range.ParagraphFormat.SpaceAfter = 1

1 个答案:

答案 0 :(得分:0)

就在我的头顶,我会说你有行和列反转。在所有采取行和方法的方法中列参数,行参数首先出现(例如Tables.Add,Cell()等)。