Reputation: 622
In MS Outlook (2016) I am working on a VBA procedure to more quickly archive certain, individually selected e-mails into certain folders in my e-mail archive.
I have a procedure that does the trick when I address the target folder manually:
'[...]
Dim MoveToFolder As Outlook.MAPIFolder
'[...]
Set MoveToFolder = ns.Folders("Archive").Folders("Projekte-Archiv").Folders("P03_NetRef")
'[...]
With this the procedure knows what folder to move pre-selected e-mail to.
Now my problem:
I am trying to set the "MoveToFolder" folder object through a string variable giving it all the necessary data.
Why do I want to do this: Handing over the folder data as a string variable would allow me to use the same procedure for as many folders in as many hierarchy levels I want.
Here is what I came up with, using the CType function:
'[...]
Dim MoveToFolder As Outlook.MAPIFolder
'[...]
Set MoveToFolder = CType("ns.Folders(""Archive"").Folders(""Projekte-Archiv"").Folders(""P03_NetRef"")", Outlook.MAPIFolder)
'[...]
(The idea is of course in a next step to insert the string through a variable, not in plain writing like the example.)
This does not work. The object type 'Outlook.MAPIFolder' results in an error on compiling ("method or data object not found").
Later insight
As I understood later on, the CType() function is not available in VBA (as opposed to VB.net).
Upvotes: 0
Views: 1127
Reputation: 166790
Untested:
Set MoveToFolder = GetFolder(ns, "Archive|Projekte-Archiv|P03_NetRef")
A function to parse the path:
Function GetFolder(root, fpath)
Dim f As Object
Dim arr, i
arr = Split(fpath, "|")
For i = 0 To UBound(arr)
If i = 0 Then
Set f = root.Folders(arr(i))
Else
Set f = f.Folders(arr(i))
End If
Next i
Set GetFolder = f
End Function
Upvotes: 1