Create new outlook task into task subfolder using vba

Status
Not open for further replies.

jbarbara11

Member
Outlook version
Outlook 2007
Email Account
Exchange Server
I have been searching for a way to create a new outlook task in an outlook task subfolder in Outlook 2007 for a few days now and no one seems to have finished any thread with a confirmed solution. Has anyone been able to do this and if so what code did you use to get it to work?

Thanks,

Jim
 

wiglwagl

Member
Outlook version
Outlook 2007
Email Account
POP3
hi

i do so with Terms, but this should work with tasks too.

Code:
Outlook.Application outlookObj = new Outlook.Application(); 
 
Outlook.MAPIFolder subFolder = null; 
 
Outlook.MAPIFolder fldContacts = (Outlook.MAPIFolder)outlookObj.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts); 
 
subFolder = fldContacts.Folders["SubfolderName"]; 
 
Outlook.ContactItem newContact = (Outlook.ContactItem)subFolder.Items.Add(Outlook.OlItemType.olContactItem); 
 
newContact.User4 = list_s[1]; 
 
newContact.User3 = list_s[0]; 
 
newContact.FullName = list_s[2]; 
 
newContact.HomeAddress = list_s[3]; 
 
newContact.HomeAddressStreet = list_s[4]; 
 
newContact.Email1Address = list_s[9]; 
 
newContact.User2 = list_s[10]; 
 
> .. 
 
> .. 
 
newContact.Save();
regards

ww
 

jbarbara11

Member
Outlook version
Outlook 2007
Email Account
Exchange Server
hi

i do so with Terms, but this should work with tasks too.

Code:
Outlook.Application outlookObj = new Outlook.Application(); 
 
Outlook.MAPIFolder subFolder = null; 
 
Outlook.MAPIFolder fldContacts = (Outlook.MAPIFolder)outlookObj.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts); 
 
subFolder = fldContacts.Folders["SubfolderName"]; 
 
Outlook.ContactItem newContact = (Outlook.ContactItem)subFolder.Items.Add(Outlook.OlItemType.olContactItem); 
 
newContact.User4 = list_s[1]; 
 
newContact.User3 = list_s[0]; 
 
newContact.FullName = list_s[2]; 
 
newContact.HomeAddress = list_s[3]; 
 
newContact.HomeAddressStreet = list_s[4]; 
 
newContact.Email1Address = list_s[9]; 
 
newContact.User2 = list_s[10]; 
 
> .. 
 
> .. 
 
newContact.Save();
regards

ww
Does not work. :( Still saves to the default top Tasks folder. I am wondering if this is even possible for Tasks. Here's my code:

Code:
Sub AddTaskToSubFolder() 
 
Dim objApp As Outlook.Application 
 
Dim defaultTasksFolder As Outlook.MAPIFolder 
 
Dim subFolder As Outlook.MAPIFolder 
 
Dim objNS As Outlook.NameSpace 
 
Dim objItm As TaskItem 
 
Set objApp = CreateObject("Outlook.Application") 
 
Set objApp = CreateObject("Outlook.Application") 
 
Set objNS = objApp.GetNamespace("MAPI") 
 
Set defaultTasksFolder = objNS.GetDefaultFolder(olFolderTasks) 
 
Set subFolder = defaultTasksFolder.Folders("FSProjects") 
 
Set objItm = subFolder.items.Add(olTaskItem) 
 
Set objItm = objApp.CreateItem(olTaskItem) 
 
objItm.Subject = "Test task to subfolder" 
 
objItm.Save 
 
End Sub
 

wiglwagl

Member
Outlook version
Outlook 2007
Email Account
POP3
hi,

yes it works fine for me. Is the Custom Folder created?

Code:
bool rc_b = false; 
 
Outlook.MAPIFolder CustomFolder = null;           
 
Outlook.Application outlookApp = new Outlook.Application();                       
 
Outlook.MAPIFolder taskFolder = (Outlook.MAPIFolder)outlookApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks); 
 
// Verify 
 
foreach (Outlook.MAPIFolder subFolder in taskFolder.Folders)
   {
        if (subFolder.Name == was_s)
    {
            CustomFolder = subFolder;
            return true;
        }
    }
    // Add wenn nicht vorhanden
    if (CustomFolder == null)
        {
             try
             {
                   CustomFolder = taskFolder.Folders.Add(was_s, Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderTasks);
                   return true;
              }
              catch (Exception ex)
              {
                  return false;
              }
         }
         return false;
 

jbarbara11

Member
Outlook version
Outlook 2007
Email Account
Exchange Server
hi,

