Working with Files, FileDialogs.cls and BrowseFolder.bas

Working with Files, FileDialogs.cls and BrowseFolder.bas

Ed__Jobe
Mentor Mentor
1,640 Views
0 Replies
Message 1 of 1

Working with Files, FileDialogs.cls and BrowseFolder.bas

Ed__Jobe
Mentor
Mentor

I'm posting this to bring together information I've gathered for working with files and folders in VBA. Attached are two zip files containing modules that you can import into your project. After downloading them, unzip them to reveal a  *.bas file. Then right click on your VBA project and select "Import File...". This will bring it in as a Class Module.

 

  acad unblock.pngacad add class file.png

 

Below are some sample functions on how to use them. The samples use Windows dialogs to get strings representing files and folders.

 

 

 

 

 

Option Explicit

Public Sub OpenFile()
    'sample to show how to use FileDialogs
    Dim objFile As FileDialogs
    Dim strFilter As String
    Dim strFileName As String

    Set objFile = New FileDialogs
    'desc,filter combinations must all be separated with pipe char "|"
    strFilter = "Drawings (*.dwg)|*.dwg|All Files (*.*)|*.*"
    objFile.Title = "Open a drawing"
    'default dir is CurDir
    objFile.StartInDir = "c:\"
    objFile.filter = strFilter
    'return a valid filename
    strFileName = objFile.ShowOpen
    If Not strFileName = vbNullString Then
        'use this space to perform operation
        'MsgBox strFileName
        AcadApplication.Documents.Open strFileName
    End If
    Set objFile = Nothing

End Sub

Public Sub SaveFile()
    'sample to show how to use FileDialogs
    Dim objFile As FileDialogs
    Dim strFilter As String
    Dim strFileName As String

    Set objFile = New FileDialogs
    'desc,filter combinations must all be separated with pipe char "|"
    strFilter = "Drawings (*.dwg)|*.dwg|All Files (*.*)|*.*"
    objFile.Title = "Save a drawing"
    'default dir is CurDir
    'objFile.StartInDir = "c:\"
    objFile.filter = strFilter
    'return a valid filename
    strFileName = objFile.ShowSave
    If Not strFileName = vbNullString Then
        'use this space to perform operation
        'MsgBox strFileName
        ThisDrawing.SaveAs strFileName
    End If
    Set objFile = Nothing

End Sub



Public Function GetFolderPath(strDialogTitle As String, Optional strStartIn As String) As String
   Dim sPath As String
   If strStartIn = "" Then strStartIn = CurDir
   sPath = BrowseFolder.FolderBrowse(strDialogTitle, strStartIn)
   If sPath <> "" Then
      If BrowseFolder.FolderExists(sPath) Then
         GetFolderPath = sPath
      End If
   End If
End Function

 

 

 

 

 

 

To work with the files as objects, you can use the Windows FileSystemObject. To do this, you need to reference "Microsoft Scripting Runtime". For more information on scripting, see Microsoft's website. With FileSystemOjbect, you can do the types of things you would do in Windows Explorer, like move, copy, delete and rename. You can also open a stream for writing to a file.

 

Below is a sample of using the object.

 

 

 

 

 

Public Sub RenameDwg()
    Dim fso As Scripting.FileSystemObject
    Dim fsoFile As Scripting.File
    Dim strFileName As String
    strFileName = "C:\Temp\SomeFile.dwg"
    
    Set fso = CreateObject("Scripting.FileSystemObject")

    If fso.FileExists(strFileName) = True Then
        Set fsoFile = fso.GetFile(strFileName)
        fsoFile.Name = "NewDwgName.dwg"
    End If
End Sub

Public Sub ListFolderContents()
    Dim fso As Scripting.FileSystemObject
    Dim fsoFile As Scripting.File
    Dim fsoFldr As Scripting.Folder
    Dim strFolder As String
    strFolder = GetFolderPath
    
    Set fso = CreateObject("Scripting.FileSystemObject")

    If fso.FolderExists(strFolder) = True Then
        Set fsoFldr = fso.GetFolder(strFolder)
        For Each fsoFile In fsoFldr.Files
            Debug.Print fsoFile.Name
        Next
    End If
End Sub

 

 

 

 

 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

1,641 Views
0 Replies
Replies (0)