I am trying to specify folders in Outlook using Interop.Outlook.
So in order to do that I use following methods:
Outlook.Folder targetFolder = ParentFolder.Folders["Level-1 Folder"] as Outlook.Folder;
This works generally ok but sometimes I will have to go more lower then a subfolder. I may need to go into sub-subfolder, like that:
Outlook.Folder targetFolder = ParentFolder.Folders["Level-1 Folder"].Folders["Level-2 Folder"] as Outlook.Folder;
I wonder if it is possible to use the Folder method from a variable. Please take a look on how I would imagine that:
string myMethod = "Folders["Level-1 Folder"].Folders["Level-2 Folder"]";
Outlook.Folder targetFolder = ParentFolder. + myMethod;
Thanks for your anwsers.
Regards, W
I wonder if it is possible to use the Folder method from a variable. Well, thats what you did in ParentFolder.Folders["Level-1 Folder"]
. ParentFolder
is a variable.
Just write
Outlook.Folder subfolder = targetFolder.Folders["Level-2 Folder"] as Outlook.Folder;
I am not sure why you think a method name has to be passed as string. Btw. Folders
is an indexer.
It makes no difference whether you chain the indexes or not
var x = a.f["a"].f["b"].f["c"];
is the same as
var x = a.f["a"];
x = x.f["b"];
x = x.f["c"];
What changes from level to level is the folder name (index), which could be in a variable. You could write
string index;
var targetFolder = ParentFolder as Outlook.Folder;
index = "Level-1 Folder";
targetFolder = targetFolder.Folders[index] as Outlook.Folder;
index = "Level-2 Folder";
targetFolder = targetFolder.Folders[index] as Outlook.Folder;
index = "Level-3 Folder";
targetFolder = targetFolder.Folders[index] as Outlook.Folder;
So you have exact the same statement at each level, except that the folder name changes. Or:
var indexes = new string[] { "Level-1 Folder", "Level-2 Folder", "Level-3 Folder" };
var targetFolder = ParentFolder as Outlook.Folder;
foreach (string index in indexes) {
targetFolder = targetFolder.Folders[index] as Outlook.Folder;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.