yes it works fine for me. Is the Custom Folder created?

Code:
bool rc_b = false; 
 
Outlook.MAPIFolder CustomFolder = null;           
 
Outlook.Application outlookApp = new Outlook.Application();                       
 
Outlook.MAPIFolder taskFolder = (Outlook.MAPIFolder)outlookApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks); 
 
// Verify 
 
foreach (Outlook.MAPIFolder subFolder in taskFolder.Folders)
   {
        if (subFolder.Name == was_s)
    {
            CustomFolder = subFolder;
            return true;
        }
    }
    // Add wenn nicht vorhanden
    if (CustomFolder == null)
        {
             try
             {
                   CustomFolder = taskFolder.Folders.Add(was_s, Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderTasks);
                   return true;
              }
              catch (Exception ex)
              {
                  return false;
              }
         }
         return false;
Do you mean MY code worked for you in Outlook (in other words, you copied my code, pasted in VBA and ran it)? If so, them maybe there is hope. To answer your question about the custom folder, yes. It exists under the default Tasks folder. It is strange because in all the various forum threads out there on this exact subject, I cannot find anyone out there who has verified that they can create a Task in VBA and have it save in a subfolder. I have seen it for emails and contacts, but not for tasks.

Anyway, any help you can provide (either directly in my code or point me to somewhere or someone who may have an answer) will be greatly appreciated.

--Jim
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
hi,

yes it works fine for me. Is the Custom Folder created?
I know from experience :) that jim's code kicks up an error if the folder does not exist or is not a subfolder of the Tasks folder.

I looks like this line is the problem:

Set objItm = objApp.CreateItem(olTaskItem)

You create the new item in the subfolder: Set objItm = subFolder.Items.Add(olTaskItem)

Then turn around and do it again: Set objItm = objApp.CreateItem(olTaskItem)

With that line commented out, it works as expected.

ETA: it also works if you reverse the order of those two lines. I'm not a VBA expert, but I don't think you don't need both lines... according to http://support.microsoft.com/kb/310244 - you need one or the other. createItem creates it in the default folder using the default form. item.add can use any form into any folder.

The CreateItem method creates a new default Outlook item. If you have to create an item that is based on a custom form that you have created, use the Items.Add method that is discussed in the next sample. You can find the CreateItem method located in the top-level application object in the Outlook object model. The CreateItem method requires only one argument. This one argument is a constant that indicates the type of item to create.

When you use the Add method on the Items collection, you can create a new item that is based on any message class. The message class may be a default Outlook message class, such as the IPM.Contact message class, or a message class for a custom form, such as the IPM.Contact.MyForm message class. To use the Items.Add method, you must first reference the folder where you want to create a new item.
 

jbarbara11

Member
Outlook version
Outlook 2007
Email Account
Exchange Server
Excellent! That did the trick! Thank you so much.

And for anyone else out there looking for a thread confirming the code that works, here it is:

Code:
Sub AddTaskToSubFolder() 
 
'Add a task to a subfolder under the Tasks folder in Outlook 
 
Dim objApp As Outlook.Application 
 
Dim defaultTasksFolder As Outlook.MAPIFolder 
 
Dim subFolder As Outlook.MAPIFolder 
 
Dim objNS As Outlook.NameSpace 
 
Dim objItm As TaskItem 
 
Set objApp = CreateObject("Outlook.Application") 
 
Set objNS = objApp.GetNamespace("MAPI") 
 
Set defaultTasksFolder = objNS.GetDefaultFolder(olFolderTasks) 
 
Set subFolder = defaultTasksFolder.Folders("FSProjects") 'Replace FSProjects with whatever subfolder name you have under Tasks. 
 
Set objItm = subFolder.items.Add(olTaskItem) 
 
objItm.Subject = "Another Test task to subfolder"   'Subject for the task 
 
'There are more parameters you can use, just Google them yourself for the start date, reminder, etc. 
 
objItm.Save 
 
End Sub
Thanks,

Jim

I know from experience :) that jim's code kicks up an error if the folder does not exist or is not a subfolder of the Tasks folder.

I looks like this line is the problem:

Set objItm = objApp.CreateItem(olTaskItem)

You create the new item in the subfolder: Set objItm = subFolder.Items.Add(olTaskItem)

Then turn around and do it again: Set objItm = objApp.CreateItem(olTaskItem)

With that line commented out, it works as expected.

ETA: it also works if you reverse the order of those two lines. I'm not a VBA expert, but I don't think you don't need both lines... according to http://support.microsoft.com/kb/310244 - you need one or the other. createItem creates it in the default folder using the default form. item.add can use any form into any folder.
 
