Fredrik1987
Board Regular
- Joined
- Nov 5, 2015
- Messages
- 69
Hi! I've been stuck with this for a couple of days now, any help would be appreciated! 
I'm looping through a folder and retrieving some technical drawings. This works just fine, but I can't access the items correctly.
The drawings follow this naming structure: Drawing_RevisionNumber_Type, e.g:1900_A_par.PDF, 1900_A_par.SAT etc.
I also need to check if the drawings have been revised, and replace the old with the new version (So a collection won't work?).
Here's what I've done so far (Se code):
With a collection I can access the items, so I'm clearly not understanding/using the dictionary correctly?
Code to access items - This is the part that doesn't work:
Class module - cFolderList:
Function:

I'm looping through a folder and retrieving some technical drawings. This works just fine, but I can't access the items correctly.
The drawings follow this naming structure: Drawing_RevisionNumber_Type, e.g:1900_A_par.PDF, 1900_A_par.SAT etc.
I also need to check if the drawings have been revised, and replace the old with the new version (So a collection won't work?).
Here's what I've done so far (Se code):
- Created a class with three properties; Name, Rev. number and Filepath (cFolderList)
- A function that returns list of files as a dictionary object (Works just fine)
- Loop through all files in folder
- Split file name by "_"
- Set Key as Drawing_Type.Filetype
- Add file if key doen't exist
With a collection I can access the items, so I'm clearly not understanding/using the dictionary correctly?
Code to access items - This is the part that doesn't work:
Code:
Sub TestRef()
Dim MyDictionary As New Scripting.Dictionary
Dim MyItem As Object
Dim MyPath As String
MyPath = Path with drawings
Set ss = GetFileFromFolder(MyPath)
For Each MyItem In MyDictionary
'/ This method works with a collection
'/ How do you do the same with a dictionary?
Debug.Print MyItem.DrawingName
Next
'/ Tried the following:
'/ Setting MyItem as cFolderList
'/ Changing it to a collection, then it works just fine
End Sub
Class module - cFolderList:
Code:
Option Explicit
Private p_FilePath As String
Private p_DrawingName As String
Private p_RevNumber As String
Sub Initialize( _
Tegning As String, _
RevisjonNr As String, _
FilePath As String _
)
DrawingName = Tegning
RevNr = RevisjonNr
MyPath = FilePath
End Sub
'/ Filbane
Public Property Get MyPath() As String
MyPath = p_FilePath
End Property
Public Property Let MyPath(FilePath As String)
p_FilePath = FilePath
End Property
'/ Drawing Name
Public Property Get DrawingName() As String
DrawingName = p_DrawingName
End Property
Public Property Let DrawingName(DrawingName As String)
p_DrawingName = DrawingName
End Property
'/ Rev. number
Public Property Get RevNr() As String
RevNr = p_RevNumber
End Property
Public Property Let RevNr(RevNum As String)
p_RevNumber = RevNum
End Property
Function:
Code:
Private Function GetFileFromFolder( _
FolderDir As String _
) As Scripting.Dictionary
' Temporary variables
Dim Dict_Temp As New Scripting.Dictionary
Dim FileInfo As cFolderList
Dim FSO As New Scripting.FileSystemObject
Dim File As Scripting.File
' Variables
Dim str_Name() As String
Dim str_Key As String
Dim MyFolder As Scripting.folder
Dim FilesInFolder As Scripting.files
'/ Code
Set MyFolder = FSO.GetFolder(FolderDir)
Set FilesInFolder = MyFolder.files
'/ Set key as Name_TypeOfDrawing_FileType
'/ Check if drawing exist, if so, check revision number
For Each File In FilesInFolder
str_Name = Split(File.name, "_") ' 0: Name, 1: Rev.number, 2: Type
str_Key = str_Name(0) & "_" & str_Name(2) & "_" & File.Type
If Dict_Temp.Exists(str_Key) = False Then
Set FileInfo = New cFolderList
FileInfo.Initialize str_Name(0), str_Name(1), File.Path
Dict_Temp.Add str_Key, FileInfo
Else
'/ It activates correctly, but I need access to the items to check for rev. number
End If
Next
'/ Check for content
If Dict_Temp.Count = 0 Then
Call EmptyDictonary(MyFolder)
Else
Set GetFileFromFolder = Dict_Temp
End If
End Function