WD: WordBasic Macro: Displaying a List of Files from a Directory |
Q117873
This article demonstrates the Microsoft WordBasic Files$() function. This function can be used to assemble a list of file names within a directory. The Files$() function returns the first file name that matches the specified file specification (for example: *.* or *.doc). If you specify a file specification on the first iteration and then omit it thereafter, you can generate a list of files.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please see the following page on the World Wide Web:
http://www.microsoft.com/partner/referral/For more information about the support options available from Microsoft, please see the following page on the World Wide Web:
http://support.microsoft.com/directory/overview.aspThe following sample macro inserts the file names from the current directory into a new document window:
Sub MAIN
FileNewDefault
doc$ = Files$("*.*")
While doc$ <> ""
Insert doc$
InsertPara
doc$ = Files$()
Wend
End Sub
To insert only the file names with a ".doc" extension, change "*.*" to
"*.doc" in the above example.
Sub MAIN
REM Determine how many files are in the current directory.
count = - 1
temp$ = Files$("*.*")
While temp$ <> ""
count = count + 1
temp$ = Files$()
Wend
If count > - 1 Then
Dim List$(count) 'dimension array for file names
Else
MsgBox "There are no files in the current directory."
Goto theend
EndIf
REM Extract the file name from the full path string.
REM Assign the file names to elements in the List$() array.
doc$ = Files$("*.*")
For i = 0 To count
List$(i) = FileNameInfo$(doc$, 3)
doc$ = Files$()
Next i
REM Sort the array of file names and display them in a list box.
SortArray List$()
Begin Dialog UserDialog 396, 154, "Microsoft Word"
OKButton 254, 88, 88, 21
CancelButton 254, 113, 88, 21
ListBox 18, 21, 215, 114, List$(), .ListBox1
Text 16, 6, 175, 13, "Files in Current Directory:", .Text1
End Dialog
Dim dlg As UserDialog
n = Dialog(dlg)
theend: ' NOTE: This line must be left aligned.
End Sub
Note to Macintosh users: The third line of the macro which reads
doc$ = Files$("*.*")
should be replaced with the following line
doc$ = Files$(MacID$("<TYPE>"))
where <TYPE> is the file type. For example: "TEXT", "W6BN", etc.
"Microsoft Word Developer's Kit," version 6.0, pages 128-130
Additional query words: Files$() Array List File Name FileName print
Keywords : kbmacro wordnt kbmacroexample winword ntword macword word6 word7 word95
Issue type : kbhowto
Technology : kbWordSearch kbWordWSearch kbWordMSearch
|
Last Reviewed: December 29, 2000 © 2001 Microsoft Corporation. All rights reserved. Terms of Use. |