Automation of Microsoft Word from Excel:   Return to List

' There must be a reference to the current Word library from TOOLS | REFERENCES within the VBE
' Create_Word_Report uses CreateObject to get the Word 97 Automation object. ' Create_Word_Report then proceeds to create a report in a Word document, pasting Excel worksheet data and an Excel chart into the document.

Public Sub Create_Word_Report()

' Because this module relies on late binding, it is
' necessary to declare constants to be used with PowerPoint.

    Const wdWindowStateMaximize As Integer = 1
    Const wdNormalView As Integer = 1
    Const wdAlignParagraphCenter As Integer = 1
    Const wdAnimationShimmer As Integer = 6
    Const wdPasteMetafilePicture As Integer = 3
    Const wdInLine As Integer = 0
    Const wdPageFitFullPage As Integer = 1
    Const wdGoToAbsolute As Integer = 1
    Const wdGoToLine As Integer = 3

    Dim WordDoc As Object
    Set WordApp = CreateObject("Word.Application")
    With WordApp
        .Visible = True
        .WindowState = wdWindowStateMaximize
        .Documents.Add
        Set WordDoc = .ActiveDocument
    End With
    WordDoc.ActiveWindow.View = wdNormalView
    With WordApp.Selection
        .InsertAfter "Wellington Cycle Works"
        .InsertParagraphAfter
        .InsertAfter "Sales Report"
        .InsertParagraphAfter
        .InsertAfter "Presented below are sales results for the " _
            & "current year:"
        .InsertParagraphAfter
        .MoveRight
    End With
    With WordDoc.Paragraphs(1).Range
        .ParagraphFormat.Alignment = wdAlignParagraphCenter
        With .Font
            .Name = "Arial"
            .Size = 20
            .Bold = True
            .Animation = wdAnimationShimmer
        End With
    End With
    With WordDoc.Paragraphs(2).Range
        With .ParagraphFormat
            .SpaceAfter = 12
            .Alignment = wdAlignParagraphCenter
        End With
        With .Font
            .Name = "Arial"
            .Size = 14
        End With
    End With
    WordDoc.Paragraphs(3).Range.ParagraphFormat.SpaceAfter = 30
    Range("SalesTable").Copy
    With WordApp.Selection
        .Paste
        .TypeParagraph
    End With
    Worksheets("Sheet1").ChartObjects("SalesChart").Copy
    With WordApp
        .Selection.PasteSpecial Link:=False, _
            DataType:=wdPasteMetafilePicture, _
            Placement:=wdInLine, DisplayAsIcon:=False
        .Selection.ParagraphFormat.Alignment = _
            wdAlignParagraphCenter
        .Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, _
            Count:=1
    End With
End Sub

See also:
Automating Database Compaction (every 5 times)
Automating Multiple Applications
Automating Access (Reports)
Automating Internet Explorer from Excel
Outlook Automation from within Excel
Powerpoint Automation (complex!) from within Excel
Automating Outlook (send email via code)
Code (via Automation) to Import Tab-Delimited File into Excel
Determining File Association (uses API)
Excel chart creation through Automation
Outlook Automation
Automation of Word (getting or starting Word)



Note to Webmaster