Add custom signature by button to end of letter outlook 2007 - vba

I have button with this macros:
Sub Mail_Outlook_With_Signature_Html_1()
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "custom signature"
On Error Resume Next
With OutMail
.Display
.HTMLBody = strbody & "<br>" & .HTMLBody
End With
End Sub
But this create a new msg, and I need first to write some text in blank letter and next add one or more custom signature to end of the letter. How can I do it?

The Outlook object model doesn't provide any special property or method for signatures. They are just a part of the message body. So, you can use the same properties you use for editing or setting the message bodies. The Outlook object model provides three main ways for working with item bodies:
Body.
HTMLBody.
The Word editor. The WordEditor property of the Inspector class returns an instance of the Word Document which represents the message body. So, you can use the Word object model do whatever you need with the message body.
See Chapter 17: Working with Item Bodies for more information.
It is up to you which way is to choose, in case of the HTMLBody property you need to find the closing </body> tag and insert your valid signature HTML markup before it (for newly created items).
P.S. There is no need to create a new Application instance if you run the code in Outlook. Instead, you need to use the Application property.

Related

How to use .HMTLBody to capture an email signature in VBA code

I am attempting to capture an email signature with VBA code and insert it automatically into emails.
Based on this answer (Outlook Email and Signature from Excel VBA - .Body vs .HTMLbody), I believe the code below should function as expected - have "Add Body Here" followed by the email signature.
Although I get an error 'Application-defined or object defined error' on the line .HTMLBody = "<p>Add Body Here.</p>" & .HTMLBody'.
Dim OApp As Object
Dim OMail As Object
Dim Signature As Variant
Set OApp = CreateObject("Outlook.Application")
Set OMail = OApp.CreateItem(0)
With OMail
.Display
.Subject = "Subject"
.HTMLBody = "<p>Add Body Here.</p>" & .HTMLBody
.Display
End With
First of all, call the Display method before making any modifications to the message body to grab the signature as is:
With OMail
.Display
.Subject = "Subject"
.HTMLBody = "<p>Add Body Here.</p>"
End With
Second, there is no need to call the Display method twice in the code.
Third, keep in mind that HTMLBody property is an HTML string which represents the message body. While the OOM handles badly formatted HTML strings, the best practice to deal with a well-formatted HTML strings, so that means you need to insert the additional content right after the opening <body> tag if you need to get it in the beginning or just assign it from scratch:
.HTMLBody = "<p>Add Body Here.</p>"

Forward mail and add content to body (Outlook 2007, VBA)

Can anyone help me with editing the VBA-Code for the following Problem:
I want to forward e-mails with a specific subject to an specific E-Mail. In this process i want to add a text to the forwarded body.
Thank's for your help!
edit.
I have the code now, but it doesn't work properly. It sends the last E-Mail clicked on :(.
Sub Test(oMail As MailItem)
Dim MyItem As Outlook.MailItem
Dim obj_curitem As MailItem
Dim obj_newitem
Dim obj_Selection
Dim obj_curfolder
Dim obj_msgitems
Dim Forward As Object
If Err.Number = 0 Then
Set obj_Selection = Outlook.ActiveExplorer.Selection
If obj_Selection.Count > 0 Then
For Each obj_curitem In obj_Selection
strID = obj_curitem.EntryID
Set olNS = Application.GetNamespace("MAPI")
'Object auf einem neuen Item erstellen
Set obj_newitem = obj_curitem.Forward
With obj_curitem.Forward
.Forward = True
.SentOnBehalfOfName = "###" 'Deine Mailadresse
.Subject = "WG" & .Subject 'Betreff
.To = "###" 'Empfängermail
.BODY = "geprüft" & .BODY 'E-Mail Inhalt
.Send
End With
Next
End If
End If
End Sub
In general you will need to handle the NewMailEx event of the Application class where you can check out the Subject property and decide whether to forward the email or not. The Forward method of the Application class allows you doing so - it executes the Forward action for an item and returns the resulting copy as a MailItem object.
This NewMailEx event fires once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem, MeetingItem, or SharingItem. The EntryIDsCollection string contains the Entry ID that corresponds to that item. You can use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item.
The Outlook object model provides three main ways for working with item bodies:
Body.
HTMLBody.
The Word editor. The WordEditor property of the Inspector class returns an instance of the Word Document which represents the message body. So, you can use the Word object model do whatever you need with the message body.
See Chapter 17: Working with Item Bodies for more information.

