Add senders Outlook signature to email created with macro from excel - email

I have set a macro in excel to create email need to have the Outlook signature line for the sender.
Tried
.htmlbody= sig
Signature = .htmlbody
Dim Email_Subject, Email_Send_To, _
Email_Body As String
Dim Mail_Object, Mail_Single As Variant
Email_Subject = Range("B7").Value
Email_Send_To = Range("B9").Value
Email_Body = Range("B8").Value
On Error GoTo debugs
Set Mail_Object = CreateObject("Outlook.Application")
Set Mail_Single = Mail_Object.CreateItem(0)
With Mail_Single
.Subject = Email_Subject
.To = Email_Send_To
.Body = Email_Body
.Display
End With
I want to have the person that is sending the email default signature from outlook show at bottom of email.

Be aware, the Body property of the MailItem class represents the message body in plain text. If you want to preserve any formatting you need to use the HTMLBody property.
With Mail_Single
.Subject = Email_Subject
.To = Email_Send_To
.Body = Email_Body
.Display
End With
The signatures are being kept as separate files in the Signatures folder. You can find this folder in the following location:
Windows XP
C:\Documents and Settings\%username%\Application Data\Microsoft\Signatures
Windows Vista, Windows 7, Windows 8 and Windows 10
C:\Users\%username%\AppData\Roaming\Microsoft\Signatures
To see this folder you must have View hidden files and folders enabled or you can simply copy and paste the above paths in the address bar in Explorer to directly open the folder.
There are three main ways for working with bodies in Outlook:
Body.
HTMLBody.
Word editor. The Inspector class provides the WordEditor property which returns an instance of the Document class from the Word object model which represents the message body. Outlook uses Word as an email editor.
You can read more about that in the Chapter 17: Working with Item Bodies.
I want to have the person that is sending the email default signature from outlook show at bottom of email.
Basically, you just need to insert the signature before the closing <body> tag and set the HTMLBody property to the result HTML markup.

Related

AppleScript for sending emails from a list

I have an AppleScript which starts a new email in Mail, and fills in the Body with the contents of a user-selected file.
The problem is that the first line of the body in the new email is always blank -- i.e. there's a line-break before the body from the user-selected file. No matter what I do, even if I hardcode the body into the script (e.g. "Hello world!") it starts on the second line of the email.
Can anyone please suggest a way to fix this?
property theToAddress : "john#example.com"
property theSubject : "Example subject"
set theTo to theToAddress
set theSubjectLine to theSubject
set theBody to choose file with prompt "Please select a file for the body of the email:"
set theMessageBody to read theBody as «class utf8»
tell application "Mail"
set theNewMessage to make new outgoing message with properties {subject:theSubjectLine, content:theMessageBody, visible:true}
tell theNewMessage
make new to recipient with properties {address:theTo}
end tell
end tell

How to set from email address when sending email by automation with outlook

In a legacy vb6 program I am sending email using the full version of outlook (not outlook express) using the code below. Works great.
Now the user wants the 'from:' address to be different for different uses so that when the email is replied to the response will show up in that users inbox in outlook. Currently the email from is the main business email address.
I thought that was an easy fix; I just need to set the .from property in the OutMail object, however it seems there is no '.from' property in the OutMail object. (It may be called something else?)
So at this point I'm wondering how it works now, with no .from specified, and I assume the user has multiple email accounts setup in outlook, it is using the main email for the business, not individual users.
How can I specify a from email address using this technique?
Dim mOutlookApp As Object
Set mOutlookApp = GetObject("", "Outlook.application")
Dim olNs As Object
Set olNs = mOutlookApp.GetNamespace("MAPI")
olNs.Logon
Dim OutMail As Object
Set OutMail = mOutlookApp.CreateItem(0)
'Set the To and Subject lines. Send the message.
With OutMail
.To = txtTo
.CC = txtCC
.Subject = txtSubjext
.HTMLBody = txtBody & vbCrLf
Dim myAttachments As Object
Set myAttachments = .Attachments
vAttach = Split(mAttachments, ",")
For i = 0 To UBound(vAttach)
myAttachments.add vAttach(i)
Next i
Dim myFolder As Object
Set myFolder = olNs.GetDefaultFolder(5) 'olFolderSent
Set .SaveSentMessageFolder = myFolder
StatusBar1.Panels(1).Text = "Status: Sending"
.send
End With
If all you care about is that the reply goes to the right mailbox, set that email address as the reply address. You can do that using Mailtem.ReplyRecipients.Add.

