VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

VBA- opening and filtering folders

1 REPLY 1
Reply
Message 1 of 2
Anonymous
619 Views, 1 Reply

VBA- opening and filtering folders

I have the following code which i can loop through all the .dwg files inside a folder.

Private Sub CommandButton1_Click()
'open file to extract
    Dim MyFolderext As String
    Dim MyFileext As String
    'origin
    MyFolderext = "C:\Users\abc\test"
    MyFileext = Dir(MyFolderext & "\*.dwg")
    Do While MyFileext <> ""
    Application.Documents.Open MyFolderext & "\" & MyFileext

'check sub if not enough inputs were placed on the user console
check

'unlock drawing layers
ThisDrawing.Layers("MC_BLOCO_INFO_AREAS").Lock = False
ThisDrawing.Layers("MC_BLOCO_TEXTOS_COMERCIAL").Lock = False
ThisDrawing.Layers("MC_BLOCO_TEXTOS_INV").Lock = False

'sub of the program
program


MyFileext = Dir
    Loop
    
'when finished
MsgBox "Done!"

'sub to clean to console for next operation
clean

End Sub

While it works in all the files inside a folder, I cannot make it work with subfolders and I still would need to filter some of them. So what I am asking is: can you help me changing the code to open all the folders inside the mother folder "C:\Users\abc\test" but skip folders "ignore"?

Labels (1)
  • VBA
1 REPLY 1
Message 2 of 2
norman.yuan
in reply to: Anonymous

You need to call Dir() in 2 different fashions: one call, as you do, is to get files in a folder, the other call is to get subfolders in a folder and you would need to make this call recursively to drill down to the deepest subfolders. 

 

Do a google search for "vba recursive dir search" would get you tons of links to sample codes. 

 

 

Norman Yuan

Drive CAD With Code

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report