Format a Range

After a range is selected, formatting can be applied to it (see Figure 16.7). The following program loops through all the paragraphs of the active document and bolds the first word of each paragraph:

Sub ChangeFormat() Dim wdApp As Word.Application Dim wdRng As Word.Range Dim count As Integer

Set wdApp = GetObject(, "Word.Application")

With wdApp.ActiveDocument

For count = 1 To .Paragraphs.count

Set wdRng = .Paragraphs(count).Range

With wdRng

.Words(1).Font.Bold = True .Collapse End With Next count End With

Set wdApp = Nothing Set wdRng = Nothing End Sub

Figure 16.7

Format the first word of each paragraph in a document.

Wo id, Excel, PowerPoint, Outlook, and Access all use the same VBA language; the only differences between them are is their object models (for example/ Excel has a Woikbooks object. Word has Documents). Any one of these applications can access another application's object model as long as the second application is installed.

To access Word's object library. Excel must establish a link to it. There are two ways of doing this - early binding or late binding. With early binding, the reference to the application object is created when the program is compiled; with late binding, it is created when the program is run.

Note: This chapter is an introduction to accessing Word from Excel; we will not be reviewing Word's entire object model or the object models of other applications Refer to the VBA Object Browser in the appropriate application to learn about other object tnodels.|

0 0

Post a comment