Visual Basic Development Bookmark and Share   
 Home > Visual Basic Interop and Upgrade > Visual Basic 2008 - change font in a created Word document
 

Visual Basic 2008 - change font in a created Word document

I'm writing a program that will read from an Excel spreadsheet - reading state names and cities in the state. I want to write to the Word document. I know how to create a Word document in VB8, but I can't figure out how to change fonts. For example, I want to create a list with the state name in a large font, then change the font and list the city names. Then when I encounter another state I increase the font.

Maine 'I want to make this a size 16 font
Bar Harbor 'The city names would be a size 10 font
Bangor
Michigan'Now back to a size 16 font
Ann Arbor' Size 10 font for the city names
Lansing

Is it possible to flip-flop font size and if so can you give me an idea.
Skip Christensen  Thursday, November 12, 2009 6:11 PM

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.
Martin Xie - MSFT  Thursday, November 19, 2009 9:17 AM

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.
Martin Xie - MSFT  Thursday, November 19, 2009 9:17 AM

You can use google to search for other answers

Custom Search

More Threads

• Runtime load sheridan control SSDBCombo in VB 5.0. Do it thro' DLL and link the DLL to all VB5.0 forms.
• Urgent : Out Of Memory with interop usercontrol in VB6
• VB program reading Excel spreadsheet and writing to SQL DB
• imapi2 events
• Best method to expose a vb.net 2005 form to a vb6 program.
• Create Outlook advanced find in VS2003
• Realtimedata server for excel in VB.NET (VB Express 2008)
• VB6 interop to .NET webservice client
• 'AddressOf' expression cannot be converted to 'integer' because 'integer' is not a delegate type.
• Help with Object reference not set to an instance of an object.