VBA/Excel/Access/Word/Forms/Dir

Материал из VB Эксперт
Перейти к: навигация, поиск

Dir() Returns a string representing the name of a file or directory with specified pattern.

   <source lang="vb">

Sub strDemo19()

  Debug.Print Dir("C:\Windows\*.ini")

End Sub

</source>
   
  


Get Directory by using the Dir function

   <source lang="vb">

Sub DirDemo2()

   MsgBox Dir

End Sub

</source>
   
  


Get the file names

   <source lang="vb">

Sub FileNames()

 Dim FName As String
 Dim FNames() As String
 Dim FType As String
 Dim i As Integer
 
 FType = "*.xls"
 FName = Dir(FType)
 Do Until FName = ""
   i = i + 1
   ReDim Preserve FNames(1 To i)
   FNames(i) = FName
   FName = Dir
 Loop
 If i <> 0 Then
   For i = 1 To UBound(FNames)
     MsgBox FNames(i)
   Next i
 End If

End Sub

</source>
   
  


Use Dir function to get the file directory

   <source lang="vb">

Sub DirDemo()

 Dim FType As String
 FType = "*.xls"
 MsgBox Dir(FType)
 

End Sub

</source>
   
  


Validate the file path

   <source lang="vb">

Function ValidPath() As Boolean

   Dim fPath As String
   fPath = "c:\"
   If Dir(fPath, vbDirectory) = "" Or fPath = "" Then
       ValidPath = False
   Else
       ValidPath = True
   End If

End Function

</source>