[RESOLVED] C++ , MFC and create folders in Outlook
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [RESOLVED] C++ , MFC and create folders in Outlook

  1. #1
    Join Date
    Mar 2014
    Posts
    4

    [RESOLVED] C++ , MFC and create folders in Outlook

    Hi Everyone,
    I am wokring on a project that has used an MFC DLL in conjunction with Outlook to send and archive mail. I need to be able to create folders for integration from an older mail system. I have looked online to see if there are examples in C++ (since I have to modify the existing C++ dll to include the funcitonality). I realize it is much simpler using VBA or C#, but I am limited with my choice.
    I essentially want to use the MFC type lib for Outlook 14 and use the CMAPIFolder,CFolders create folders in the Inbox.

    here is a sample of the code:
    //I have included the necessary header files

    COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
    //start outlook
    if (!olApp.CreateDispatch(_T("Outlook.Application"), &e))
    {
    ::MessageBoxA(0,"","Please start Outlook and try again!!",0);
    return(-1);
    }
    oNS=olApp.GetNamespace(_T("MAPI"));
    COleVariant profile((LPTSTR)(LPCTSTR)olApp.get_DefaultProfileName());
    oNS.Logon(covOptional,covOptional,covOptional,covOptional);
    //get inbox folder .
    CFolders cf_Inbox = curStore.GetDefaultFolder(OutlookFolders:lFolder_Inbox);
    if(cf_Inbox!=NULL)
    {
    if (FileName!=NULL)
    {
    createFolderName=CString(FileName);
    if (!createFolderName.IsEmpty())
    {
    CComVariant folderType("IPF.Note");
    cf_Inbox.Add((LPTSTR)(LPCTSTR)createFolderName,folderType); //<<--- keep getting a member not found error.

    }
    }
    }

    }

    I am not sure if I am going about this the correct way. I would appreciate any input.
    Thanks in advance

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,752

    Re: C++ , MFC and create folders in Outlook

    When posting code, please format the code properly and use code tags. Go Advanced, select the code and click '#'.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Mar 2014
    Posts
    4

    Re: C++ , MFC and create folders in Outlook

    Sorry for the improper formatting. I wasn't sure how to format. Thanks for indicating how to do it.

    Hi Everyone,
    I am wokring on a project that has used an MFC DLL in conjunction with Outlook to send and archive mail. I need to be able to create folders for integration from an older mail system. I have looked online to see if there are examples in C++ (since I have to modify the existing C++ dll to include the funcitonality). I realize it is much simpler using VBA or C#, but I am limited with my choice.
    I essentially want to use the MFC type lib for Outlook 14 and use the CMAPIFolder,CFolders create folders in the Inbox.

    here is a sample of the code:
    //I have included the necessary header files for Namespace,Application,CFolders. I have also modified the import statements in the header files to work with outlook type lib 14.
    Code:
    void createOutlookFolders(char* FolderName)
    {
    
    COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); //start outlook if (!olApp.CreateDispatch(_T("Outlook.Application"), &e)) {
    ::MessageBoxA(0,"","Please start Outlook and try again!!",0);
    } oNS=olApp.GetNamespace(_T("MAPI")); COleVariant profile((LPTSTR)(LPCTSTR)olApp.get_DefaultProfileName()); oNS.Logon(covOptional,covOptional,covOptional,covOptional); //get inbox folder . CFolders cf_Inbox = oNS.GetDefaultFolder(olFolderInbox);//olFolderInbox comes from ::olDefaultFolders enum if(cf_Inbox!=NULL) {
    if (FolderName!=NULL) {
    customFolderName=CString(FolderName);
    if (!customFolderName.IsEmpty()) {
    CComVariant folderType("IPF.Note"); cf_Inbox.Add((LPTSTR)(LPCTSTR)customFolderName,folderType); //<<--- keep getting a member not found error.
    }
    }
    cf_Inbox.ReleaseDispatch();
    }
    oNS.Logoff(); olApp.ReleaseDispatch();
    }
    I am not sure if I am going about this the correct way. I would appreciate any input.
    Thanks in advance



    //************************SOLVED the Issue****************************************
    I think I have solved the issue. The code will only create a folder in the inbox folder. Please see below:


    Code:
    void createOutlookFolders(char* FolderName)
    {
    
    COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); //start outlook if (!olApp.CreateDispatch(_T("Outlook.Application"), &e)) {
    ::MessageBoxA(0,"","Please start Outlook and try again!!",0);
    } else {
    oNS=olApp.GetNamespace(_T("MAPI")); COleVariant profile((LPTSTR)(LPCTSTR)olApp.get_DefaultProfileName()); oNS.Logon(covOptional,covOptional,covOptional,covOptional); //get inbox folder . MAPIFolderPtr pInbox = oNS.GetDefaultFolder(olFolderInbox);//olFolderInbox comes from ::olDefaultFolders enum if(pInbox!=NULL) {
    if (FolderName!=NULL) {
    customFolderName=CString(FolderName); _FoldersPtr inboxColl=pInbox->GetFolders();
    if (!customFolderName.IsEmpty()) {
    inboxColl.Add((LPTSTR)(LPCTSTR)customFolderName,covOptional);//creates sub folder under inbox main folder.
    }
    }
    }
    }
    oNS.Logoff(); olApp.ReleaseDispatch();
    }
    Users are responsible for memory management OleInitialisation and uninitializations themselves.

    Please feel free to provide input or comments.
    Last edited by qwerty1234; March 6th, 2014 at 01:12 PM.

  4. #4
    Join Date
    Mar 2014
    Posts
    4

    Re: C++ , MFC and create folders in Outlook

    I have solved and tested out the code with MFC using CFolders,CMAPIFolder,CApplication,CNameSpace. I could not find an example online anywhere using C++ ,MFC and outlook 2010. This maybe helpful to someone.

    Code:
    int CreateCustom_CORFileFolder(CNameSpace oNS,char *createFolderName)
    {
    	int created=-1;
    	TRY
    	{
    		COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
    		CMAPIFolder cMAPIFolder_CreatedFolder=NULL;
    		CMAPIFolder cMAPIFolder_inboxFolder=NULL;
    		CFolders  cfolders_inboxColl=NULL;
    		CString folderName;	
    		
    		if (oNS!=NULL)
    		{
    			cMAPIFolder_inboxFolder=oNS.GetDefaultFolder(olFolderInbox);
    			if (cMAPIFolder_inboxFolder!=NULL)
    			{
    				cfolders_inboxColl=cMAPIFolder_inboxFolder.get_Folders();
    				if (cfolders_inboxColl!=NULL)
    				{
    					folderName=CString(createFolderName);
    					if(!folderName.IsEmpty())
    					{
    						cMAPI_CreatedFolder=cfolders_inboxColl.Add((LPTSTR)(LPCTSTR)folderName,covOptional);
    						if(customFolder)
    						{
    							//AfxMessageBox(_T("cutomFolder is named: ")+customFolder.get_Name(),0,0);
    							if(!customCreatedFolder.get_Name().IsEmpty())
    							{
    								if (cMAPI_CreatedFolder.get_Name().Compare(folderName)==0)
    								{
    									created=1;									
    								}
    							}
    						}
    					}
    				}
    			}
    			if (cMAPI_CreatedFolder) cMAPI_CreatedFolder.ReleaseDispatch();
    			if (cfolders_inboxColl)	cfolders_inboxColl.ReleaseDispatch();
    			if (cMAPIFolder_inboxFolder) cMAPIFolder_inboxFolder.ReleaseDispatch();
    		}
    	}
    	CATCH_ALL(e)
    	{
    		e->ReportError();
    	}
    	END_CATCH_ALL;
    	return(created);
    }
    good luck
    Last edited by qwerty1234; March 10th, 2014 at 11:37 AM.

  5. #5
    Join Date
    Sep 2005
    Location
    London
    Posts
    208

    Re: C++ , MFC and create folders in Outlook

    Hi qwerty,

    Did you try calling CreateFolder() on the IMAPIFolder interface?

    Quote Originally Posted by qwerty1234 View Post
    I have tested out the code with MFC using CFolders,CMAPIFolder,CApplication,CNameSpace. I could not find an example online anywhere using C++ ,MFC and outlook 2010. This maybe helpful to someone.
    Check out the following example as it may help you work out how it all works:
    http://read.pudn.com/downloads166/so...eDlg.cpp__.htm


    Best regards,

    Doron Moraz


    EDIT: Notice that running a MAPI-based application usually requires that the user has sufficient permissions to access the data on an Exchange store or personal folders (.pst) file.
    Last edited by Doron Moraz; March 10th, 2014 at 11:22 AM.

  6. #6
    Join Date
    Mar 2014
    Posts
    4

    Re: C++ , MFC and create folders in Outlook

    Hi Doron,
    Thanks for your response. I had figured out the solution earlier. I forgot to mark it as resolved. I appreciate your helpful response. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center