Page 487 - Microsoft Office Excel 2003 Programming Inside Out
P. 487

Excel and Other Office Applications

                                 Set appWord = GetObject(, "Word.Application”)
                                 appWord.Visible = True
                                 appWord.Activate

                                 Set doc = appWord.ActiveDocument
                                 appWord.ShowMe
                                 With doc
                                    doc.Activate
                                    doc.Application.Selection.TypeText "This file was created " & Date
                                 End With
                             End Sub

                    Creating a New Word Document

                             There will be times when you want to create a new file instead of working with an existing
                             Word document. To accomplish this task, you need to have an open instance of Word and
                             then you’ll create a new document.

                             The following example uses early binding, so the code to create a new document is the same   Chapter 21
                             as if you were creating a new document from Word. Before you test this procedure, add the
                             Word Object Library to your procedure.

                             Sub CopyFromExcelToNewWordDocument()
                                 Dim wdApp As Word.Application
                                 ThisWorkbook.Sheets("Table”).Range("A1:B6”).Copy

                                 On Error Resume Next
                                 Set wdApp = GetObject(, "Word.Application”)
                                 If wdApp Is Nothing Then
                                    Set wdApp = GetObject("", "Word.Application”)
                                 End If
                                 On Error GoTo 0
                                 With wdApp
                                    .Documents.Add
                                    .Visible = True
                                 End With
                                 With wdApp.Selection
                                    .EndKey unit:=wdStory
                                    .TypeParagraph
                                    .Paste
                                 End With
                                 Set wdApp = Nothing
                             End Sub






                                                                                                       461
                                                                        Part 6:  Excel and the Outside World: Collaborating Made Easy
   482   483   484   485   486   487   488   489   490   491   492