Open

Syntax

Open Name$ For Mode$ As [#]FileNumber

Remarks

Opens a sequential file for input or output of text. You can use sequential files to supply values for macro variables or to store data generated by macros.

Sequential files, which are opened with Open and closed with Close, are not displayed in document windows. Although you can use Open to open any file, Open and Close are intended to be used with text files. For more information about sequential files, see Chapter 9, "More WordBasic Techniques," in Part 1, "Learning WordBasic."

Argument

Explanation

Name$

The name of the file to open

Mode$

The mode in which the file is opened:

Input Opens the text file so that data may be read from the file into the macro. When you open a file for input, you can use Read, Input, Line Input, and Input$() instructions to read from the file. If Name$ does not exist, an error occurs.

Output Opens the text file so that data may be written from the macro to the file. When you open a file for output, you can use Write and Print instructions to write to the file. If Name$ does not exist, Word creates it. If Name$ already exists, Word deletes its contents.

Append Opens the text file so that data may be written from the macro to the file. When you open a file to append, you can use Write and Print instructions to write to the file. The existing contents of the file remain and the information you write is added to the end of the file. If Name$ does not exist, Word creates it.

FileNumber

A number to assign the file, from 1 through 4


Example

This macro saves a rich-text format (RTF) copy of each document listed in the text file CONVERT.TXT. The Open instruction opens CONVERT.TXT for input as a sequential file. The instructions in the While¼Wend loop run once for each document, assigning the filename listed next in CONVERT.TXT to curdoc$, opening the document, saving an RTF copy, and then closing the document.


Sub MAIN
Open "CONVERT.TXT" For Input As #1
While Not Eof(1)
    Line Input #1, curdoc$
    FileOpen .Name = curdoc$
    FileSaveAs .Name = curdoc$, .Format = 6
    FileClose 2
Wend
Close #1    
End Sub

See Also

Close, Eof(), Input, Input$(), Line Input, Lof(), Print, Read, Seek, Write