jhowse
03-13-2017, 12:15 PM
Hi,
I'm pretty new VBA in outlook, and I am trying to create module that will allow me to move older emails from one folder to another, and if the destination folder does not exist, to create it..
I am using a version of the GetFolder Function by Microsoft for Outlook, which uses a text path string to create an Outlook.folder output to return to the calling sub..
The function works fine if the folder exists, but fails if I attempt to create a new folder that doesn't exit in the path with the following error:
An unexpected Error has occurred.
Error Number: 438
Error Description: Object doesn't support this property or method
I am hoping someone here can point out where my problem is in the code and how I can fix it..
TIA..
Function GetFolder(ByVal FolderPath As String) As Outlook.folder
Dim TestFolder As Outlook.folder
Dim FoldersArray As Variant
Dim i As Integer
On Error GoTo GetFolder_Error
Debug.Print "Org Folder Path: " & FolderPath
If Left(FolderPath, 2) = "\\" Then
FolderPath = Right(FolderPath, Len(FolderPath) - 2)
Debug.Print "New Folder Path: " & FolderPath
End If
'Convert folderpath to array
FoldersArray = Split(FolderPath, "\")
Set TestFolder = Application.Session.Folders.Item(FoldersArray(0))
If Not TestFolder Is Nothing Then
Debug.Print "Base Folder Path: " & FoldersArray(0)
For i = 1 To UBound(FoldersArray, 1)
Dim SubFolders As Outlook.Folders
Set SubFolders = TestFolder.Folders
Debug.Print "Working Folder: " & FoldersArray(i)
On Error Resume Next
' If doesn`t exist create
If SubFolders.Item(FoldersArray(i)) Is Nothing Then
On Error GoTo GetFolder_Error
Debug.Print "Add Start" & vbCrLf
Set SubFolders = SubFolders.Folders.Add(FoldersArray(i))
Debug.Print "Folder Created" & vbCrLf
Set SubFolders = TestFolder.Folders
End If
Set TestFolder = SubFolders.Item(FoldersArray(i))
Next
End If
'Return the TestFolder
Set GetFolder = TestFolder
Debug.Print "Done" & vbCrLf
Set TestFolder = Nothing
Set FoldersArray = Nothing
Exit Function
GetFolder_Error:
Set GetFolder = Nothing
For j = 1 To UBound(FoldersArray, 1)
txt = txt & FoldersArray(j) & vbCrLf
Next j
Debug.Print vbCrLf & "An unexpected Error has occurred." _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
& vbCrLf & "GetFolder Error [" & FoldersArray(i) & "]"
Exit Function
End Function
I'm pretty new VBA in outlook, and I am trying to create module that will allow me to move older emails from one folder to another, and if the destination folder does not exist, to create it..
I am using a version of the GetFolder Function by Microsoft for Outlook, which uses a text path string to create an Outlook.folder output to return to the calling sub..
The function works fine if the folder exists, but fails if I attempt to create a new folder that doesn't exit in the path with the following error:
An unexpected Error has occurred.
Error Number: 438
Error Description: Object doesn't support this property or method
I am hoping someone here can point out where my problem is in the code and how I can fix it..
TIA..
Function GetFolder(ByVal FolderPath As String) As Outlook.folder
Dim TestFolder As Outlook.folder
Dim FoldersArray As Variant
Dim i As Integer
On Error GoTo GetFolder_Error
Debug.Print "Org Folder Path: " & FolderPath
If Left(FolderPath, 2) = "\\" Then
FolderPath = Right(FolderPath, Len(FolderPath) - 2)
Debug.Print "New Folder Path: " & FolderPath
End If
'Convert folderpath to array
FoldersArray = Split(FolderPath, "\")
Set TestFolder = Application.Session.Folders.Item(FoldersArray(0))
If Not TestFolder Is Nothing Then
Debug.Print "Base Folder Path: " & FoldersArray(0)
For i = 1 To UBound(FoldersArray, 1)
Dim SubFolders As Outlook.Folders
Set SubFolders = TestFolder.Folders
Debug.Print "Working Folder: " & FoldersArray(i)
On Error Resume Next
' If doesn`t exist create
If SubFolders.Item(FoldersArray(i)) Is Nothing Then
On Error GoTo GetFolder_Error
Debug.Print "Add Start" & vbCrLf
Set SubFolders = SubFolders.Folders.Add(FoldersArray(i))
Debug.Print "Folder Created" & vbCrLf
Set SubFolders = TestFolder.Folders
End If
Set TestFolder = SubFolders.Item(FoldersArray(i))
Next
End If
'Return the TestFolder
Set GetFolder = TestFolder
Debug.Print "Done" & vbCrLf
Set TestFolder = Nothing
Set FoldersArray = Nothing
Exit Function
GetFolder_Error:
Set GetFolder = Nothing
For j = 1 To UBound(FoldersArray, 1)
txt = txt & FoldersArray(j) & vbCrLf
Next j
Debug.Print vbCrLf & "An unexpected Error has occurred." _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
& vbCrLf & "GetFolder Error [" & FoldersArray(i) & "]"
Exit Function
End Function