The below code is a basic example on how to create a Microsoft Word document. This is sometimes useful if want to create a report showing your data outside of Excel.
Private Sub CreateWordDoc()
'-----------------------------------------------------------------------------
' Procedure : CreateWordDoc
' Author : Matthew - Zypher.co.uk
' Date : 12/04/2007
' Purpose : Create a MS Word document using earling binding
' Requires a reference to 'Microsoft Word ??.? Object Library'
'-----------------------------------------------------------------------------
'
Dim objWord As Word.Application
Dim doc As Word.Document
'Create Word doc object
Set objWord = CreateObject("Word.Application")
With objWord
' Ensure the MS Word object is visible
.Visible = True
' Add a new word document and save the file prior to adding text
Set doc = .Documents.Add
doc.SaveAs "C:\Your\File\Directory\Filename.doc(x)"
' Or open an existing document
'Set doc = wrdApp.Documents.Open("C:\Foldername\Filename.doc")
End With
'Construct document
With objWord.Selection
' Set the font type
.Font.Name = "Trebuchet MS"
' Set the font size
.Font.Size = 16
' Set the format, depending on the value of i
For i = 1 To 50
Select Case i
Case Is < 10
' Set the font size
.Font.Size = 12
' Set font to bold
.Font.Bold = True
' Align the text to the right of the page
.ParagraphFormat.Alignment = wdAlignParagraphRight
Case Is < 20
' Set the font size
.Font.Size = 8
' Turn off bold
.Font.Bold = False
' Align the text to the right of the page
.ParagraphFormat.Alignment = wdAlignParagraphLeft
Case Is < 30
' Set the font size
.Font.Size = 10
' Turn on bold
.Font.Bold = True
' Align the text to the right of the page
.ParagraphFormat.Alignment = wdAlignParagraphRight
Case Is < 40
' Set the font size
.Font.Size = 6
' Turn off bold
.Font.Bold = False
' Align the text to the center
.ParagraphFormat.Alignment = wdAlignParagraphCenter
Case Else
' do nothing
End Select
' Add text
.TypeText "Here is an example test line, #" & i _
& " - Font size is " & .Font.Size
' Move to the next line
.TypeParagraph
Next i
End With
' Save the file
doc.Save
' Bring the MS Word window to the front
doc.Activate
End Sub
As always, if you have any questions do let us know.