Page 489 - Microsoft Office Excel 2003 Programming Inside Out
P. 489
Excel and Other Office Applications
Table 21-2. Excel Objects and Their Functions
Top-Level Office Object Class Name
Application object CreateObject(“Excel.Application”)
Workbook object CreateObject(“Excel.Sheet”)
Chart object CreateObject(“Excel.Chart”)
The following procedure was created in Word 2003. A reference was included to reference the
Excel Object Library. To execute this procedure, you must add the Excel reference, enter the
procedure into Word, and then create a new workbook in the root directory of your C: drive
named NewExcelWbk.xls.
Sub OpenWriteExcelWbkContents()
Dim xlApp As Excel.Application
Dim xlWbk As Excel.Workbook
Dim tString As String, r As Long
Documents.Add
Set xlApp = CreateObject("Excel.Application”)
Set xlWbk = Excel.Workbooks.Open("C:\NewExcelWbk.xls”)
Chapter 21
r=1
With xlWbk.Worksheets(1)
While .Cells(r, 1).Formula <> ""
tString = Cells(r, 1).Formula
With ActiveDocument.Content
.InsertAfter "Contents of file: " & xlWbk.Name
.InsertParagraphAfter
.InsertParagraphAfter
.InsertAfter tString
.InsertParagraphAfter
End With
r=r+1
Wend
End With
xlWbk.Close
xlApp.Quit
Set xlWbk = Nothing
Set xlApp = Nothing
End Sub
The previous example created a basic Excel workbook. Now that you can create a basic work-
book, you can copy data and a chart into a Word document. One such workbook is shown in
Figure 21-5.
463
Part 6: Excel and the Outside World: Collaborating Made Easy

