Hi Skip,
Welcometo MSDN forums!
You canuse Word Automation to write data intoWord document in VB.NET.
As the following code sample, you can create a table in Word document, then write data into the table with particular Font style.
Code sample: Create a new word document, insert a 3 x 5 table and fill it with specific data, finally save it.
Firstly Add Reference to COM component “Microsoft Word Object Library�into your project.
|
Imports Word = Microsoft.Office.Interop.Word
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Create Word Application
Dim oWord As Word.Application = CreateObject("Word.Application")
' Create new word document
Dim oDoc As Word.Document = oWord.Documents.Add()
oWord.Visible = True
'Insert a 3 x 5 table and fill it with specific data
Dim r As Integer, c As Integer
Dim oTable As Word.Table = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 3, 5)
oTable.Range.ParagraphFormat.SpaceAfter = 6
For r = 1 To 3
For c = 1 To 5
oTable.Cell(r, c).Range.Text = "Row" & r & "Col" & c
Next
Next
'Change Font style: Make the first row bold and italic
oTable.Rows.Item(1).Range.Font.Bold = True
oTable.Rows.Item(1).Range.Font.Italic = True
oTable.Rows.Item(1).Range.Font.Size =12 ' Save this word document
oDoc.SaveAs("C:\myfile.doc", True)
oDoc.Close()
oWord.Application.Quit()
End Sub
End Class
|
The output in C:\myfile.doc will be as below:
|
Row1Col1
|
Row1Col2
|
Row1Col3
|
Row1Col4
|
Row1Col5
|
|
Row2Col1
|
Row2Col2
|
Row2Col3
|
Row2Col4
|
Row2Col5
|
|
Row3Col1
|
Row3Col2
|
Row3Col3
|
Row3Col4
|
Row3Col5
|
More totorials and code samples:
FAQ: How do I use Word Automation to read/write Word document in VB.NET
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/93232b82-ec01-4dec-8680-cd08c2951fce
FAQ: How do I use Excel Automation to read/write Excel document in VB.NET
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/df02c6d2-e1b5-4731-bb04-2674aed789de
Best regards,
Martin Xie
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.