How to activate a Word file inserted programmatically into an Outlook email body?

I have a .Net desktop program that uses Interop to send emails via Outlook. When sending to multiple recipients it uses an email per recipient as the salutation is substituted for personalization. The body of the emails is inserted from a Word file as follows (shortened for clarity):
objOutlook = New Outlook.Application
objItem = objOutlook.CreateItem(Outlook.OlItemType.olMailItem)
Dim inspector As Outlook.Inspector = objItem.GetInspector
Dim wordDoc As Word.Document = inspector.WordEditor
wordDoc.Activate() 'Not sure what this does but seems to be needed !
'Insert file contents into body of email.
Dim currentRange As Word.Range = wordDoc.Application.Selection.Range
currentRange.InsertFile(fileName,,,, False)
If at this point I now do an ObjItem.Display, followed by ObjItem.Send then it works perfectly. The Word file contents are put into the email body and the email sent.
But if I simply do an ObjItem.Send (without the .Display) then the email is sent with a blank body.
How do I force the email body to be set with the Word content without having to show the email ?
Try to call the Save method of the MailItem class.
Also you may try to save the document to a file in the HTML format and then set the HTMLBody property of the MailItem class.

Modifying the content type of an attachment in a CDO.Message object

When I try to add an MHTML file as an attachment to an email message in VBScript, the ContentMediaType is incorrectly set to "message/rfc822" (RFC 822). From what I understand, this is correct according to Microsoft, but is incorrect according to RFC 2557 which states that it should be "multipart/related". This is a problem, because most (if not all) mail clients interpret "message/rfc822" as an email message. Since the file extensions ".mht" and ".mhtml" do not match any valid file extension of an email message, the mail client appends one of ".msg", ".eml", etc. to the filename. When a user opens the attachment, it opens as an email message and doesn't display correctly since an MHTML file and an email message are saved differently.
Sub SendEmail(FromAddress, ToAddress, Subject, Body, Attachment)
Call Err.Clear
On Error Resume Next
Schema = "http://schemas.microsoft.com/cdo/configuration/"
Set Configuration = Sys.OleObject("CDO.Configuration")
Configuration.Fields.Item(Schema + "sendusing") = 2
Configuration.Fields.Item(Schema + "smtpserver") = SMTPServer
Configuration.Fields.Item(Schema + "smtpserverport") = 25
Configuration.Fields.Item(Schema + "smtpauthenticate") = 1
' Configuration.Fields.Item(schema + "sendusername") = ""
' Configuration.Fields.Item(schema + "sendpassword") = ""
Call Configuration.Fields.Update
Set Message = Sys.OleObject("CDO.Message")
Set Message.Configuration = Configuration
Message.From = FromAddress
Message.To = ToAddress
Message.Subject = Subject
Message.HTMLBody = Body
If Not IsEmpty(Attachment) Then
'CDO.Message.AddAttachment doesn't set the correct content media type for an MHTML file.
Call Message.AddAttachment(Attachment)
End If
Call Message.Send
End Sub
When I run this code, Message.Attachments.Item(1).ContentMediaType is set to "message/rfc822". I need it to be "multipart/related" if Attachment (a string) ends with ".mht" or ".mhtml" (case-insensitive). I can do this with the following code.
If Len(Attachment) >= 4 And InStr(Len(Attachment) - 3, Attachment, ".mht", vbTextCompare) Or Len(Attachment) >= 4 And InStr(Len(Attachment) - 5, Attachment, ".mhtml", vbTextCompare) Then
Message.Attachments.Item(1).ContentMediaType = "multipart/related"
End If
For some unknown reason, this undefines the attachment from Message.Attachments.
I've looked at manually adding the attachment per these instructions, but when I call Message.Attachments.Item(1).Fields.Update, the object becomes undefined. I think setting the attachments's ContentMediaType, implicitly invokes it's Fields's Update method which is what I think is responsible for this unexpected behavior.
How can I get around this and send an MHTML file with the "multipart/related" content type while maintaining the proper file extension?
So your problem is that at least some email clients save MHTML attachment incorrectly if content type for the attachment is set as content-type="message/rfc822".
First, it is worth noting that your analysis of a root cause of the issue is flawed. You appear to be confused by where multipart/related MIME type comes into play. As a matter of fact, RFC 2557 does NOT state that body part corresponding to MHTML attachment must have content-type="multipart/related". Instead, MIME multipart/related is internal structure of MHTML file itself. Quoting Wikipedia article:
The content of an MHTML file is encoded as if it were an HTML e-mail message, using the MIME type multipart/related.
I.e. if you open MHTML file with text editor, you should see the following:
Content-Type: multipart/related; ...
Microsoft states that MHTML files should be served with content-type="message/rfc822" in KB937912. This is exactly what CDO does by default when you attach such file via AddAttachment method. I believe such behavior does not contradict RFC 2557 in any way. As per the RFC:
There are a number of document formats... that specify
documents consisting of a root resource and a number of distinct
subsidiary resources referenced by URIs within that root resource.
There is an obvious need to be able to send such multi-resource
documents in e-mail [SMTP], [RFC822] messages.
The standard defined in this document specifies how to aggregate such
multi-resource documents in MIME-formatted [MIME1 to MIME5] messages
for precisely this purpose.
To recap, you definitely should not set content type of MHTML attachment to multipart/related.
While message/rfc822 seems to be the way to use with MHTML files, it obviously triggers the problem you described in the question. I tested with Outlook 2010 and OWA 2010, and was able to reproduce it.
Alternative content types that are used by various email clients for MHTML attachments are application/octet-stream and application/x-mimearchive. These two didn't exhibit the problem in my tests.

