Type mismatch compile error with recursive function - perplexed

P

Paul Schrum

MS Word 2007
I have limited experience programming Office applications. This is my
first effort with Word.

I have a recurrsive function which looks good on paper, but when I try
to run the macro I get
Compile error:
Type mismatch

The compiler highlites the first occurence of
recurseWriteFolderName (aFolder)

As you can see in the code below my sig, I have dim'd everything as
Folder.

Note, I have activated the reference to the Microsoft Runtime
Scripting library.

- Paul Schrum
===========

Dim count As Integer

Sub DirStructure()
count = 1

ThisDocument.Sections(1).Range.InsertAfter "test string"

Dim topLevelFolder As String
Dim fso As New FileSystemObject
Dim theFolders As Folders
Dim aFolder As Folder

topLevelFolder = "Q:\60146825\"
Set theFolders = fso.GetFolder(topLevelFolder).SubFolders
For Each aFolder In theFolders
recurseWriteFolderName (aFolder)
Next

End Sub

Sub recurseWriteFolderName(aFolder_ As Folder)
writeLine aFolder_.Name

Dim subFolder As Folder
For Each subFolder In aFolder_.SubFolders
count = count + 1
If count > 1026 Then
Return
End If
recurseWriteFolderName (subFolder)
Next
End Sub

Sub writeLine(textToWrite As String)
ThisDocument.Sections(1).Range.InsertAfter textToWrite
End Sub
 
T

Tony Jollans

(aFolder) is not a Folder object!

The parentheses force evaluation of what's inside them, and, in this case, the evaluation process returns the default property of the aFolder object - a String (I guess, but haven't checked that Name is the default).

Just remove the parentheses, and code ...

recurseWriteFolderName aFolder

... and, similarly, for the recursive call ...

recurseWriteFolderName subFolder
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top