VBA Code to select Sender and Signature - excel

In Excel I'm using code like this to begin an e-mail message through Outlook:
Set mOutlookApp = GetObject("", "Outlook.application")
Set OutMail = mOutlookApp.CreateItem(0)
With OutMail
.To = "blahblah#blah.com"
.Subject = "More BLAH here"
.HTMLBody = "Message Text" & .HTMLBody ' This preserves the Signature in the message.
.Display
End With
Normally, when I send a (manual) e-mail, I can choose to send it from an address other than my normal one (by pressing the "From" button).
Also, I can choose from one of several signatures I have saved.
How can I accomplish these feats in VBA code?

Change 1 to the account number from which you want to send
.SendUsingAccount = OutApp.Session.Accounts.Item(1)
As far as the signatures are concerned, they are stored in %USERPROFILE%\Application Data\Microsoft\Signatures you can loop through the signatures and choose the relevant one

Set the MailItem.SendUsingAccount property, call Display (at that point Outlook will insert the signature), read the HTMLBody property (it will now contain the signature), merge it with your own data (note that 2 HTML strings cannot be simply concatenated), set the HTMLBody property.

Related

How to send multiple hyperlinks with mail in Excel VBA, depending on the cell value

There is and light order managment system where you can order a few items. When you have chosen your order, you click a button, and the rows that is order and number of items is copied to a new confirmation sheet. This sheet is then supposed to be sent to a chosen reciever by oMail. This works fine.
But in one of the columns there is a certificate on PDF(hyperlink), that is linked to a server(local file on my computer:))
I'm trying to send this PDF's as multiple hyperlinks in the mail, but that is no sucess :p. The thing I want to do is check if the cell is empty, if not, attach the hyperlink(s) in my stringbody. And send the mail.
Here is my code:
I use a function also to make the range to html format:
It's more useful to edit e-mail body via GetInstector
For example:
Dim myInspector As Object'inspector object
Dim wdDoc As Object''document
Dim myitem As Object'mailitem
Set myitem = CreateObject("Outlook.Application").CreateItem(0)'creating mailitem from Excel
Set myInspector = myitem.GetInspector'set inspector to edit mailitem
Set wdDoc = myInspector.WordEditor'set MS Word to edit mailbody
With myitem'here you are setting the e-mail properties
.To = Email'fill to e-mail
.Subject = Subja'Subject
.Display'display mailitem etc.
End With
'here edit the body with MS Word methods
wdDoc.Range.Paragraphs.Add
wdDoc.Range.Paragraphs.item(1).Range.Text = "Something you want in 1st paragraph" & vbCr
wdDoc.Range.Paragraphs.Add
wdDoc.Range.Paragraphs.item(2).Range.Text = "Something else you want in 2nd paragraph " & vbCr
So, actually you edit e-mail programmatically the same as you do it in Word. It makes useless long and complicated string with HTML-tags. You add MS Word objects and forms.

How can I reply to email without losing the previous email chain?

I have code to populate email from Excel using VBA. I want to retain previous correspondence and also add my signature to the end of the email.
Code that opens the current Outlook item and populates it. The variable ChsBody is assigned earlier.
If OutItem.Class = olMail Then
Set replyEmail = OutItem.ReplyAll
With replyEmail
.Signature
.Body = ChsBody
.Display
End With
Else
MsgBox "ERROR: Make sure email is selected"
End If
Would it be possible to retain the email chain and add my response as a string?
Would it be possible to retain the email chain and add my response as a string?
Change
.Body = ChsBody
to
.HTMLBody = ChsBody & "<br>" & .HTMLBody
In the code you override the Body content with your own:
.Body = ChsBody
Instead, if you want to preserve the existing content, you need to append the string:
.Body = ChsBody & .Body
Note, there are three main ways of dealing with message bodies in Outlook:
Body.
HTMLBody.
The Word editor. The WordEditor property of the Inspector class returns an instance of the Word Document which represents the message body.
See Chapter 17: Working with Item Bodies for more information.

Create Mail and set focus to this Window

I have VBA code within Excel to create Outlook email. The email does not always get brought to the front.
I added OApp.ActiveExplorer.Activate, but this brings the Outlook application forward rather than the new email.
The new email window will have a dynamic window name, making it difficult to locate by code.
Set OApp = CreateObject("Outlook.Application")
Set email= OApp .CreateItem(0)
With email
.To = address
.Subject = subject
.Display
End With
OApp.ActiveExplorer.Activate
Call email.GetInspector.Activate after calling Display.

Need to send separate emails to multiple recipients from excel list

I have an excel list of people. I want to send emails to all as separate mails. Need to dynamically change the subject,body and recipients.
I have tried using vba and doing it. But i do not know how to dynamically change the subject and body. Also how can i enter multiple lines in the body?
i do not want all recipients to be sent the same mail.Image shows the field names in excel
I need to have first name and first letter of last name in subject. And in body first line includes Hi firstname..And personal mail goes in the "to" field and professional as "cc"
Try it this way:
'Initialize objects
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
.To = ws.Range("A1") 'Assuming TO mail addresses are located here and separated with ";"
.CC = ws.Range("B1") 'Assuming TO mail addresses are located here and separated with ";"
.Subject = ws.Range("C1") 'Assuming subject is declared here
.HTMLBody = ws.Range("D1") 'Assuming body is declared here
If address_attachment_line.Value <> "" Then
.Attachments.Add FilePath & FileName
End If
.Display
End With
Through storing the dynamic information in the ranges referenced by the code, you can control the single mails.
Furhermore to make linebreaks in the body text, just use the tag <br> as it is interpreted as HTML content.
Hope this helps you!

Macros to Send attach different files to multiple individual emails, vba

I am a computer technician, not a programmer, but in my new job I have been asked to finish a macros in excel (vba).
It consists of a list of id card numbers in one of the columns (the number of cells is variable each time it is used, one day you can put 20 people and another 12 for example), and emails in another column.
In a folder there are some pdf documents whose name is the id card of the person that appears in the excel.
What they ask me is that, being ordered the id card in alphabetical order, take the id card and email. The id card will serve to find your corresponding pdf and add it as an attachment with the idea of sending it by email, to whom? there the cell is used with the email data. This has to be done with each of the existing rows, take pdf file to attach it and send email to the address of that same row until there are no more rows on the sheet.
Can someone tell me how to do that or tell me the functions I need?
Thank you.
Graphical idea:
The macro is currently set to .Display the email and not send. After you have finished running tests you will want to change this to .Send to actually send the email.
You will also need to update the value of strLocation. Inside the quotes is where you will need to put the location of the folder that houses all of your target PDFs.
The order of your cells doesn't matter here as long as each row is associated to one individual.
Hopefully these emails are internal - you should not use this for external mailing lists as you cannot offer the option to unsubscribe. Outlook may flag/ban your account if you are suspected of spam.
This assumes the values in Column C are actual email addresses that will be recognized as is by Outlook. (urdearboy#email.com)
Sub CorpCard()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo cleanup
For Each cell In Columns("C").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*#?*.?*" Then
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.SentOnBehalfOfName = "[your associated Outlook email here]"
.to = cell.Value
.Subject = "Subject goes here"
.Body = "Hi " & Range("B" & cell.Row).Value & "," _
'Body to be patsed here
strLocation = "C:\Users\urdearboy\Desktop\File Name\" & Cells(cell.Row, "D").Value & ".pdf"
.Attachments.Add (strLocation)
.Display
'.Send
End With
On Error GoTo 0
Set OutMail = Nothing
End If
Next cell
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub

Resources