PDA

View Full Version : error 13 - Type Mismatch



Movian
11-09-2012, 05:59 AM
Hey,
I have a function to return a variant array of file names when passed a folder. This works fine in most instances however i am building a secondary system to work with the main system and when i use the exact same setup in this DB i get an error 13

The function being called is GetAllFilesInDir , after that i show how i am using it. All the variables are correct, tried all the trouble shooting i can think of... hope you guys have an idea!!

Public Function GetAllFilesInDir(strDirPath As String, Optional Extension As Variant) As Variant
' Loop through the directory specified in strDirPath and save each
' file name in an array, then return that array to the calling
' procedure.
' Return False if strDirPath is not a valid directory.
Dim strTempName As String, bugnum As Integer, varFiles() As Variant, lngFileCount As Long

' Make sure that strDirPath ends with a "\" character.
If Right$(strDirPath, 1) <> "\" Then
strDirPath = strDirPath & "\"
End If

' Make sure strDirPath is a directory.
If GetAttr(strDirPath) = vbDirectory Then
strTempName = Dir(strDirPath, vbDirectory)
Do Until Len(strTempName) = 0
' Exclude ".", "..".
If (strTempName <> ".") And (strTempName <> "..") Then
' Make sure we do not have a sub-directory name.
If (GetAttr(strDirPath & strTempName) _
And vbDirectory) <> vbDirectory Then
' Increase the size of the array
' to accommodate the found filename
' and add the filename to the array.

If IsMissing(Extension) Then
ReDim Preserve varFiles(lngFileCount)
varFiles(lngFileCount) = strTempName
lngFileCount = lngFileCount + 1
Else
If Right(strTempName, 3) = Extension Then
ReDim Preserve varFiles(lngFileCount)
varFiles(lngFileCount) = strTempName
lngFileCount = lngFileCount + 1
End If
End If
End If
End If
' Use the Dir function to find the next filename.
strTempName = Dir()
Loop
' Return the array of found files.
GetAllFilesInDir = varFiles
End If
GetAllFiles_End:
Exit Function
End Function
this is the code generating the error. I am using Option Explicit
Dim DataFile() as variant, loc as string
loc = CStr(GetSetting("Outputloc") & "\")
DataFile() = GetAllFilesInDir(loc)


~EDIT~
Looks like it MAY be caused by windows 8.... GetATTR seems to be returning 8208 instead of the expected 16 for a folder..... trying to look into this further. But if anyone has any more info on this, would be appreciated. Just want to make sure its a folder that's being looked through.

jonh
11-12-2012, 03:43 AM
I always use FSO.

File extension can be anything or omitted...


Sub TestIt()
arrFiles = GetFilesByExtension("C:\SomeFolder", ".txt")

If IsEmpty(arrFiles) Then
Debug.Print "Invalid Path"
Else
If UBound(arrFiles) = 0 Then
Debug.Print "No Files"
Else
For i = 0 To UBound(arrFiles) - 1
Debug.Print arrFiles(i)
Next
End If
End If

End Sub

Function GetFilesByExtension(path As String, Optional extension As String) As Variant
Set fso = CreateObject("Scripting.FileSystemObject")
Dim arr(): ReDim arr(0)
If fso.folderexists(path) Then
Set folder = fso.GetFolder(path)
For Each f In folder.Files
If UCase(Right(f.Name, Len(extension))) = UCase(extension) Then
arr(UBound(arr)) = f.Name
ReDim Preserve arr(UBound(arr) + 1)
End If
Next
GetFilesByExtension = arr
End If
End Function

Tommy
11-12-2012, 02:13 PM
I think that this
Dim DataFile() As Variant
should be
Dim DataFile As Variant

The function is defined as a variant.