Add "ActiveExplorer.Selection.Item(1)" as attachment - vba

I create a Mail from a Template and add some attributes from the highlighted Mail.
I would like the original marked E-Mail (ActiveExplorer.Selection.Item(1)) as attachment to the replyEmail.
My code so far:
Dim replyEmail As Outlook.MailItem
Dim actualEmail As Outlook.MailItem
Set actualEmail = ActiveExplorer.Selection.Item(1)
Set replyEmail = Application.CreateItemFromTemplate("U:\Template.oft")
replyEmail.To = actualEmail.To
replyEmail.CC = actualEmail.CC
' Here comes the tricky part, where i would like the original marked E-Mail (ActiveExplorer.Selection.Item(1)) as attachment to the replyEmail!
replyEmail.attachment = ActiveExplorer.Selection.Item(1)
replyEmail.Display

That's pretty easy:
replyEmail.Attachments.Add actualEmail

Related

I want to add "CC" and Text in the body of this code. What should I do to add it?

I have been able to create an automated email reply as I wanted. However, I wanted to add text in the body of the email and cc to add email address. How should I add it?
Sub FwdSelToAddr()
Dim objOL As Outlook.Application
Dim objItem As Object
Dim objFwd As Outlook.MailItem
Dim strAddr As String
Dim objRecip As Outlook.Recipient
Dim objReply As MailItem
On Error Resume Next
Set objOL = Application
Set objItem = objOL.ActiveExplorer.Selection(1)
If Not objItem Is Nothing Then
strAddr = ParseTextLinePair(objItem.Body, "Email:")
If strAddr <> "" Then
Set objFwd = objItem.Forward
objFwd.To = strAddr
objFwd.Display
Else
MsgBox "Could not extract address from message."
End If
End If
Set objOL = Nothing
Set objItem = Nothing
Set objFwd = Nothing
End Sub
This is what I have done so far. I just want to be able to add CC email address and text in the body in the automated reply.
You need to modify the code a bit by setting the Cc property and the HTMLBody one if you need to modify or update the message body:
If strAddr <> "" Then
Set objFwd = objItem.Forward
objFwd.To = strAddr
objFwd.Cc = "email#address.com"
objFwd.HTMLBody = "<b>Hello world</b>"
objFwd.Display
Else
Be aware, to preserve the message body from the original email you need to insert your content between the opening <body> and closing </body> tags. If you need to add in the beginning of the message paste your additional text right after the opening tag, if you intend to paste it in the end of message - paste right before the closing tag.
Also you may find the Recipients property of the MailItem class helpful. It allows a more convenient way for setting up recipients for the Outlook items. You can read more about that property in the article that I wrote for the technical blog - How To: Fill TO,CC and BCC fields in Outlook programmatically.

Setting the position of an Attachment in a TaskItem does not work in Outlook 2019

I try to create a task from a MailItem using VBA in Outlook 2019.
According to the docu for Attachment.Add:
Position Optional Long: This parameter applies only to email
messages using the Rich Text format: it is the position where the
attachment should be placed within the body text of the message. A
value of 1 for the Position parameter specifies that the attachment
should be positioned at the beginning of the message body. A value 'n'
greater than the number of characters in the body of the email item
specifies that the attachment should be placed at the end. A value of
0 makes the attachment hidden.
However, if I use position 1 (see below), the icon with the link to the original mail will still be at the end of the body instead at beginning. Am I missing something?
Sub CreateTask()
Set olApp = Outlook.Application
Set Msg = olApp.ActiveExplorer.Selection.Item(1)
Dim olTask As TaskItem
Set olTask = olApp.CreateItem(olTaskItem)
With olTask
.Subject = Msg.Subject
.RTFBody = Msg.RTFBody
.Attachments.Add Msg, , 1 ' For some reasone position argument not working :(
'.Save
.Display
End With
End If
There is an Outlook quirk, .Display before editing.
Appears it applies in this situation too.
Option Explicit
Sub CreateTask()
Dim itm As Object
Dim msg As MailItem
Dim olTask As TaskItem
Set itm = ActiveExplorer.Selection.Item(1)
If itm.Class = olMail Then
Set msg = itm
Set olTask = CreateItem(olTaskItem)
With olTask
.subject = msg.subject
.RTFBody = msg.RTFBody
.Display ' <--- Earlier rather than later
.Attachments.Add msg, , 1
End With
End If
End Sub
Use the MailItem.MarkAsTask method which marks a MailItem object as a task and assigns a task interval for the object.

VBA - How to insert Horizontal Line in Outlook TaskItem

