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

Excel and Other Office Applications

                                 Workbooks.Add
                                 r=3
                                 Set wrdApp = CreateObject("Word.Application”)
                                 Set wrdDoc = GetObject("C:\GSC\Correspondence\Spring Promotion.doc”)
                                 With wrdDoc
                                    For i = 1 To .Paragraphs.Count
                                        Set tRange = .Range(Start:=.Paragraphs(i).Range.Start, _
                                            End:=.Paragraphs(i).Range.End)
                                        tString = tRange.Text
                                        tString = Left(tString, Len(tString) - 1)
                                        ActiveSheet.Range("A” & r).Formula = tString
                                        r=r+1
                                    Next i
                                 End With
                                 With Range("A1”)
                                    .Formula = "File Contents of: " & wrdDoc.Name
                                    .Font.Italic = True
                                    .Font.Size = 18
                                 End With
                                 wrdDoc.Close
                                 wrdApp.Quit
                                 Set wrdDoc = Nothing
                                 Set wrdApp = Nothing
                                                       Chapter 21
                                 ActiveWorkbook.Saved = True
                             End Sub

                             Inside Out

                             GetObject vs. CreateObject
                             The GetObject and the CreateObject functions both return references to an object, so how
                             do you choose which one to use?
                             The CreateObject function is used to create a new instance of the application. Use this func­
                             tion if the application you need to reference is not currently open. If an instance is already
                             running, a second instance of the application will be initiated. Consider the following exam­
                             ple. The Garden Supply Company has an Excel Workbook that copies data into a Microsoft
                             Word document for the monthly sales reports. To create a new instance of Microsoft Word,
                             you should use the following code:

                             Set xlApp = CreateObject("Word.Application”)

                             The GetObject function is used with an application that is already running or to start an
                             application with a file already loaded. The following example starts Excel and opens
                             Myfile.xls:
                             Set XLBook = GetObject("C:\Myfile.xls”)

                             The object returned in XLBook is a reference to the Workbook object.




                                                                                                       459
                                                                        Part 6:  Excel and the Outside World: Collaborating Made Easy
   480   481   482   483   484   485   486   487   488   489   490