Create new outlook task into task subfolder using vba

Status
Not open for further replies.

jbarbara11

Member
Outlook version
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
 
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
 
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
 
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;
 
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
 
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.
 
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.
Similar threads
Thread starter Title Forum Replies Date
S Create Outlook Task from Template and append Body with Email Body Outlook VBA and Custom Forms 4
J PSA: How to create custom keyboard shortcut for "Paste Unformatted Text" in Outlook on Windows Outlook VBA and Custom Forms 1
Commodore Any way to create "from-only" account on Outlook 2021? Using Outlook 1
N Can't create NEW GROUP and add/remove a member from existing Group in Outlook Using Outlook 1
O Outlook 365 - How to create / copy a new contact from an existing one? Using Outlook 5
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 Using Outlook 0
Diane Poremsky Create a Custom Numbering Field for Outlook messages Using Outlook 0
Diane Poremsky Manually create a POP3 account in Outlook 2007 Using Outlook 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 Using Outlook 0
Diane Poremsky Create an Outlook appointment from an email message Using Outlook 4
Diane Poremsky Outlook Crashes When You Reply or Create a New Message Using Outlook 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
J Create or Import a Outlook Rule through C# code. Outlook VBA and Custom Forms 2
F can I use Outlook 2007 to create peer review/approve/reject forms Outlook VBA and Custom Forms 1
E Outlook could not create the work file. Check the temp environment variable Using Outlook 8
S Create a new Outlook MailItem in an Outlook folder(not a draft) Outlook VBA and Custom Forms 2
D Create a macro in Outlook to run a rule Outlook VBA and Custom Forms 32
B How to create the customized Outlook SQL DASL for selected days? Outlook VBA and Custom Forms 2
N How Can I create an Outlook Macro to import calendar? Outlook VBA and Custom Forms 1
P Create appointment to custom (shared) outlook calendar Outlook VBA and Custom Forms 3
S HELP: Create Buttons in Outlook Email body or Tool bar Outlook VBA and Custom Forms 1
N How to create hidden rule in outlook 2007 Outlook VBA and Custom Forms 3

Similar threads

Back
Top