Good day,
I'm trying to automate my outlook.
I have an excel list with mails and a code is composing drafts for me.
But here is a problem when I try to send a mail in web version it states
error: "This message can't be sent right now. Please try again later."
Mails appear correct in desktop version, but not in web.
Sub Box()
Dim objOL As Object
Set objOL = CreateObject("Outlook.Application")
Dim Name As Namespace
Set Name = objOL.GetNamespace("MAPI")
Dim Msg As MailItem
Set Msg = objOL.CreateItemFromTemplate
Msg.To = Cond.Cells(i, 2).Text
Msg.CC = Cond.Cells(i, 3).Text
Msg.Subject = PP.Range("F1").Text
Msg.HTMLBody = "<HTML><BODY>" & Cond.Cells(i, 4) & Cond.Cells(i, 5) & "</BODY></HTML>"
I've tried to. property I've tried Recipient.Add brackets <>.
Can you help me if you know how, please
First, you need to specify the file path of the template:
Set Msg = objOL.CreateItemFromTemplate
The Application.CreateItemFromTemplate method creates a new Microsoft Outlook item from an Outlook template (.oft) and returns the new item. It requires at least one parameter passed - the path and file name of the Outlook template for the new item.
Second, to set up To and CC properties:
Msg.To = Cond.Cells(i, 2).Text
Msg.CC = Cond.Cells(i, 3).Text
These properties expect to be set to a semicolon-delimited string list of display names for the To or CC recipients for the Outlook item. Make sure that you pass a valid string.
A better approach is to use the Recipients property of Outlook items. The Recipients.Add method creates a new recipient in the Recipients collection. Then don't forget to use the Resolve or ResolveAll methods. They attempt to resolve one or all the Recipient objects in the Recipients collection against the Address Book, for example:
' now we add new recipietns to the e-mail
recipientTo = recipients.Add("Eugene Astafiev")
recipientTo.Type = Outlook.OlMailRecipientType.olTo
recipientCC = recipients.Add("Someone Else")
recipientCC.Type = Outlook.OlMailRecipientType.olCC
recipientBCC = recipients.Add("eugene.astafiev#somedomain.com")
recipientBCC.Type = Outlook.OlMailRecipientType.olBCC
retValue = recipients.ResolveAll()
Read more about that in article that I wrote for the technical blog - How To: Fill TO,CC and BCC fields in Outlook programmatically.
Related
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.
I wrote Excel VBA code to generate emails from a report that is downloaded into Excel. It saves to the "drafts" folder in Outlook.
I want to save to another folder within drafts to keep them separate from my regular drafts.
This is what I have.
With objMail
.To = rngTo
.Subject = "Next 2 Weeks Orders"
.HTMLBody = intro & vbNewLine & po & signiature
.Save
End With
There are several ways to create new items in Outlook. If you need to save a new item to a specific folder in Outlook you choices are:
Create a new item using the CreateItem method and then use the MoveTo method to place the item to the target folder.
Use the Items.Add method which places new items to the folder where the Items collection comes from. For example:
nSpace = OutlookApp.GetNamespace("MAPI")
targetFolder = nSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
folderItems = targetFolder.Items
mail = folderItems.Add(Outlook.OlItemType.olMailItem)
You can find all these ways described with code samples in the How to create and show a new Outlook mail item programmatically: C#, VB.NET article.
What is your code that actually creates objMail? Are you using Application.CreateItem? Use MAPIFolder.Items.Add on a specific folder instead. Assuming "My custom subfolder" is a child folder of the Drafts folder:
set folder = objOutlook.Session.GetDefaulFolder(16) 'olfolderDrafts
set folder = folder.Folders("My custom subfolder")
set objMail = folder.Items.Add
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!
Updated per suggestions from Jeeped:
I am looking for a method of creating a set of emails fitting the following parameters:
each email will be personalized to the recipient and based off a template letter set by my supervisor.
There will be a greeting line with their name and title, along with the names of the departments they oversee.
each email will have a set of documents specific to that recipient.
they should be saved to file for final inspection before they are sent.
column 5 that is not referenced in the code below is the column containing the department name.
The closest I have come is the following code:
Sub send_template_w/attachments()
On Error Resume Next
Dim o As Outlook.Application
Set o = New Outlook.Application
Dim omail As Outlook.Mailitem
Set omail =.Createitem(olMailitem)
Dim I As Long
For i=2 To Range(“a100”).End(xlUp).Row
With omail
.Body = “Dear “ & Cells(i,1).Value
.To = Cells(i,2).Value
.CC = Cells(i,3).Value
.Subject = Cells(i,4).Values
.Attachments.Add Cells(i,6)
.Attachments.Add Cells(i,7)
.SaveAs Environ("HOMEPATH") &; "\My Documents\" & Cells(i,2).Value
End With
Next
End Sub
So far, this code will generate and save an email but what I want to do is use a present email template for these emails--either by adding the greeting at the beginning and department name into the body of the the email to be sent out. Can this be done through a word or Outlook document and if so, how?
Create a model of the mail. "Save As" to an .oft file. For example MyTemplate.oft
Instead of
Set omail =.Createitem(olMailitem)
there is
Set omail = o.CreateItemFromTemplate("C:\MyTemplate.oft").
To add the entries from the Excel sheet you could include unique placeholders in the body of the template then Replace with Excel values.
I have a list of data, let's say client information (Name, Email, Amount Owing etc.), stored in an Excel worksheet. My aim is to click a button in Excel and send each client their information in an Outlook Template.
create a mail object
set the mail object to the template file
setting and then filling in the template with data about the current client - mostly stuck here, not sure how to specify variables in a template and then relate to them in VBA
save to drafts for later review/send
e.g. Dear << clientname >> = Dear John Smith
My code thus far:
Dim myOlApp As Outlook.Application
Dim MyItem As Outlook.MailItem
Set myOlApp = CreateObject("Outlook.Application")
Set MyItem = myOlApp.CreateItemFromTemplate("C:\egTemplate.oft")
With MyItem
.To = Worksheets("Clients").Range(1, 2)
.Subject = "Monthly bill"
'Refer to and fill in variable items in template
.Save
End With
Set MyItem = Nothing
Set MyOlApp = Nothing
Here is what you can do :
With MyItem
'Refer to and fill in variable items in template
.Body = Replace(.Body, "<< clientname >>", Worksheets("Clients").Range(1, 2))
End With
or, if your mail is in HTML:
With MyItem
'Refer to and fill in variable items in template
.HTMLBody = Replace(.HTMLBody, "<< clientname >>", Worksheets("Clients").Range(1, 2))
End With
Tested successfully on Excel / Outlook 2007
This is a perfect job for mail merge. If you want to do it programmatically, see
Mail Merge in Word+Excel using VBA
Or you could simply do it manually (from Word), inserting merge fields and then selecting your workbook as the data source. You can merge to email and Outlook will send out personalized emails to each recipient's email using the information from each row/record.