Vba doesn't forward the latest message in the conversation

Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myitems = myInbox.Items
For Each myitem In myitems
If myitem.Class = olMail Then
If InStr(1, myitem.Subject, "Hi") > 0 Then
If myitem.Sender.GetExchangeUser.PrimarySmtpAddress = "xyz#abc.com" Then
With myitem.Forward
.Recipients.Add "pqr#abc.com"
.CC = "xyz#abc.com"
.Body = "Hey,there"
.Send
End With
End if
End if
End if
Next myitem
Basically if I got the last email with subject line as "Hi" from xyz then I want to forward it to pqr and CC xyz . Everthing works fine but the forwarded message is not the original one but it just composes a new email. I always want to add something while forwarding an email in addition to what I received from xyz. Can anyone help please. Thanks.
Edit:- If I put a display command immediately after With myitem.Forward it shows the entire thread but it disappears and turns in to a new email once I add the recipient and the body. Also I think that it can interpret .body as the entire new body and I should find something which will add to existing body.
If you need to preserve the existing message you should to insert your text before the existing property value (in case of plain text emails).
.Body = "Hey,there" + .Body
Or inject your HTML markup into the <body> element to keep the HTML markup well-formed.
The Outlook object model provides three main ways for working with item bodies:
Body - a string representing the clear-text body of the Outlook item.
HTMLBody - a string representing the HTML body of the specified item.
Word editor - the Microsoft Word Document Object Model of the message being displayed. The WordEditor property of the Inspector class returns an instance of the Document class from the Word object model which you can use to set up the message body.
You can read more about all these ways in the Chapter 17: Working with Item Bodies. It us up to you which way is to choose to customize the message body.

Outlook 2013: select multiple emails and autoreply using template

I am trying to get this code to work.
I want to select multiple emails from my inbox and send a auto reply using a template.
I am getting a run-time error: Object variable or With Block variable not set.
Any help would be appreciated. Also I would like to add a msg box telling me how many items were sent.
Option Explicit
Sub ReplywithTemplate()
Dim Item As Outlook.MailItem
Dim oRespond As Outlook.MailItem
For Each Item In ActiveExplorer.Selection
' This sends a response back using a template
Set oRespond = Application.CreateItemFromTemplate("C:\Users\Accounting\AppData\Roaming\Microsoft\Templates\scautoreply.oft")
With oRespond
.Recipients.Add Item.SenderEmailAddress
.Subject = Item.Subject
' includes the original message as an attachment
.Attachments.Add Item
' use this for testing, change to .send once you have it working as desired
.Display
End With
On Error Resume Next
Next
Set oRespond = Nothing
End Sub
I have noticed the following lines of code:
For Each oRespond In ActiveExplorer.Selection
' This sends a response back using a template
Set oRespond = Application.CreateItemFromTemplate("C:\Users\Accounting\AppData\Roaming\Microsoft\Templates\scautoreply.oft")
With oRespond
You need to use a new variable for creating an auto-reply email from a template because the selected Outlook item is missed (replaced with a newly created one).
So, basically you can create an item from a template, add recipients from the selected Outlook item and call the Send method. Or you can use the Reply method of the selected item in Outlook, copy the required properties from a template and call the Send method. It is up to you which way is to choose.
Finally, you may find the Getting Started with VBA in Outlook 2010 article helpful.

Outlook - Forward selected items as attachments macro - issue with forward symbol

I found the below code from a very helpful post by user thommck. It forwards selected items as attachments in separate emails to a specified recipient.
When I use the code, the forward symbol does not appear on the email icon of the email I just forwarded. If I use the regular Outlook method for "Forward as Attachment," the symbol is added to the envelope icon in my viewing pane.
Any ideas on how to get this forward symbol to appear when using this code?
Sub ForwardSelectedItems()
On Error Resume Next
Dim objItem As Outlook.MailItem
If Application.ActiveExplorer.Selection.Count = 0 Then
MsgBox ("No item selected")
Exit Sub
End If
For Each objItem In Application.ActiveExplorer.Selection
Set objMsg = Application.CreateItem(olMailItem)
With objMsg
.Attachments.Add objItem, olEmbeddeditem
.Subject = "enter text"
.To = "example#example.com"
.Send
End With
Next
Set objItem = Nothing
Set objMsg = Nothing
End Sub
You need to use the Forward method of the MailItem class instead of creating a new mail item in the code:
Application.CreateItem(olMailItem)
should be replaced with:
objItem.Foward()
Note, you need to clear the message body if you don't want to see the content of the attached item.
You may find the Getting Started with VBA in Outlook 2010 article helpful.
You can simulate pressing a button with ExecuteMso.
https://msdn.microsoft.com/en-us/library/office/ff862419%28v=office.15%29.aspx
expression.ExecuteMso(idMso)
expression An expression that returns a CommandBars object.
To find the idMso, go through the process of adding a button to the Quick Access Toolbar or a ribbon. Once you find the control in the list, hover over it until the tooltip appears. The idMso is the name in brackets.
In this case it is ForwardAsAttachment