Excel VBA Automate Microsoft Word From Excel
Excel VBA Automate Microsoft Word From Excel
'This example shows how to create a new word document, set page setup properties, add paragraphs,
reference paragraphs by index number, format the paragraphs, with options of saving the new document
in the default or a specific folder, using Early Binding.
'variables declared as a specific object type ie. specific to the application which is being automated:
Dim applWord As Word.Application
Dim docWord As Word.Document
Dim paraWord As Word.Paragraph
'Set the Application object variable to create a new instance of Word:
Set applWord = New Word.Application
'set Page Setup properties - Page Orientation, Page Size & Margins:
With docWord.PageSetup
.LineNumbering.Active = True
.Orientation = wdOrientPortrait
.TopMargin = applWord.InchesToPoints(1)
.BottomMargin = applWord.InchesToPoints(0.75)
.LeftMargin = applWord.InchesToPoints(0.75)
.RightMargin = applWord.InchesToPoints(0.85)
.PageWidth = applWord.InchesToPoints(8)
.PageHeight = applWord.InchesToPoints(11)
.Gutter = applWord.InchesToPoints(0.25)
.GutterPos = wdGutterPosLeft
End With
'The Selection Property returns the the selected area in the document or if nothing is selected it
represents the insertion point.
With applWord.Selection
.Font.Name = "Arial"
.Font.Size = 12
.Font.Bold = True
.Font.Color = wdColorRed
.ParagraphFormat.Alignment = wdAlignParagraphCenter
'set spacing after this paragraph, in points:
.ParagraphFormat.SpaceAfter = 10
End With
'use the TypeText Method to insert text into the new word document.
applWord.Selection.TypeText Text:="Title of New Document"
'use Paragraphs.Add Method to add a new paragraph mark at the end of the document:
docWord.Paragraphs.Add
'Set the paragraph object to the new para added. Paragraphs(IndexNumber) is used to return the
Paragraph object. Use the Count property to determine the number of paragraphs (ie. determine the last
para added) in the collection of Paragraph objects. You can also use the Paragraphs.Last Property to
return the last para viz. Set paraWord = docWord.Paragraphs.Last.
Set paraWord = docWord.Paragraphs(docWord.Paragraphs.count)
paraWord.Range.Text = "I bring to your notice that the bill for the month of September, 2011 for my
mobile number 1234567, seems to be incorrect. I have apparantly been overcharged because there are
some calls which are reflected in the bill which have not actually been made by me."
docWord.Paragraphs.Add
End Sub
Example 2: Automate using Early Binding - Create a new word document and insert a
letter with multiple paras, using built-in word styles.
Refer Image 2:
---------------------------------------------
Image 2
---------------------------------------------
Sub Automate_Word_from_Excel_2()
'Automate Word from Excel, using Early Binding: You must add a reference to the Word Object Library in
Excel (your host application) by clicking Tools-References in VBE, which will enable using Word's
predefined constants. Once this reference is added, a new instance of Word application can be created
by using the New keyword.
'This example (Automate using Early Binding) shows how to create a new word document and insert a
letter with multiple paras, using built-in word styles.
'variables declared as a specific object type ie. specific to the application which is being automated:
Dim applWord As Word.Application
Dim docWord As Word.Document
'Set the Application object variable to create a new instance of Word:
Set applWord = New Word.Application
With .Styles(wdStyleNormal)
.Font.Name = "Arial"
.Font.Size = 10
.Font.Color = wdColorBlue
.ParagraphFormat.LineSpacingRule = wdLineSpaceSingle
End With
With .Styles(wdStyleBodyText)
.Font.Name = "Arial"
.Font.Size = 10
.Font.Color = wdColorGreen
.ParagraphFormat.Alignment = wdAlignParagraphJustify
.ParagraphFormat.LineSpacingRule = wdLineSpaceSingle
.ParagraphFormat.FirstLineIndent = applWord.InchesToPoints(0.5)
End With
'Use the Range.InsertParagraphAfter Method to insert a paragraph mark. The insertion is made after the
specified range. The range expands to increase the new paragraph, after applying this method.
'Use the Range.InsertAfter Method to insert text at the end of a range.
.Range(0).Style = .Styles(wdStyleHeading1)
.Content.InsertAfter "Request for Mobile Bill Correction"
'Insert paragraph marks at the end of the document. A Range object is returned by the Content property.
.Content.InsertParagraphAfter
.Content.InsertParagraphAfter
'set style starting from range after the Heading 1:
.Range(.Characters.count - 2).Style = .Styles(wdStyleHeading2)
'Insert text, use Chr(11) to add new line:
.Content.InsertAfter "The Billing Department" & Chr(11) & "M/s MobilePhone Company" & Chr(11) &
"London Office" & Chr(11) & "UK."
.Content.InsertParagraphAfter
.Content.InsertParagraphAfter
'use the Paragraphs.Last Property to return the last para in the document:
.Paragraphs.Last.Style = .Styles(wdStyleNormal)
.Content.InsertAfter vbTab & "Subject: Correction of Bill for my Mobile Number 1234567."
'Insert paragraph mark at the end of the document. A Range object is returned by the Content property.
.Content.InsertParagraphAfter
.Paragraphs.Last.Style = .Styles(wdStyleNormal)
.Content.InsertAfter "Dear Sir,"
.Content.InsertParagraphAfter
.Paragraphs.Last.Style = .Styles(wdStyleBodyText)
.Content.InsertAfter "I bring to your notice that the bill for the month of September, 2011 for my mobile
number 1234567, seems to be incorrect. I have apparantly been overcharged because there are some
calls which are reflected in the bill which have not actually been made by me."
.Content.InsertParagraphAfter
.Paragraphs.Last.Style = .Styles(wdStyleBodyText)
.Content.InsertAfter "I hereby request you to kindly rectify as above and send me the correct bill so that I
can pay at the earliest. Thanking you in anticipation of a quick response."
.Content.InsertParagraphAfter
.Content.InsertParagraphAfter
.Paragraphs.Last.Style = .Styles(wdStyleNormal)
.Content.InsertAfter "Yours Sincerely,"
.Content.InsertParagraphAfter
.Content.InsertParagraphAfter
.Paragraphs.Last.Style = .Styles(wdStyleNormal)
.Content.InsertAfter "Alan Croft"
.Content.InsertParagraphAfter
End With
End Sub
Example 3: Open an Existing word doc, insert text, format doc and save, and then close
& quit application, using Late Binding.
Sub Automate_Word_from_Excel_3()
'Automate Word from Excel, using Late Binding. You need not add a reference to the Word library in
Excel (your host application), in this case you will not be able to use the Word's predefined constants and
will need to replace them by their numerical values in your code.
'This example shows how to open an existing word doc, insert text, format doc and save, and then close
& quit application, using Late Binding.
'Variables declared as Object type, which can be a reference to any object. Variables cannot be declared
as a specific object type (ie. specific to the application which is being automated) using Word.Application
or Word.Document, because a reference to the Word library has not been added.
Dim applWord As Object
Dim docWord As Object
'set the Application object variable to create a new instance of Word:
Set applWord = CreateObject("Word.Application")
'Insert paragraph mark at the end of the document. A Range object is returned by the Content property.
docWord.Content.InsertParagraphAfter
'insert text at the end of the document:
docWord.Content.InsertAfter "Inserted text at the end of document on " & Now
'A built-in or user-defined style is returned by using Styles(index). Index can be mentioned as the user-
defined style name, or a WdBuiltinStyle constant or index number. You can set style properties like Font,
as follows.
'Set style in the new word document: Note that the Word's predefined constants wdStyleNormal and
wdColorRed are not recognized because reference to the Word library has not been added. In this case
the following code will not run and the style cannot be set with the following code:
'docWord.Styles(wdStyleNormal).Font.Color = wdColorRed
'This example (Automate using Late Binding) shows how to create a new word document, insert a letter
by sourcing data from an excel worksheet, using built-in word styles. This method is particularly useful for
making letters with similar content being addressed and emailed to multiple addresses.
'Variables declared as Object type, which can be a reference to any object. Variables cannot be declared
as a specific object type (ie. specific to the application which is being automated) using Word.Application
or Word.Document, because a reference to the Word library has not been added.
Dim applWord As Object
Dim docWord As Object
Dim ws As Worksheet
Set ws = ActiveWorkbook.Sheets("Sheet3")
'Create a new instance of the Word application, if an existing Word object is not available.
'Set the Application object as follows:
On Error Resume Next
Set applWord = GetObject(, "Word.Application")
'if an instance of an existing Word object is not available, an error will occur (Err.Number = 0 means no
error):
If Err.Number <> 0 Then
Set applWord = CreateObject("Word.Application")
End If
'disable error handling:
On Error GoTo 0
With docWord
'A built-in or user-defined style is returned by using Styles(index). Index can be mentioned as the user-
defined style name, or a WdBuiltinStyle constant or index number. You can set style properties like Font,
as follows.
'Set styles for the new word document:
'replaced the Word's built-in constant wdStyleHeading1 with its numerical value -2.
With .Styles(-2)
.Font.Name = "Verdana"
.Font.Size = 13
'replaced the Word's built-in constant wdColorRed with its numerical value 255.
.Font.Color = 255
.Font.Bold = True
'replaced the Word's built-in constant wdAlignParagraphCenter with its numerical value 1.
.ParagraphFormat.Alignment = 1
End With
'replaced the Word's built-in constant wdStyleHeading2 with its numerical value -3.
With .Styles(-3)
.Font.Name = "TimesNewRoman"
.Font.Size = 12
'replaced the Word's built-in constant wdColorBlue with its numerical value 16711680.
.Font.Color = 16711680
.Font.Bold = True
End With
'replaced the Word's built-in constant wdStyleNormal with its numerical value -1.
With .Styles(-1)
.Font.Name = "Arial"
.Font.Size = 10
'replaced the Word's built-in constant wdColorBlue with its numerical value 16711680.
.Font.Color = 16711680
'replaced the Word's built-in constant wdLineSpaceSingle with its numerical value 0.
.ParagraphFormat.LineSpacingRule = 0
End With
'replaced the Word's built-in constant wdStyleBodyText with its numerical value -67 .
With .Styles(-67)
.Font.Name = "Arial"
.Font.Size = 10
'replaced the Word's built-in constant wdColorGreen with its numerical value 32768.
.Font.Color = 32768
'replaced the Word's built-in constant wdAlignParagraphJustify with its numerical value 3.
.ParagraphFormat.Alignment = 3
'replaced the Word's built-in constant wdLineSpaceSingle with its numerical value 0.
.ParagraphFormat.LineSpacingRule = 0
.ParagraphFormat.FirstLineIndent = applWord.InchesToPoints(0.5)
End With
'Use the Range.InsertParagraphAfter Method to insert a paragraph mark. The insertion is made after the
specified range. The range expands to increase the new paragraph, after applying this method.
'Use the Range.InsertAfter Method to insert text at the end of a range.
'replaced the Word's built-in constant wdStyleHeading1 with its numerical value -2.
.Range(0).Style = .Styles(-2)
.Content.InsertAfter "Request for Mobile Bill Correction"
'Insert paragraph marks at the end of the document. A Range object is returned by the Content property.
.Content.InsertParagraphAfter
.Content.InsertParagraphAfter
'set style starting from range after the Heading 1:
'replaced the Word's built-in constant wdStyleHeading2 with its numerical value -3.
.Range(.Characters.count - 2).Style = .Styles(-3)
'Insert text, use Chr(11) to add new line:
.Content.InsertAfter ws.Range("A7") & Chr(11) & ws.Range("B7") & Chr(11) & ws.Range("C7") & Chr(11)
& ws.Range("D7") & Chr(11) & ws.Range("E7") & Chr(11) & ws.Range("F7")
.Content.InsertParagraphAfter
.Content.InsertParagraphAfter
'use the Paragraphs.Last Property to return the last para in the document:
'replaced the Word's built-in constant wdStyleNormal with its numerical value -1.
.Paragraphs.Last.Style = .Styles(-1)
.Content.InsertAfter vbTab & ws.Range("A1")
'Insert paragraph mark at the end of the document. A Range object is returned by the Content property.
.Content.InsertParagraphAfter
'replaced the Word's built-in constant wdStyleNormal with its numerical value -1.
.Paragraphs.Last.Style = .Styles(-1)
.Content.InsertAfter "Dear Sir,"
.Content.InsertParagraphAfter
'replaced the Word's built-in constant wdStyleBodyText with its numerical value -67 .
.Paragraphs.Last.Style = .Styles(-67)
.Content.InsertAfter ws.Range("A3")
.Content.InsertParagraphAfter
'replaced the Word's built-in constant wdStyleBodyText with its numerical value -67 .
.Paragraphs.Last.Style = .Styles(-67)
.Content.InsertAfter ws.Range("A5")
.Content.InsertParagraphAfter
.Content.InsertParagraphAfter
'replaced the Word's built-in constant wdStyleNormal with its numerical value -1.
.Paragraphs.Last.Style = .Styles(-1)
.Content.InsertAfter "Yours Sincerely,"
.Content.InsertParagraphAfter
.Content.InsertParagraphAfter
'replaced the Word's built-in constant wdStyleNormal with its numerical value -1.
.Paragraphs.Last.Style = .Styles(-1)
.Content.InsertAfter "Alan Croft"
.Content.InsertParagraphAfter
End With
End Sub