Status
Not open for further replies.
Thread starter Similar threads Forum Replies Date
A Outlook 2010 trying to create calendar/task entries generates error "Could not save item.The connect Using Outlook 2
A Outlook 2010 trying to create calendar/task entries generates error "Could not save item.The connect Using Outlook 1
C Can't create Outlook data file Outlook Wishlist 0
G Can't create Folder Groups in Outlook 2013 Using Outlook 0
N Outlook rules don't create a copy for bcc'ed emails Using Outlook 3
F Delete/create/reset Exchange mailbox on Outlook.com Using Outlook.com accounts in Outlook 3
R Can not create folder to store specific emails in in Outlook for Mac Using Outlook 1
A Outlook macro to create search folder with mail categories as criteria Outlook VBA and Custom Forms 3
Rupert Dragwater How to create a new email with @outlook.com Using Outlook.com accounts in Outlook 32
D create an html table in outlook custom form 2010 using vba in MsAccess Outlook VBA and Custom Forms 7
R Outlook add-in to create new contact from an email. Using Outlook 0
Tanja Östrand Outlook 2016 - Create Macro button to add text in Subject Outlook VBA and Custom Forms 1
D Outlook macros to create meeting on shared calendar Outlook VBA and Custom Forms 10
G How do I create a custom pick list in VB for an outlook automated email? Outlook VBA and Custom Forms 1
Diane Poremsky Create a custom field for Outlook messages New Slipstick.com Articles 0
Diane Poremsky Create a Custom Numbering Field for Outlook messages New Slipstick.com Articles 0
Diane Poremsky Manually create a POP3 account in Outlook 2007 New Slipstick.com Articles 0
T Create Rule For Secondary E-Mail Address In Outlook 2016 Using Outlook 4
Diane Poremsky Use VBA to create an Outlook Search Folder for Sender New Slipstick.com Articles 0
Diane Poremsky Create an Outlook appointment from an email message New Slipstick.com Articles 4
Diane Poremsky Outlook Crashes When You Reply or Create a New Message New Slipstick.com Articles 0
I Create custom Outlook 2013 Rule in Office 365 Outlook VBA and Custom Forms 5
C Edit/Create Pen Not Working Outlook 2013 Using Outlook 1
Rupert Dragwater Outlook could not create the work file Using Outlook 5
J Outlook Rule - Create Link in Forwarding Message Outlook VBA and Custom Forms 2
E Create a URL hyperlink in an Outlook custom form? Outlook VBA and Custom Forms 2
R iCloud Issues in Outlook 2013, cannot create reminders Using Outlook 2
Rupert Dragwater How to delete old rules and create new in Outlook 2013 Using Outlook 12
P Cannot open Options Menu Outlook 2007 and Cannot create a new profile Using Outlook 0
C Outlook 2007 cant create new account Using Outlook 7
G Outlook could not create the work file Using Outlook 0
K Outlook Cached Mode - can't create rules to move email to another mailbox Using Outlook 2
O Can't create new email or access email acounts Outlook 2003 Using Outlook 1
S [outlook-users] Create contact group from other user's contacts Using Outlook 1
L Create hyperlinks to bookmarks in Outlook 2003 otf file Using Outlook 1
B How do I create and send mail in Outlook 2003 from code? Using Outlook 5
O Outlook 2010 - How to create custom Group By Arrangements for email Using Outlook 3
R Outlook 2007 attachments create multiple copies in the inbox Using Outlook 3
B How to create fixed reminders in Outlook Using Outlook 2
V Outlook - Unable to Create New Email Account Automatically Using Outlook 2
T Outlook 2010 continues to deselect self while trying to create email Using Outlook 3
P Cannot create connected drop down list in Outlook 2007 forms Using Outlook 1
M Can't create quickparts in Outlook 2010 Using Outlook 1
C Outlook VBA - Create Macro Move Contact field Item to Another Using Outlook 1
R How to use Outlook 2007 Calendar to create a meeting or apointment with a specific color code / background? Using Outlook 1
F Create a second Outlook profile to handle a second email account. Using Outlook 2
M Create Mailing Labels using Contacts in Outlook 2010 Using Outlook 2
R Outlook Calendar - Directly typing in to quick-create reservations often get cut off. Buggy. Using Outlook 1
J Is it possible to create text columns in Outlook? Can images be placed out of line with the text? Using Outlook 1
E Outlook 2010 crashes, can't create new profile... Using Outlook 4
Similar threads


















































Top