20264

Make Outlook Task item through Excel Button

Question:

I want to make a simple program in which I can create an Outlook Task. User fills in data and clicks the create button.

I found the following code online and it should work, but it doesn't. No task is added in my Outlook, but no error is shown either. I have the feeling it somehow goes wrong with adding the recipients of the task.

Any clue why I don't get an error but, no tasks are added?

Dim OutApp As Outlook.Application Dim OutTask As Outlook.TaskItem Set OutApp = CreateObject("Outlook.Application") Set OutTask = OutApp.CreateItem(olTaskItem) Set myRecipient = OutTask.Recipients.Add("I.wont.write.my.actual.address.in.this@example.com") myRecipient.Type = olTo If myRecipient.Resolved Then With OutTask .Display .Subject = Cells(3, "I") .StartDate = Now .DueDate = Cells(2, "I") .Body = "Please see the attached email for a service request assigned to you." End With End If Set OutTask = Nothing Set OutApp = Nothing

I just can't figure it out and it's really breaking my brain at the moment. Hope someone can hint me in the right direction!

Answer1:

<blockquote>

I found the following code online and it should work, but it doesn't. No task is added in my Outlook, but no error is shown either. I have the feeling it somehow goes wrong with adding the recipients of the task.

</blockquote>

<em>Correct - Attempt to <strong><a href="https://msdn.microsoft.com/en-us/library/office/aa210271(v=office.11).aspx" rel="nofollow">resolve</a></strong> the <strong><a href="https://msdn.microsoft.com/en-us/library/office/aa211006(v=office.11).aspx" rel="nofollow">Recipient</a></strong> object myRecipient.Resolve against the Address Book before assuming its resolved If myRecipient.Resolved Then also defined variable Dim myRecipient As Outlook.Recipient for myRecipient</em>

Option Explicit Sub tasks() Dim OutApp As Outlook.Application Set OutApp = CreateObject("Outlook.Application") Dim OutTask As Outlook.TaskItem Set OutTask = OutApp.CreateItem(olTaskItem) Dim myRecipient As Outlook.Recipient Set myRecipient = OutTask.Recipients.Add("0m3r@Email.com") myRecipient.Type = olTo myRecipient.Resolve If myRecipient.Resolved Then With OutTask .Display .Subject = Cells(3, "I") .StartDate = Now .DueDate = Cells(2, "I") .Body = "Please see the attached email." End With End If Set OutTask = Nothing Set OutApp = Nothing End Sub

<em><a href="https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/option-explicit-statement" rel="nofollow">Option Explicit Statement (Visual Basic)</a></em>

<blockquote>

<sub>Forces explicit declaration of all variables in a file, or allows implicit declarations of variables.</sub>

</blockquote>

Answer2:

Check this examples, run each one and see if they are usefull to you, hope one fit your needs:

Sub outlook_send_followup() ' High importance = 2 ' Nothing = 1 ' Low importance = 0 Dim OutApp As Object Set OutApp = CreateObject("Outlook.Application") Dim MyItem As Object Set MyItem = OutApp.CreateItem(olMailItem) With MyItem .To = "example@hotmail.com" .Subject = "hi, this is a task" .SentOnBehalfOfName = "example@hotmail.com" .HTMLBody = "<HTML MSG FORMAT HERE>" .Importance = 1 .FlagStatus = olFlagMarked .FlagRequest = "Follow up" .FlagDueBy = Now .Display End With Set MyItem = Nothing Set OutApp = Nothing End Sub Sub create_outlook_taks() 'Const olImportanceLow = 0 'Const olImportanceNormal = 1 'Const olImportanceHigh = 2 Dim outlook_app As Object Set outlook_app = CreateObject("Outlook.Application") With outlook_app.CreateItem(3) .Importance = 2 .Subject = "THIS IS A TASK" .StartDate = Now + 5 .DueDate = Now + 10 .ReminderTime = Now - 3 .Body = "HI YOU CREATED THIS TASK" .Display '.Save End With Set outlook_app = Nothing End Sub

Recommend

  • Spring MVC does not allow name attribute to be used. Causes hindrance when working with javascript
  • How to display total of a column in the last row of a datatable generated using jQuery?
  • Strange behaviour in a while loop with a date compare in PHP
  • How do I compare current date in jrxml file for jasper reports?
  • Java: List the dates between a start and end date [duplicate]
  • How do I clone a class in intellij?
  • How can i find the size of a dynamically allocated array in C?
  • LINQ to NHibernate WHERE EXISTS IN
  • SharePoint Designer 2010 - Determine if today's date is within x days of a start date column us
  • Bundler using old version
  • problem with cross-domain ajax calls
  • jQuery and Uploadify session in the php file
  • Can you build a truly RESTful service that takes many parameters?
  • How to add System.Windows dll to Visual Studio 2010 express?
  • Subversion reverting resolved file
  • composer dependency stating in doesn't have php-xsl
  • Angular page doesn't refresh after data is added or removed
  • How to map Request parameter in Spring?
  • Wrapping text in spinner android
  • Python getting common name from URL using ssl.getpeercert()
  • cell spacing in div table
  • Query to find the duplicates between the name and number in table
  • Excel's Macro-Recorder usage
  • custom UITableViewCell with image for highlighting
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • Play WS (2.2.1): post/put large request
  • Excel - Autoshape get it's name from cell (value)
  • vba code to select only visible cells in specific column except heading
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • ActionScript 2 vs ActionScript 3 performance
  • To display the title for the current loaction in map in iphone
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Traverse Array and Display in markup
  • How to set the response of a form post action to a iframe source?
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal