Create MS Word Document From Your Program

Author: Microsoft.

This code will allow you to launch MS Word from your program,
Start a new document and put text inside it, determine the font size
and color,  print or save the document.

There is much more things that you can do with the Word object.
You can find them by looking at the Word object properties.
Simply type objWord.  in the command1_click event, and you
will get a list of all Word object properties.

Preparations

Add 1 Command Button to your form.
Add reference to Word: From VB menu choose Project -> References
then mark the Microsoft Word X.0 Object Library check box and press OK.

Form Code

Private Sub Command1_Click()
Dim objWord As New Word.Application

'-- Show Microsoft Word
'  if you want to hide it, replace
'  the "True" below with "False"

objWord.Visible = True

'-- Add new Document
objWord.Documents.Add

'-- Add text to Document
objWord.Selection.TypeText "This is the text VB-Town.com"

'-- Select all Text
objWord.Selection.WholeStory

'-- Change Font Size
objWord.Selection.Font.Size = 50

'-- uncomment the following line to change the font color
'objWord.Selection.Font.Color = wdColorRed

'-- uncomment the following line if you want
'-- to prompt the user to save the document
' objWord.Documents.Save

'-- uncomment the following line if you want to
'-- print the document
'objWord.PrintOut

Set objWord = Nothing
End Sub

Go Back