VB CAD Tips

www.vbcad.com

Open and process all drawings in sub-directories
 
Chapter 19 of the VBA for AutoCAD 2000 book covers batch processing AutoCAD files.  The example on page 19-2 shows how to open each .dwg file in a specified directory.  An individual who has purchased the book wanted to take this to the next level - how to open all drawings in not only the specified directory, but also all of the sub-directories.  This is possible through the use of the Dir function.   However, it does take a fair amount of code.  Another way to accomplish this task is through the use of the File System Oject.  The File System Object contains objects, methods, and properties to simplify working with drives, directories, and files.   Before using any of FSO's calls, you must first add a reference to it.  The File System Object is contained in the Microsoft Scripting Runtime dll.

In the VBA environment, click on the menu: Tools - References.  In the dialog box you are shown, you will see a list of objects you can add as references.  Once added, the objects, methods, and properties of the selected object becomes available.  For this example, we want to scroll down to the "Microsoft Scripting Runtime" item.  If you click on it (not on the check box, but the words), you will see that the actual file name you are referencing is "SCRRUN.DLL".  If you do not see "Microsoft Scripting Runtime" in the Available References list box, you can click the Browse button and look for it.  If it is on your machine, it will be in your System directory.  You can also perform a search for it using Windows Explorer.  Go ahead and select the check box next to "Microsoft Scripting Runtime" and then click the "OK" button in the References dialog box.

Now that you have added the Microsoft Scripting Runtime dll, you have access to the File System Object.

The individual who requested this tip provided a reference to a web site where he first read about FSO's capabilities.  So, believeing in giving credit where credit is due, here is the link:  http://www.vbtechniques.com/ar/fso.asp    The code below has been borrowed and modified from the above link to fit our needs.  Place it in a blank Module.  Make sure that the General Declarations code gets into the General Declarations area of the newly added module.

'General Declarations
Option Explicit
Dim m_objFSO As Scripting.FileSystemObject
'End of General Declarations

Sub RunThisMacro()
   Set m_objFSO = New Scripting.FileSystemObject
   GetAllDwgs "C:\Program Files\Acad2000"
End Sub

Sub GetAllDwgs(strPath As String)
   Dim objFolder As Scripting.Folder
   Dim objFile As Scripting.File
   Dim objSubdirs As Scripting.Folders
   Dim objLoopFolder As Scripting.Folder
   Dim strFileName As String

   Set objFolder = m_objFSO.GetFolder(strPath)
   '
   ' Check files in the root search directory
   '

   For Each objFile In objFolder.Files
      If UCase$(Right$(objFile.ShortPath, 4)) = ".DWG" Then
         strFileName = objFile.Path
         MsgBox "Place code here to run on the file" & vbCr & strFileName
      End If
   Next objFile
   '
   ' Loop through all subdirectories
   '

   Set objSubdirs = objFolder.SubFolders
   For Each objLoopFolder In objSubdirs
      GetAllDwgs objLoopFolder.Path
   Next objLoopFolder

   Set objSubdirs = Nothing
   Set objFolder = Nothing

End Sub

How does it work?  The GetAllDwgs fuction does two things.  First, it looks at all of the files in the directory it is looking at.  If the file extension is .DWG, it sets the file name to a variable and displays the filename in a message box.   This is where you want to place your code to work with the file.  You can open it, print it, draw in it, extract info from it, etc.  Make sure you close it.

After it looks through all of the files it is looking at, it goes down to look at all sub-directories.  You may notice that the procedure GetAllDwgs calls itself with this code:

      GetAllDwgs objLoopFolder.Path

When your code behaves in this way - executing itself from within itself, it is called 'recursive'.  This is what allows us to get all sub-directories.

Happy Programming!

Back to Tips ©2000 by VB CAD  All rights reserved.         www.vbcad.com