I am working on a private Outlook project where I can read all emails from a specific account, starting in a specific folder, looking only in a specific folder, from a specific date etc.
Usually the default start folder is the InBox (in Dutch Postvak IN). To start there I would use the following line of code:
Set olparentfolder = olAccount.DeliveryStore.GetDefaultFolder(olFolderInbox)
I have a drop-down list with all the sub-folders in the InBox. So I would like to change this line of code dynamically and start the search from the chosen sub-folder.
Usually the code would be something like:
Set olparentfolder = olAccount.DeliveryStore.GetDefaultFolder(olFolderInbox).Folders("Main").Folders("Main-Sub") etc.
What I obviously don't know is the level of .Folders in the code. So this has to be dynamic. I could use a Case statement and code for lets say 10 levels, but that is not very dynamically.
I have created a small sub to explain what I mean. In this sub the line of code with "set = ..." is a string. But what I can do with the string I would also like to do with the set statement itself. I changed the sub a bit so it will work here at the forum.
Any suggestions are welcome.
Marcel
Sub Startfolder_Splitten()
Dim arr() As String
' Split de string in een array (i.e. "Hoofdfolder\Subfolder-1\Subfolder 1-1")
'arr = Split(UI_Emails_Inlezen.Range("cel_Te_Gebruiken_Startmap").Value, "\")
arr = Split("Hoofd\Sub\Subsub\SubSubSub\SubSubSubSub\SubSubSubSubSub", "\")
' Even elk item in de array printen naar het Directe venster
Dim i As Long
For i = LBound(arr) To UBound(arr)
Debug.Print i, arr(i)
Next
Dim aantal_niveaus As Long
aantal_niveaus = i
Debug.Print aantal_niveaus
'Set olparentfolder = olAccount.DeliveryStore.GetDefaultFolder(olFolderInbox) geeft standaard als default "Postvak IN of in het Engels InBox"
'0 Hoofdfolder
'1 SubFolder-1
'2
'Moet dus worden
'Set olparentfolder = olAccount.DeliveryStore.GetDefaultFolder(olFolderInbox).Folders("Hoofdfolder").Folders("Subfolder-1")
Dim formule As String
Dim geen_sub_niveau As String
Dim j As Long
formule = "Set olparentfolder = olAccount.DeliveryStore.GetDefaultFolder(olFolderInbox)"
geen_sub_niveau = "- Kies uit onderstaande lijst -"
If UI_Emails_Inlezen.Range("cel_Te_Gebruiken_Startmap").Value <> geen_sub_niveau Then
Debug.Print "Wel sub-niveau"
For j = 1 To i
formule = formule & ".Folders(""" & arr(j - 1) & """)"
Next j
Else
Debug.Print "Geen sub-niveau"
GoTo Einde
End If
Einde:
Debug.Print formule
'Dit werkt, maar hoe krijg ik dit in de VBA-code zelf
End Sub
Usually the default start folder is the InBox (in Dutch Postvak IN). To start there I would use the following line of code:
Set olparentfolder = olAccount.DeliveryStore.GetDefaultFolder(olFolderInbox)
I have a drop-down list with all the sub-folders in the InBox. So I would like to change this line of code dynamically and start the search from the chosen sub-folder.
Usually the code would be something like:
Set olparentfolder = olAccount.DeliveryStore.GetDefaultFolder(olFolderInbox).Folders("Main").Folders("Main-Sub") etc.
What I obviously don't know is the level of .Folders in the code. So this has to be dynamic. I could use a Case statement and code for lets say 10 levels, but that is not very dynamically.
I have created a small sub to explain what I mean. In this sub the line of code with "set = ..." is a string. But what I can do with the string I would also like to do with the set statement itself. I changed the sub a bit so it will work here at the forum.
Any suggestions are welcome.
Marcel
Sub Startfolder_Splitten()
Dim arr() As String
' Split de string in een array (i.e. "Hoofdfolder\Subfolder-1\Subfolder 1-1")
'arr = Split(UI_Emails_Inlezen.Range("cel_Te_Gebruiken_Startmap").Value, "\")
arr = Split("Hoofd\Sub\Subsub\SubSubSub\SubSubSubSub\SubSubSubSubSub", "\")
' Even elk item in de array printen naar het Directe venster
Dim i As Long
For i = LBound(arr) To UBound(arr)
Debug.Print i, arr(i)
Next
Dim aantal_niveaus As Long
aantal_niveaus = i
Debug.Print aantal_niveaus
'Set olparentfolder = olAccount.DeliveryStore.GetDefaultFolder(olFolderInbox) geeft standaard als default "Postvak IN of in het Engels InBox"
'0 Hoofdfolder
'1 SubFolder-1
'2
'Moet dus worden
'Set olparentfolder = olAccount.DeliveryStore.GetDefaultFolder(olFolderInbox).Folders("Hoofdfolder").Folders("Subfolder-1")
Dim formule As String
Dim geen_sub_niveau As String
Dim j As Long
formule = "Set olparentfolder = olAccount.DeliveryStore.GetDefaultFolder(olFolderInbox)"
geen_sub_niveau = "- Kies uit onderstaande lijst -"
If UI_Emails_Inlezen.Range("cel_Te_Gebruiken_Startmap").Value <> geen_sub_niveau Then
Debug.Print "Wel sub-niveau"
For j = 1 To i
formule = formule & ".Folders(""" & arr(j - 1) & """)"
Next j
Else
Debug.Print "Geen sub-niveau"
GoTo Einde
End If
Einde:
Debug.Print formule
'Dit werkt, maar hoe krijg ik dit in de VBA-code zelf
End Sub