Home > OS >  VBA on a MAC - How to read FileNames in a given Folder (have to avoid using the Windows ActiveX File
VBA on a MAC - How to read FileNames in a given Folder (have to avoid using the Windows ActiveX File

Time:05-10

I've got a Windows VBA code which I'd like to translate for a MAC, as there's no access to the ActiveX FileSystemObject.

My goal is to read all the FileNames of all PNG-Files in a given Folder. This Code runs well on windows, but results in a "runtime error 429 activex component can't create object" on a MAC:

    Function ReadFileNames(ByVal sPath As String) As Integer
    
        Dim oFSO, oFolder, oFile As Object
        Dim sFileName As String
                    
        Set oFSO = CreateObject("scripting.FileSystemObject")
        Set oFolder = oFSO.getfolder(sPath)
        
        For Each oFile In oFolder.Files
    
            If Not oFile Is Nothing And Right(LCase(oFile.Name), 4) = ".png" Then  ' read only PNG-Files
                sFileName = oFile.Name
                ' do something with the FileName ...
            End If
    
        Next oFile
    
    End Function

Can anyone translate this code for a MAC ?

CodePudding user response:

Here is a sub, using the native VBA DIR command, listing EXCEL workbooks in a folder by printing their names on the debug window:

Public Sub DirXlList()
    Const cstrPath As String = "c:\users\xxxx\misc\"
    Dim strDirItem As String
    strDirItem = Dir(cstrPath & "*.xlsx")
    While strDirItem <> ""
       Debug.Print "FileName: " & strDirItem, "FullPath: " & cstrPath & strDirItem
       strDirItem = Dir()
       DoEvents
    Wend
End Sub

Does this help?

CodePudding user response:

VBA for Mac can link to the entire c standard library, like this example:

Private Declare PtrSafe Function CopyMemory_byPtr Lib "libc.dylib" Alias "memmove" (ByVal dest As LongPtr, ByVal src As LongPtr, ByVal size As Long) As LongPtr

I'm too lazy to write out relevant examples for you, but if, by chance, you are familiar with using the c standard library for file manipulation, you can just do it that way.

  • Related