WD: Macro: Displaying a List of Directories

ID: Q121890

The information in this article applies to:

SUMMARY

The Microsoft WordBasic CountDirectories() and GetDirectory$() functions can be used to assemble a list of directory names within the current directory.

For information and examples about how to perform this function in Word 97 for Windows or Word 98 Macintosh Edition, from the Visual Basic for Applications Editor, click the Office Assistant, type "Dir," click Search, and then click to view "Dir."

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If Microsoft Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q120802
   TITLE     : Office: How to Add/Remove a Single Office Program or 
               Component

MORE INFORMATION

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code "as is" without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

The following example macro inserts the directory names from the current directory into a new document window:

   Sub MAIN
      FileNewDefault
      num  = CountDirectories()
      For i = 1 To num
         Insert GetDirectory$(i)
         InsertPara
      Next i
   End Sub

The following example macro determines how many directories stem from the current directory and defines an array named SubDirs$() to hold the directory names:

   Sub MAIN
      REM Define an array for the directory names
      Dim subdirs$(CountDirectories())
      subdirs$(0) = "[..]"
      For i = 1 To CountDirectories()
         subdirs$(i) = LCase$(GetDirectory$(i))
      Next
      REM Sort the array of directories and display them in a list box.
      SortArray subdirs$()
      Begin Dialog UserDialog 396, 154, "Directories Available"
         OKButton 254, 88, 88, 21
         CancelButton 254, 113, 88, 21
         ListBox 18, 21, 215, 114, subdirs$(), .ListBox1
      End Dialog
      Dim dlg As UserDialog
      n = Dialog(dlg)
   End Sub

NOTE: The disk that accompanies the Word Developer's Kit includes a macro that allows you to browse through files in any directory. The macro uses a dynamic dialog box to display a file and directory list similar to the Open dialog box in Word. The macro is named ch05ex06FileBrowser and is located in the Examples.dot file.

REFERENCES

"Microsoft Word Developer's Kit," version 6.0, page 128-130

Additional query words: GetDirectory$() Array Subdirectory CountDirectories

Keywords          : kbmacro wordnt kbmacroexample word8 macword98 winword word97 ntword macword word6 word7 word95 
Version           : WINDOWS:6.0,6.0a,6.0c,7.0,7.0a,97; MACINTOSH:6.0,6.0.1,6.0.1a,98
Platform          : MACINTOSH WINDOWS
Issue type        : kbhowto kbinfo

Last Reviewed: February 5, 1998