none
Issue with Outlook Sessions?? RRS feed

  • Question

  • Have an issue with the following code:

    Sub NextEMailReportsTask()
    
    Dim objApp As Outlook.Application
    Dim defaultTasksFolder As Outlook.MAPIFolder
    Dim subFolder As Outlook.MAPIFolder
    Dim objNS As Outlook.NameSpace
    Dim objItm As TaskItem
    Dim Today As Date
    Today = Now()
    
    Set objApp = CreateObject("Outlook.Application")
    Set objNS = objApp.GetNamespace("MAPI")
    Set defaultTasksFolder = objNS.GetDefaultFolder(olFolderTasks)
    Set subFolder = defaultTasksFolder.Folders("Automated Tasks")
    
    Set objItm = subFolder.Items.Add(olTaskItem)
    
    With objItm
      .Subject = "E-Mail Sales Report"
      .Body = "Automatically sends e-mails to yada yada yada"
      .ReminderSet = True
      .ReminderTime = Format(DateSerial(year(Today), month(Today), day(Today) + 1), "short date") & " " & Format("6:40:00 AM", "short time")
      .Save
    End With

    I had a similar problem with sending e-mails, because I wasn't using the current session.  I tried to modify the code based on the corrections that I made to my e-mail code, and it's not working.

    Can someone point out what the code / format needs to be in order for this to run smoothly? There are times it runs smoothly for days and then just stops giving me error message on Set objApp = CreateObject("Outlook.Application")

    Any help would be appreciated.

    Thanks.

    JMData Consultant

    Monday, February 3, 2014 3:31 PM

Answers

All replies

  • Hello,

    The CreateObject("Outlook.Application") line of code uses the late binding technique for instantiating the host application (Outlook in our case) from another applications. Instead, you need to use the New operator:

    Sub NextEMailReportsTask()
    
    Dim objApp As New Outlook.Application
    Dim defaultTasksFolder As Outlook.MAPIFolder
    Dim subFolder As Outlook.MAPIFolder
    Dim objNS As Outlook.NameSpace
    Dim objItm As TaskItem
    Dim Today As Date
    Today = Now()
    
    Set objNS = objApp.GetNamespace("MAPI")
    Set defaultTasksFolder = objNS.GetDefaultFolder(olFolderTasks)
    Set subFolder = defaultTasksFolder.Folders("Automated Tasks")
    
    Set objItm = subFolder.Items.Add(olTaskItem)
    
    With objItm
      .Subject = "E-Mail Sales Report"
      .Body = "Automatically sends e-mails to yada yada yada"
      .ReminderSet = True
      .ReminderTime = Format(DateSerial(year(Today), month(Today), day(Today) + 1), "short date") & " " & Format("6:40:00 AM", "short time")
      .Save
    End With
    The How to automate Outlook from another program article provides an overview of programming Outlook using Automation from another program.

    Where do you run the code? There is no need to create a new Application instance from Outlook VBA.
    Monday, February 3, 2014 3:41 PM
  • With VBA macros, you do not need to explicitly create a variable set to the Outlook.Application object.  Remove your objApp declaration and use "Set objNS = Application.GetNamespace("MAPI") to use the intrinsic Outlook.Application object provided by the VBA project.  See if that makes a difference.

    Eric Legault (MVP: Outlook; About me)
    Outlook Appins: Store social media fields in your Outlook Contacts!
    Co-author, Microsoft Office 2013 Professional Step-By-Step

    Monday, February 3, 2014 4:39 PM
    Moderator
  • Eric -- You had help me in the previous posting that I had referenced in this instance.  I attempted that change and at the time it didn't work.  I'll make another attempt and update the posting.

    Eugene -- I'm running everything from Outlook.  No outside program is calling / starting the VBA code. 

    Thanks for the help.

    JMData Consultant

    Tuesday, February 4, 2014 1:56 PM
  • There is no need to create an instance of the Outlook Application in that case:

    Sub NextEMailReportsTask()   
       Dim defaultTasksFolder As Outlook.MAPIFolder
       Dim subFolder As Outlook.MAPIFolder
       Dim objNS As Outlook.NameSpace
       Dim objItm As TaskItem
       Dim Today As Date
       Today = Now()
    
       Set objNS = Application.GetNamespace("MAPI")
       Set defaultTasksFolder = objNS.GetDefaultFolder(olFolderTasks)
       Set subFolder = defaultTasksFolder.Folders("Automated Tasks")
    
       Set objItm = subFolder.Items.Add(olTaskItem)
    
    With objItm
      .Subject = "E-Mail Sales Report"
      .Body = "Automatically sends e-mails to yada yada yada"
      .ReminderSet = True
      .ReminderTime = Format(DateSerial(year(Today), month(Today), day(Today) + 1), "short date") & " " & Format("6:40:00 AM", "short time")
      .Save
    End With

    Tuesday, February 4, 2014 2:54 PM
  • BTW, what was the exact error message you were receiving?

    Eric Legault (MVP: Outlook; About me)
    Outlook Appins: Store social media fields in your Outlook Contacts!
    Co-author, Microsoft Office 2013 Professional Step-By-Step

    Tuesday, February 4, 2014 4:08 PM
    Moderator
  • Eric,

    Thanks... that appears to have done the trick.  The error message that I was getting was the -2xxxx Automation Error Posting.  Same error message that I had when you helped me out with the e-mailing issue when I was also attempting to create another Outlook session instead of using the one that was already started.

    I appreciate your help on this.

    JMData Consultant

    Wednesday, February 5, 2014 1:17 PM