I'm trying to get the filenames of some drawings in a folder. I've got the following code, but it's not working. I get a file count from the line "filecount = objFiles.Count", so it's recognizing "objFiles" as the collection of files in the folder, but everything after that simply does nothing. Oddly, I can change the "If" statement from "<>" to "=", and it still reads the "Then" statement in both cases. However, the FileName is blank. What am I doing wrong?
Dim objFile As FileDialogs
Set objFile = New FileDialogs
objFile.Title = strFolderName
objFile.StartInDir = "C:\Documents and Settings"
strFolderName = objFile.ShowOpen
strFolderName = Left$(strFolderName, InStrRev(strFolderName, "\") - 1)
MsgBox strFolderName
Dim fso As Object
Dim objFiles As Object
Dim filecount As Integer
Dim dwgfilenum As Integer
Dim poo As Variant
'Create objects to get a count of files in the directory
Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set objFiles = fso.GetFolder(strFolderName).Files
'If Err.Number <> 0 Then
filecount = objFiles.Count
For dwgfilenum = 1 To filecount
If Right((objFiles.Item(dwgfilenum).FullFileName), 3) <> "dwg" Then
poo = objFiles.Item(dwgfilenum).FullFileName
MsgBox poo
'filecount = filecount - 1
End If
Next
MsgBox filecount
Solved! Go to Solution.
Solved by augusto.goncalves. Go to Solution.
Hi,
I'm not sure this is a Inventor API question, or if you are facing this only inside Inventor VBA engine. ]
Anyway, I tried the following and it is working fine:
>>>
Public Sub test()
Dim fso As New FileSystemObject
Dim fol As Folder
Set fol = fso.GetFolder("C:\")
For Each oFile In fol.Files
Debug.Print oFile.Name
Next
End Sub
<<<
Can you try again with for each instead?
Regards,
Probably should have noted that I'm using IV R2010... It recognizes "New FileSystemObject" and "Folder" as a user-defined types, which are not defined. I'm an amateur ar VBA, and the user-defined type is above my level. Thanks for the help, anyway.
Hi,
Those types are defined on the system, but you need to add reference to Microsoft Scripting, which will also help you debug your code.
Regards,
How do I add the reference to MS Scripting? If I need to get into the local drive, I'll have to contact I.T. department, not a problem but I'd like to know what to tell them I want done. Thanx.
Just go to Tool, references, and then select as shown below.
I added the MS scripting, but I was also able to get the information I needed with the code I had... I just used a "For Each" statement. Yhanks a bunch for your help!