would you please tell me whats the problem the VBA code below and how to resolve it.

I got compile error telling 'Type mismatch' its not clear to me whats the problem and how to fix it

Sub ListFiles()
Dim aryFiles() As String
Dim strMessage As String
Dim strFile As String

aryFiles = directory.getfiles("C:\ConvertToExecl", "*.pdf")
strMessage = "Found" & aryFiles & "pdf files." & vbCrLf & vbCrLf
For Each strFile In aryFiles
    strMessage = strMessage & strFile & vbCrLf
MsgBox (strMessage)

End Sub
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dale HarrisProfessional Services EngineerCommented:
Where is Directory created?
alam747Author Commented:
Directory is in C: drive named ConvertToExcel where all pdf files exist.
Dale HarrisProfessional Services EngineerCommented:
Okay, I couldn't figure out the exact way to do it, but to get you a little closer (as I'm leaving for the day soon), you can't put

strMessage = "Found" & aryFiles & "pdf files." & vbCrLf & vbCrLf

down because aryFiles is not a count of files.  You will need to do some kind of UBound command or something.

Also, are you sure you can assign "Directory.GetFiles" to a string array, or do you need a file object array?

Unless it's only getting the names of it.

Lastly, for each strFile in aryFiles, strFile has to be a variant is what the debugger told me.

Sorry about not providing the answer.  I have to sleep eventually ;)


Dale Harris
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

alam747Author Commented:
Thanks for your comments,
<aryFiles = directory.getfiles("C:\ConvertToExecl", "*.pdf")>
This is not VBA code, it's VB.net code

This works using the VBA Dir function

This site shows how it was done
Option Compare Database
Option Explicit

Sub ListFiles()
Dim myFileNames() As String
Dim strMessage As String
Dim strFile As String
Dim check As String
Dim x As Long

strFile = Dir("C:\ConvertToExcel\*.pdf")
check = "start"
x = 0
Do While check <> ""
    check = Dir
    strFile = strFile & ";" & check
    x = x + 1

myFileNames() = Split(strFile, ";")
strMessage = "Found " & x & " pdf files." & vbCrLf & vbCrLf
For x = LBound(myFileNames) To UBound(myFileNames)
    strMessage = strMessage & myFileNames(x) & vbCrLf
Next x
MsgBox (strMessage)

End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alam747Author Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Applications

From novice to tech pro — start learning today.