WD: Macro: Displaying a List of Directories

Last reviewed: February 3, 1998
Article ID: Q121890
The information in this article applies to:
  • Microsoft Word for Windows, versions 6.0, 6.0a, 6.0c
  • Microsoft Word for Windows NT, version 6.0
  • Microsoft Word for Windows 95, versions 7.0, 7.0a
  • Word for the Macintosh, versions 6.0, 6.0.1
  • Microsoft Word 98 Macintosh Edition
  • Microsoft Word 97 for Windows

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 : kbmacroexample macword ntword winword word6 word7 word8 word95 word97 wordnt macword98 kbmacro
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


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 3, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.