I want to insert an horizontal line, eg., before a text in a TaskItem body. It is possible to do this using Insert menu and clicking the horizontal line button on symbols group. But, how to code this?
This is what I've tried:
Sub NewTask()
Dim objTask As Outlook.TaskItem
Set objTask = Application.CreateItem(olTaskItem)
With objTask
.Subject = "Example Task"
.Body = ??What to put in here?? & "Example Body"
.Save
End With
Set objTask = Nothing
End Sub
This post show how to reach this for Mails. As far as I know, MailItem have Html body property whereas TaskItem does not have.
Thanks in advance.
Instead, you need to use the TaskItem.RTFBody property which returns or sets a byte array that represents the body of the Microsoft Outlook item in Rich Text Format.
The code for a horizontal line is the following:
\pard \brdrb \brdrs\brdrw10\brsp20 {\fs4\~}\par \pard
To set up the RTF formatting in Outlook you may use the following code:
.BodyFormat = olFormatRichText
.Body = StrConv("your RTF string", vbFromUnicode) 'Convert RTF string to byte array
Be aware, The Outlook object model supports three main ways of customizing the message body:
The Body property returns or sets a string representing the clear-text body of the Outlook item.
The HTMLBody property of the MailItem class returns or sets a string representing the HTML body of the specified item. Setting the HTMLBody property will always update the Body property immediately. For example:
Sub CreateHTMLMail()
'Creates a new e-mail item and modifies its properties.
Dim objMail As Outlook.MailItem
'Create e-mail item
Set objMail = Application.CreateItem(olMailItem)
With objMail
'Set body format to HTML
.BodyFormat = olFormatHTML
.HTMLBody = "<HTML><BODY>Enter the message text here. </BODY></HTML>"
.Display
End With
End Sub
The Word object model can be used for dealing with message bodies. See Chapter 17: Working with Item Bodies for more information.
Note, the MailItem.BodyFormat property allows you to programmatically change the editor that is used for the body of an item.
I was not able to reach the solution through RTFBody. However the Word object model approach, pointed by Eugene Astafiev, helped me to solve the issue.
First of all: Add reference to Word library in VBA Editor, Tools, References
And this is the example sub working:
Sub NewTask()
Dim objTask As Outlook.TaskItem
Dim objInsp As Inspector
Dim objDoc As Word.Document
Dim objSel As Word.Selection
Set objTask = Application.CreateItem(olTaskItem)
Set objInsp = objTask.GetInspector
Set objDoc = objInsp.WordEditor
Set objSel = objDoc.Windows(1).Selection
With objTask
.Subject = "Example Task"
objSel.InsertAfter "Example Body"
objDoc.InlineShapes.AddHorizontalLineStandard
.Display
.Save
End With
Set objTask = Nothing
Set objInsp = Nothing
Set objDoc = Nothing
Set objSel = Nothing
End Sub

How do i modify the vba code to reply with attachments via outlook and include png(in email signature)?

How do i modify the vba code to reply with attachments and include png(in email signature)?
Sub AnyUpdatesForOS()
Dim OrgEmail As MailItem
Dim ReEmail As MailItem
Set OrgEmail = Application.ActiveWindow.Selection.Item(1)
Set ReEmail = Application.CreateItemFromTemplate("C:\Users\Deski\AppData\Roaming\Microsoft\Templates\AnyUpdatesForOS.oft")
ReEmail.To = OrgEmail.Sender
ReEmail.CC = OrgEmail.CC
ReEmail.BCC = OrgEmail.BCC
ReEmail.Subject = OrgEmail.Subject
ReEmail.HTMLBody = ReEmail.HTMLBody & OrgEmail.Reply.HTMLBody
ReEmail.Display
End Sub
Use the Forward method instead. To add additional attachments you can use the Attachments.Add method.
Sub AnyUpdatesForOS()
Dim OrgEmail As MailItem
Dim ReEmail As MailItem
Dim myAttachments As Attachments
Set OrgEmail = Application.ActiveWindow.Selection.Item(1)
Set ReEmail = OrgEmail.Forward()
Set myAttachments = myItem.Attachments
myAttachments.Add "C:\Test.doc", olByValue, 1, "Test"
ReEmail.Display
End Sub

Auto Reply with notes, email body and add CC

I am try to CC second person but I am getting Error run-time 13 Type mismatch.
Option Explicit
'// Auto Replay with notes and email body- run Action Script
Public Sub ReplywithNote(Item As Outlook.MailItem)
Dim olInspector As Outlook.Inspector
Dim olDocument As Word.Document
Dim olSelection As Word.Selection
Dim olReply As MailItem
Dim olRecipient As Outlook.Recipient
Set olReply = Item.ReplyAll
olReply.Display
Set olRecipient = myItem.Recipient.Add("omar")
olRecipient.Type = olCC
Set olInspector = Application.ActiveInspector()
Set olDocument = olInspector.WordEditor
Set olSelection = olDocument.Application.Selection
olSelection.InsertBefore "Received, Thank you."
'// Uncomment to send
olReply.Send
End Sub
Thanks.
Try Recipient not Recipients
Dim olRecipient As Outlook.Recipient
The Add method of the Recipients class creates a new recipient in the Recipients collection. The parameter is the name of the recipient; it can be a string representing the display name, the alias, or the full SMTP e-mail address of the recipient.
If you run the following sample code in Outlook there is no need to create a new Application instance, use the Application property available in VBA out of the box.
Set myOlApp = CreateObject("Outlook.Application") // Application
Set myItem = myOlApp.CreateItem(olMailItem)
Set myRecipient = myItem.Recipients.Add ("Jon Grande")
myRecipient.Type = olCC
Don't forget to call the Resolve method of the Recipient class after adding a new one. Or just the ResolveAll method of the Recipients class to resolve recipients against the address book.
See How to: Specify Different Recipient Types for a Mail Item for more information.