SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 264 | Next

Jim DeMarco

"Pro Excel 2007 VBA"

Quit
Set m_oWordApp = Nothing
Here we are displaying the Word application and our new document. The message box is
here to stop the code from running so we can navigate over to the Word window (if it??™s not
already the active window). Once we click the message box to close it, our cleanup code runs
and shuts down the instance of Word and kills the Word Application object.
5. Save your work.
The code so far should look like Listing 8-4.
Listing 8-4.MakeWordDoc Subroutine
Sub MakeWordDoc()
Dim i As Integer
Dim sTitle As String
Dim sBody As String
Set m_oWordApp = CreateObject("Word.Application")
Set m_oWordDoc = m_oWordApp.Documents.Add
sTitle = GetTitle
sBody = GetTitleBody
With m_oWordApp
.Selection.Style = .ActiveDocument.Styles("Heading 1")
.Selection.TypeText sTitle
.Selection.TypeParagraph
.Selection.TypeText sBody
End With
m_oWordApp.Visible = True
MsgBox "word s/b open now"
m_oWordApp.Quit
Set m_oWordApp = Nothing
End Sub
Let??™s run the code we??™ve generated so far and see what we get.
CHAPTER 8 n OFFICE INTEGRATION 293
6. Put your cursor anywhere in the MakeWordDoc subroutine.
7. Click the Run button on the VBE toolbar or press F5.
nNote Depending on the speed of your computer, the code may run for a bit before Word appears.
Remember, you are loading an actual instance of Word, so this will take about as long as Word normally
takes to load on your machine.
Figure 8-4 shows the Word instance displaying the report heading in Heading 1 style and
descriptive text in the default (Normal) style formatting.


Pages:
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276