Create an outlook task with a range from excel in the body of the task

Firesword

New Member
Joined
Oct 10, 2018
Messages
46
Office Version
  1. 2016
Platform
  1. Windows
Hi Everyone

My first post, I'm new to VBA but have got a lot of help from this site from articules already posted, but this is the first time I cannot find what I'm looking for hence the post.
I'm using Winows 10 64bit with office 2016. I have an excel file that I'd like to create a task from, my code is below. The bit I cannot get to work is the .body which is a table on sheet2 it's a varialbe range which is why I went down the route of a table. But I cannot add this to the body of the task. I can add a string but not the table. I have no preference to how this is done ie if the range is copied to a picture and pasted into the body or anyother way.
Any help would be greatly appreciated.
Thanks
Simon

Sub NewTask()

Dim rng As Range

Set rng = [Table2[#All]]

With CreateObject("Outlook.Application").CreateItem(3)
.Subject = "Stock Requisition Request"
.StartDate = Now
.DueDate = Now
.ReminderSet = True
.Assign
.Recipients.Add Sheets(Sheet4.Name).Cells(1, 1)
.Body = [Table2[#All]]
.Display
End With

End Sub

PS I did read the FAQ and I hope I've posted this correctly
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
When you post your code you want to put the code inside code tags just FYI. [ CODE ] [ / CODE ]

Code:
Sub NewTask()


Dim rng As Range
Dim outlook As Object
Dim myEmail As Object
Dim xIns As Object
Dim pEd As Object


Set outlook = CreateObject("Outlook.Application")
Set myEmail = outlook.CreateItem(3)


Set rng = [Table2[#All]]


With myEmail


    .Subject = "Stock Requisition Request"
    .StartDate = Now
    .DueDate = Now
    .ReminderSet = True
    .Assign
    .Recipients.Add Sheets(Sheet4.Name).Cells(1, 1)
    .Body = "Opening Message:"
    .display
    
    Set xIns = myEmail.GetInspector
    Set pEd = xIns.WordEditor
    
    rng.Copy
    
    pEd.Application.Selection.Start = Len(.Body)
    pEd.Application.Selection.End = pEd.Application.Selection.Start
    pEd.Application.Selection.PasteAndFormat (wdFormatPlainText)
    .display


End With


Set xIns = Nothing
Set pEd = Nothing


Set outlook = Nothing
Set myEmail = Nothing


Set rng = Nothing


End Sub
 
Last edited:
Upvote 0
Thank you for such a quick reply and for the code. TBH I don't understand it, but it does work. :). If I may ask though, the data does not align to the headings, is there a way to format the data so it aligns to the headings?
 
Upvote 0
Hi Coding4Fun

After a lot of searching I have found the formating part that I need I have only changed one bit of your code from
Code:
    pEd.Application.Selection.PasteAndFormat (wdFormatPlainText)

To
Code:
    pEd.Application.Selection.PasteAndFormat (wdFormatOriginalFormatting)

Again I could not have done this without your help. Thank you.
 
Upvote 0

Forum statistics

Threads
1,223,749
Messages
6,174,275
Members
452,553
Latest member
red83

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top