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
Related
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.
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 have a macro that creates a pdf and then send it to outlook to attach as a email but it tries to open the app instead of using the web based version. what code can I use to replace it with?
You need to save the workbook on the disk and then use the file path of the just saved file to add it as an attachment in Outlook. The source of the attachment can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment. For example:
Sub AddAttachment()
Dim myItem As Outlook.MailItem
Dim myAttachments As Outlook.Attachments
Set myItem = Application.CreateItem(olMailItem)
Set myAttachments = myItem.Attachments
myAttachments.Add "D:\Test.xlsx", _
olByValue, 1, "Test"
myItem.Display
End Sub
The code for automating Outlook desktop will not work for the browser out of the box.
I'm getting my Job logs to my Outlook email, where the subject contains most of the necessary details like what job, which day, what server and the status. I used Date as a parameter to get the email into Excel.
I'm trying to take it further. If a job fails generate a log either in the body of the email or as an email attachment.
For the log in the mail body I brought those to the sheet and parsed with the search term "Error" and captured the line.
For the attachment I can only save it in a folder and get the name in Excel. Is there any way I could read the content of the attachment, search for "Error" and bring only that line to the Excel sheet.
Option Explicit
Const AttachmentPath As String = "C:\users\maharaj\qalogs\"
Sub GetFromOutlook2()
Dim outlookAtch As Object
Dim NewFileName As String
NewFileName = AttachmentPath & Format(Date, "DD-MM-YYYY") & "-"
Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer
Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders("QALOGS")
i = 1
For Each OutlookMail In Folder.Items
If OutlookMail.ReceivedTime "GREATER THAN or EQUAL TO" = Range("start_Date").Value Then
Range("email_Subject").Offset(i, 0).Value = OutlookMail.Subject
Range("email_Date").Offset(i, 0).Value = OutlookMail.ReceivedTime
Range("email_Sender").Offset(i, 0).Value = OutlookMail.SenderName
Range("email_Body").Offset(i, 0).Value = OutlookMail.Body
outlookAtch.SaveAsFile NewFileName & outlookAtch.Filename
Range("email_attachment").Offset(i, 0).Value = outlookAtch
End If
i = i + 1
Next OutlookMail
Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing
End Sub
First of all, I'd recommend using the Restrict or Find/FindNext methods of the Items class instead of iterating over all items in the folder:
For Each OutlookMail In Folder.Items
If OutlookMail.ReceivedTime "GREATER THAN or EQUAL TO" = Range("start_Date").Value Then
Read more about these methods in the following articles:
How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
How To: Use Restrict method to retrieve Outlook mail items from a folder
Also, you may consider using the AdvancedSearch method of the Application class. The key benefits of using the AdvancedSearch method in Outlook are:
The search is performed in another thread. You don’t need to run another thread manually since the AdvancedSearch method runs it automatically in the background.
Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The Restrict and Find/FindNext methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook).
Full support for DASL queries (custom properties can be used for searching too). You can read more about this in the Filtering article in MSDN. To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the IsInstantSearchEnabled property of the Store class).
You can stop the search process at any moment using the Stop method of the Search class.
For the Attachment i can only able to save them in a folder and get give them name in the excel.
Yes, you are on the right avenue. The attached file should be saved on the disk first. Depending on the file extension you can automate an appropriate application for reading its content or just using IO operations for reading the content directly without involving Office applications.
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.