vb.net将表添加到Word文档 [英] vb.net Adding Tables to Word Document
问题描述
我正在尝试创建一个应用程序,该应用程序将使用输入到程序中的信息并使用Microsoft word生成报价单.现在,我无法在Word文档中创建表.用户可以选择在1至4种不同产品之间报价.如果引用了1个产品,则它将创建一个6x3的表格.如果引用了2个产品,则它将创建一个6x5表格,其中第3列将2个产品分开.如果引用了3个或4个产品,则它将在第一个表下创建另一个表,并遵循与第一个表相同的规则.如果我引用3或4种产品,该程序可以完美运行,但是当我尝试引用1或2种产品时,它将导致错误.
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.
我对编程还是很陌生,所以我的代码看起来很草率.我知道我的错误可能与我使用If语句的方式有关,但这就是我所拥有的.谢谢
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
推荐答案
就在我的头顶上,我说你有行&列颠倒了.在采用行&的所有方法中列参数,行参数排在首位(例如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.).
这篇关于vb.net将表添加到Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!