Runtime Error 13 Type Mismatch FSO

J

Jenny

Hi, I found this code (courtesy Greg Maxey a while ago). I get an error.

Is anyone able to help me get this running please? I am trying to learn how
to manipulate foldes/subfolders and so far it's not going well! Thank you.

Set oFld = fso.GetFolder("E:\")
'Runtime Error 13 Type Mismatch


Option Explicit

'Add reference to Microsoft Scripting Runtime
Dim fso As New FileSystemObject
Dim colFolders As New Collection
Sub Demo()
Dim oFld As Folder
Dim lngC As Long
Set oFld = fso.GetFolder("E:\TEST")
'Runtime Error 13 Type Mismatch
MsgBox "Do something with: " & oFld
CollectSubFolders oFld
If colFolders.Count > 0 Then
For lngC = 1 To colFolders.Count
MsgBox "Do something with " & colFolders(lngC)
Next
End If
Set colFolders = Nothing
End Sub
Sub CollectSubFolders(oFolder As Folder)
Dim SubFolder As Folder
For Each SubFolder In oFolder.SubFolders
colFolders.Add SubFolder.Path, SubFolder.Path
Set oFolder = fso.GetFolder(SubFolder.Path)
CollectSubFolders SubFolder
Next
End Sub
 
G

Graham Mayor

Did you read the first line of the macro - 'Add reference to Microsoft
Scripting Runtime?
VBA Editor > Tools > References
I assume you have an E: drive?
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

Jenny

Yes have it turned on (I've been running losts of snippets) and yes E: drive
is my attached hard disk.
 
J

Jenny

Graham
both these snippets put the info on a word screen - but the moment I play
with subfolders I get in trouble!

Sub testfso1()
Dim fso As New FileSystemObject
Dim drv As Drives
Dim d As Object
Dim strText As String

Set drv = fso.Drives

For Each d In drv
strText = "Drive: " & d.DriveLetter
Selection.TypeText strText
Selection.TypeParagraph
Next
End Sub


Sub test2()
Dim fso As New FileSystemObject
Dim drv As Drives
Dim strText As String
Dim d As Object


Set drv = fso.Drives

For Each d In drv
strText = "Drive: " & d.DriveLetter & " FreeSpace: "
If d.IsReady Then
strText = strText & d.FreeSpace
Else
strText = strText & "Drive not ready"
End If
Selection.TypeText strText
Selection.TypeParagraph
Next
End Sub
 
G

Greg Maxey

Jenny,

It was just tinker code and nothing was ever exhaustively tested. I was
able to run the code as posted on my own "F:\My Documents" folder without
issue. When I tried with my E:\Programs directory however, I did get a RTE
90. It was due to some sort of system folder. I modified the code to
handle that error: Is your "E:\" restriced in some way? Will the code run
on some other folder say your Word documents folder?



Sub Demo()
Dim oFld As Folder
Dim lngC As Long
Set oFld = fso.GetFolder("E:\")
ActiveDocument.Range.InsertAfter oFld & vbCr + vbCr
CollectSubFolders oFld
If colFolders.Count > 0 Then
For lngC = 1 To colFolders.Count
ActiveDocument.Range.InsertAfter colFolders(lngC) & vbCr
Next
End If
Set colFolders = Nothing
End Sub


Sub CollectSubFolders(oFolder As Folder)
Dim SubFolder As Folder
On Error GoTo Err_Handler
For Each SubFolder In oFolder.SubFolders
colFolders.Add SubFolder.Path, SubFolder.Path
Set oFolder = fso.GetFolder(SubFolder.Path)
CollectSubFolders SubFolder
Err_ReEntry:
Next
Exit Sub
Err_Handler:
MsgBox SubFolder.Name & " " & Err.Number & " " & Err.Description
Resume Err_ReEntry
End Sub
 
J

Jenny

Greg, nothing works for any directory - they are trusted locations I get the
same error regardless location I put.

Thank you for your time.
 
G

Greg Maxey

Jenny,

The only way I was able to generate a RTE-13 was to deliberately declare
oFld as something other than a folder. E.g., Dim oFld as Field. Then the
code errored on the first message box line.

This is just a stab in the dark, because I really don't have the best grasp
myself on public and global declaration, but is it possible that you have
oFld declared as something else either in the current project or normal dot.
etc.that could be causing a conflict?
 
J

Jenny

Now that's possible Greg. I do have a couple of macro templates up so that
could be it.
Thank you.
 
C

CB

Prefix all with "scripting" e.g.

Dim fso As New scripting.FileSystemObject
Dim oFld As scripting.Folder

You have another reference to something that uses the Folder object as well
 

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