vb.net向Word文档添加表格

发布于 2024-11-27 20:41:54 字数 6832 浏览 0 评论 0原文

我正在尝试制作一个应用程序,它将获取输入到程序中的信息并使用 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

I am trying to make an application that will take the information entered into the program and generate a quote sheet using Microsoft word. Right now I am having trouble creating tables in the word document. The user can choose to quote between 1 to 4 different products. If 1 product is quoted, then it will create a 6x3 table. If 2 products are quoted, then it will create a 6x5 table with the 3rd column separating the 2 products. If 3 or 4 products are quoted then it will create another table under the first table and follow the same rules as the first table. The program works perfectly if I quote 3 or 4 products but when I try to quote 1 or 2 products then it results in an error.

I am still very new to programming so my code might look very sloppy. I know my error is probably with how I use my If statements, but here is what I have. Thanks

    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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野侃 2024-12-04 20:41:54

就在我的脑海里,我想说你有很多行和事情。列颠倒了。在所有采用 row & 的方法中列参数,行参数在前(例如 Tables.Add、Cell() 等)。

Just off the top of my head, I'd say you've got rows & columns reversed. In all the methods that take row & column parameters, the row parameter comes first (e.g. Tables.Add, Cell(), etc.).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文