Microsoft KB Archive/78334

{|
 * width="100%"|

Excel: Macro Statement to Test If File Is Open or Available

 * }

-

The information in this article applies to:


 * Microsoft Excel for Windows, version 3.0, 4.0, 4.0a, 5.0
 * Microsoft Excel for the Macintosh, version 3.0, 4.0, 5.0
 * Microsoft Excel for OS/2, version 3.0

-

SUMMARY
The following macro statement will check to see if a particular file is currently open in Microsoft Excel:

=OR(&quot;filename&quot;=DOCUMENTS(3))

MORE INFORMATION
The DOCUMENTS function returns an array of all open filenames. To evaluate this statement, Microsoft Excel converts the single value &quot;filename&quot; into an array of the same size as the one returned by the DOCUMENTS function. Each element in the first array is then evaluated against the corresponding element in the second array. If a match is found, the OR function returns TRUE, indicating that &quot;filename&quot; is currently open.

Example
The following macro statement will test to see if the file TEST.XLS is open. If TEST.XLS is not currently open, the macro will open it. Otherwise, TEST.XLS is activated.

=IF(OR(&quot;test.xls&quot;=DOCUMENTS(3)),ACTIVATE(&quot;test.xls&quot;),OPEN(&quot;test.xls&quot;))

Note that if the file TEST.XLS is not in the current directory, you must include the entire path for the file as the argument to the OPEN command.

This method can also be used to test if a particular file is located in a directory by replacing the DOCUMENTS function with the FILES function. For example,

=IF(OR(&quot;myfile.xls&quot;=FILES(&quot;c:\excel\*.xls&quot;)),open(&quot;myfile.xls&quot;))

will look for the file &quot;myfile.xls&quot; in the \Excel directory. If it exists, Microsoft Excel will open the file.