15.5 Using the fundamental operations

Using the fundamental operations

Once we ve defined all these basic operations, we can use them instead of calling the servers directly. Listing 1 shows a program that opens Word, creates a new document, puts a little text into it, saves it, and closes Word. The program does everything behind the scenes with no user interface at all (other than the sound of the hard drive working). To check the results, open Word and open Sample.DOC (stored in the temporary files directory specified by SYS(2023)). The program is included in the Developer Download files available at www.hentzenwerke.com as UseWordWrapper.PRG.

Listing 1. Using a wrapper class. This program uses the Word wrapper class to open Word, create a document, and save it. The only code in this program that talks directly to Word is the two lines that actually put text in the document. Everything else goes through the wrapper class.

* Demonstrate the Word wrapper class

#DEFINE CR CHR(13)

LOCAL oWord

* Open Word

oWord = NewObject("cusWord","automation")

WITH oWord

.OpenServer()

IF NOT .IsServerOpen()

WAIT WINDOW "Couldn't open Word."

RETURN .F.

ENDIF

* Create a document

.NewDocument()

* Check whether document is open

IF .IsDocOpen()

* Put some text in the new document

WITH .oDocument.Range()

.InsertAfter("This is our new Word document." + CR)

.InsertAfter("Only these lines directly reference Word methods." + CR)

ENDWITH

* Save the new document

.SaveDocument(,SYS(2023) + "\sample.doc")

ELSE

WAIT WINDOW "Couldn't create new document"

ENDIF

* Close Word

.CloseServer()

ENDWITH

RETURN

Because OpenServer and NewDocument both return logical values to indicate success or failure, this code could actually be condensed somewhat. The whole process of opening the server and creating a new document could be reduced to a single line:

IF .OpenServer() and .NewDocument()

We didn t do it that way, for two reasons. First, we wanted to demonstrate more of the methods. In fact, in a longer example, you re likely to use IsServerOpen and, especially, IsDocOpen quite a bit. Second, combining those two methods into one line prevents you from providing specific error messages. All you know is that the process failed. Since the wrapper class generates error messages, that may be sufficient. (In fact, if your version of the wrapper class provides messages from TellUser, you may not want to include any error messages in the program that uses the class.)

Notice that the oWord variable here refers not to the Word Automation object itself as in the examples in previous chapters, but to the cusWord wrapper object.

While this example doesn t seem that much shorter than those in earlier chapters, realize that the big difference is that this version includes a lot of error handling behind the scenes.

 

Copyright 2000 by Tamar E. Granor and Della Martin All Rights Reserved



Microsoft Office Automation with Visual FoxPro
Microsoft Office Automation with Visual FoxPro
ISBN: 0965509303
EAN: 2147483647
Year: 2000
Pages: 128

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net