4.3 Managing documents

Managing documents

The methods for creating, opening, saving, and closing documents are fairly straightforward. The only complication is in what object provides which method. The methods for creating and opening a document belong to the Documents collection. The methods for saving and closing a document belong to the Document object. Although it seems confusing at first glance, this actually makes sense because you don t have a Document object to work with at the time you create or open a document. But when it comes time to save or close it, a document reference is available.

To open an existing document, use the Open method of the Documents collection. Open has many parameters, but most of the time, the only one that matters is the first: the name and path of the file to open. The Word server doesn t recognize VFP s search path, so you usually need to provide the full path to the document, like this:

oDocument = oWord.Documents.Open("d:\writing\books\automation\chapter4.doc")

If it s successful, Open returns an object reference to the newly opened document. If the specified document doesn t exist, an error is generated, and nothing is returned. (This example, like the others in this chapter and the next two, assumes that you have an instance of Word running, with oWord holding a reference to it.)

To create a new document, use the Add method, which has only two, optional, parameters. The important one is the first, which indicates the path to the template on which the new document should be based. If it s omitted, the new document is based on the Normal template. (Templates are discussed in more detail in Chapter 5, "Intermediate Word.")

Like Open, Add returns a reference to the newly created document. This line creates a new document based on a template called "OfficeFax":

oDocument = oWord.Documents.Add( ;

"C:\WINNT\Profiles\Tamar\Application Data\Microsoft\OfficeFax.DOT")

As with the filename in Open, the full path to the template is needed. See the section "Document templates" in Chapter 5 for information on where Word installs and keeps templates.

When you re finished working with a document, two methods are available to save it. Both methods belong to the Document object. The Save method saves the document back to its current file; if it s never been saved, a Save As dialog box appears. The SaveAs method lets you specify the filename (and a lot of other stuff) without seeing a dialog, which is usually what you want in Automation code.

If the currently active document has already been saved, this line resaves the document without user intervention:

oWord.ActiveDocument.Save()

To save the document to a different file, or to save a document for the first time without the user specifying a filename, call SaveAs and pass the filename, like this:

oWord.ActiveDocument.SaveAs("D:\Documents\ThisIsNew.DOC")

Be careful. When you pass a filename to SaveAs, it overwrites any existing file without prompting. (Of course, SaveAs, like Word s other methods, doesn t respect VFP s SET SAFETY setting, since it s not running inside VFP.)

You can check whether the document has been saved by testing its Saved property. If Saved is .T., the document is unchanged. This can be the case either because you ve already saved the document and haven t changed it since, or because it s a new document and hasn t yet been modified.

In addition, the Name and FullName properties give you an alternative way to check whether a document has ever been saved. When you create a new document, Word assigns a name in the form "Documentn," where n is a number. When you save the document, you can give it a more meaningful name, as well as specify the file path. The Name property of the Document contains just the file stem with no path or extension. The FullName property contains the complete filename, including the path and extension. However, before the file is saved for the first time, both Name and FullName contain the same string the initial document name assigned by Word. You can write code like this to figure out whether to use Save or SaveAs:

WITH oWord.ActiveDocument

IF .Name = .FullName

* Prompt user to get a name,

* then:

.SaveAs( cFileName )

ELSE

.Save

ENDIF

ENDWITH

 

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