如何使用 VB.NET 在 word 上创建多个表
How to Create mutiple Tables on word with VB.NET
Dim Linha3 = WordDoc.Paragraphs.Add
Dim Tabela = WordDoc.Tables.Add(Linha3.Range, 7, 5)
Tabela.Range.Font.Name = "Calibri"
Tabela.Range.Font.Size = 8
Tabela.Columns(2).Width = 50
Tabela.Columns(3).Width = 150
Tabela.Columns(4).Width = 80
Tabela.Columns(5).Width = 80
Tabela.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter
Tabela.Cell(1, 4).Merge(Tabela.Cell(1, 5))
我的问题是。我想循环执行此操作,因为我必须创建多个表。问题是,当循环发生时,它给了我一个错误,说像 Columns(2) 已经调整大小,然后加倍它......所以我想我必须像“Tabela2”一样创建但是我怎样才能创建一个动态变量?
Imports Microsoft.Office.Interop
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim objApp = New Word.Application
Dim objDoc As Word.Document
'For New Page = Word.WdBreakType.wdPageBreak
'For New section without a corresponding page break = Word.WdBreakType.wdSectionBreakContinuous
Dim objPageBreak As Object = Word.WdBreakType.wdSectionBreakContinuous
objApp = CreateObject("Word.Application")
objApp.Visible = True
objDoc = objApp.Documents.Add
For i = 1 To 3
CreateTableInWordDocument(objDoc.Bookmarks.Item("\endofdoc").Range, objDoc, objApp)
objDoc.Bookmarks.Item("\endofdoc").Range.InsertBreak(objPageBreak)
Next
End Sub
Private Sub CreateTableInWordDocument(objRange As Word.Range, objDoc As Word.Document, objApp As Word.Application)
Dim objTable As Word.Table
objTable = objRange.Tables.Add(objRange, 7, 5)
objTable.Borders.Enable = True
objTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleDouble
objTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
Dim r As Integer, c As Integer
For r = 1 To 7
For c = 1 To 5
objTable.Cell(r, c).Range.Text = "Row" & r & " Column" & c
Next
Next
objTable.Rows.Item(1).Range.Font.Bold = True
objTable.Rows.Item(1).Range.Font.Italic = True
Me.Close()
End Sub
End Class
您必须在 Windows 桌面应用程序中添加一个 Button1 控件并使用此代码 - 在循环中 For i=1 To 3 - 这里 3 是要添加到 Word 文档中的表格数。
关于添加更多表的事情是。在我的代码中,这里:
Dim Tabela = WordDoc.Tables.Add(Linha3.Range, 7, 5)
“WordDoc.Paragraphs.Add”与我在 WordDoc 上使用的不一样。我在文档的其余部分使用“Linha”而不是 Linha3。我不知道为什么它会有所不同,但这就是给我错误的原因。
所以我只是将其更改为“Linha”,它工作正常,对某些单元格的文本进行了一些自定义编辑。
Dim WordDoc = WordApp.Documents.Add
Dim Linha = WordDoc.Paragraphs.Add
答案是不要再添加 WordDoc.Paragraphs,你应该没问题。
Dim Linha3 = WordDoc.Paragraphs.Add
Dim Tabela = WordDoc.Tables.Add(Linha3.Range, 7, 5)
Tabela.Range.Font.Name = "Calibri"
Tabela.Range.Font.Size = 8
Tabela.Columns(2).Width = 50
Tabela.Columns(3).Width = 150
Tabela.Columns(4).Width = 80
Tabela.Columns(5).Width = 80
Tabela.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter
Tabela.Cell(1, 4).Merge(Tabela.Cell(1, 5))
我的问题是。我想循环执行此操作,因为我必须创建多个表。问题是,当循环发生时,它给了我一个错误,说像 Columns(2) 已经调整大小,然后加倍它......所以我想我必须像“Tabela2”一样创建但是我怎样才能创建一个动态变量?
Imports Microsoft.Office.Interop
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim objApp = New Word.Application
Dim objDoc As Word.Document
'For New Page = Word.WdBreakType.wdPageBreak
'For New section without a corresponding page break = Word.WdBreakType.wdSectionBreakContinuous
Dim objPageBreak As Object = Word.WdBreakType.wdSectionBreakContinuous
objApp = CreateObject("Word.Application")
objApp.Visible = True
objDoc = objApp.Documents.Add
For i = 1 To 3
CreateTableInWordDocument(objDoc.Bookmarks.Item("\endofdoc").Range, objDoc, objApp)
objDoc.Bookmarks.Item("\endofdoc").Range.InsertBreak(objPageBreak)
Next
End Sub
Private Sub CreateTableInWordDocument(objRange As Word.Range, objDoc As Word.Document, objApp As Word.Application)
Dim objTable As Word.Table
objTable = objRange.Tables.Add(objRange, 7, 5)
objTable.Borders.Enable = True
objTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleDouble
objTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
Dim r As Integer, c As Integer
For r = 1 To 7
For c = 1 To 5
objTable.Cell(r, c).Range.Text = "Row" & r & " Column" & c
Next
Next
objTable.Rows.Item(1).Range.Font.Bold = True
objTable.Rows.Item(1).Range.Font.Italic = True
Me.Close()
End Sub
End Class
您必须在 Windows 桌面应用程序中添加一个 Button1 控件并使用此代码 - 在循环中 For i=1 To 3 - 这里 3 是要添加到 Word 文档中的表格数。
关于添加更多表的事情是。在我的代码中,这里:
Dim Tabela = WordDoc.Tables.Add(Linha3.Range, 7, 5)
“WordDoc.Paragraphs.Add”与我在 WordDoc 上使用的不一样。我在文档的其余部分使用“Linha”而不是 Linha3。我不知道为什么它会有所不同,但这就是给我错误的原因。 所以我只是将其更改为“Linha”,它工作正常,对某些单元格的文本进行了一些自定义编辑。
Dim WordDoc = WordApp.Documents.Add
Dim Linha = WordDoc.Paragraphs.Add
答案是不要再添加 WordDoc.Paragraphs,你应该没问题。