Remove Confirmation Box vbs script

I have the following code where i could send mail through my configured outlook.
I can run this vbs using a rule in my outlook which in turn send a mail to the email specified in the script
But i am getting a confirmation box asking a virus or not while running this script to send a mail.
How to get rid of this confirmation box to make always allow to send mails.
Dim ToAddress
Dim MessageSubject
Dim MessageBody
Dim MessageAttachment
Dim ol, ns, newMail
ToAddress = "John.Smith#place.com" ' change this...
MessageSubject = "My Subject"
MessageBody = "DATA"
Set ol = WScript.CreateObject("Outlook.Application")
Set ns = ol.getNamespace("MAPI")
ns.logon "","",true,false
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.Body = MessageBody & vbCrLf
' validate the recipient, just in case...
Set myRecipient = ns.CreateRecipient(ToAddress)
myRecipient.Resolve
If Not myRecipient.Resolved Then
MsgBox "unknown recipient"
Else
newMail.Recipients.Add(myRecipient)
newMail.Send
End If
Set ol = Nothing
I believe your being hit by the now built-in security feature(s) that Microsoft put in place a couple years back with a security patch. The only way I know around it to is to digitally sign the code and then import the certicate that was used to sign that code into the certificate store, or better yet use the Redemption DLL. From the Redemption DLL site:
Outlook Redemption works around
limitations imposed by the Outlook
Security Patch and Service Pack 2 of
MS Office 98/2000 and Office
2002/2003/2007 (which include Security
Patch) plus provides a number of
objects and functions to work with
properties and functionality not
exposed through the Outlook object
model.
The DLL can be downloaded from here: http://www.dimastr.com/redemption/download.htm, and if you look around you can find several examples of how to use it. Here is one to get you started: http://www.utteraccess.com/forums/printthread.php?Cat=&Board=80&main=409393&type=thread
Also please note the peviously posted and answered questions:
How to avoid Outlook security alert when reading outlook message from C# program
Outlook nagging dialog about macro