Search code examples
vb.netolevisual-foxpro

Create folders in Outlook via OLE automation (VFP or VB)


I would like to add some new folders alongside the InBox, Send, Trash folders using OLE Automation. My preferred language is VFP, but VB might be useful too.

Here is how far I have dug into the Application object to ferret this out.

    loApp = CREATEOBJECT("outlook.application")
loSpace = loApp.GetNameSpace("MAPI")
UU = ""
FOR EACH loFolder IN loSpace.Folders
    IF  NOT LOWER(loFolder.Name)$"outlook data file  archives   wek@santaclaus.com "
        *? "______________________________"
        *? loFolder.Name
        *? CHR(9)+"-----"
        IF LOWER(loFolder.NAME) = LOWER("davidcosmos@metaworld.com")
            ? loFolder.NAME         && This is where the Accounts 
            FOR EACH loSubFolder IN loFolder.Folders
                ? CHR(9)+loSubFolder.Name+"-----"
                SET STEP ON
                IF loSubFolder.Name = "Inbox"
                    FOR EACH Email IN loSubFolder.Items
                        * Here the Email object represents each Email
                    ENDFOR 
                ELSE 
                ENDIF
           ENDFOR
        ELSE    
        ENDIF 
    ELSE 
    ENDIF    
ENDFOR
  • I assume that the creation of folders should be done at the loSubFolder layer.

loSubFolder.

Any good idea would great...

TIA

DK


Solution

  • It should be as simple as using the Folders.Add method, here's the Excel VBA link but the method should also be available in OLE:

    https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/folders-add-method-outlook

    So, in your case, you would find the folder (or sub-folder, which is still a folder) that you want to add a new folder to, and call something along the lines of:

    loSubFolder.Folders.Add("new folder name")
    

    If you don't specify a second parameter in the Add method, it will create a folder that holds the same types of items as its parent, in this case the type of items in loSubFolder.

    You can also specify a second parameter that is one of several explicitly specified OlDefaultFolders constants, for example:

    loSubFolder.Folders.Add("new folder name", olFolderInbox)