Reply to opened Email - excel

So i have developed a tool wherein there are set of email templates that can be chosen for reply. I am trying to work the tool to reply to an active email or selected email.
The tool works when you have selected an email in outlook. The problem is it won't work when for example you have opened a saved email in a shared folder, instead of replying to that opened email (saved from the folder) the tool actually open the selected email in outlook instead.
Dim OutlookApp as object
Dim outlookmail as object
Set OutlookApp as GetObject(, "Outlook.application")
Set outlookmail = OutlookApp.ActiveExplorer.Selection.Item(1).ReplyAll
If checkbox1.value = true then
.bodyformat = olFormatHTML
.display
.HtmlBody = 'mssg
End if
End sub

Related

VBA Send Email From Non-Default Account

I am logged in with two Outlook Accounts, and I need to send an e-mail with the one that is not set to default.
I tried using .SendOnBehaulfOfName, but even though it showed me the right account prior to sending the email, after I sent it I saw that it was sent from the default email (which is the wrong one).
I also tried using .SendUsingAccount but it did not change the "From" email.
Dim OutlookApp As Object
Dim OutlookMail As Object
Dim ol As Outlook.Application
Set ol = New Outlook.Application
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
With OutlookMail
.To = lista_envio
.CC = ""
.BCC = ""
.Subject = assunto_email
.Display '
.HTMLBody = mensagem
.Attachments.Add (caminho)
.SendUsingAccount = ol.Session.Accounts("Contato")
End With
Set OutlookMail = Nothing
Set OutlookApp = Nothing
Set ol = Nothing
Using the For each Account in Outlook.Application.Session.Accounts method also doesn't work.
Any ideas on what is wrong?
Thanks
Make sure that such an account configured in Outlook. If you want to see changes on the Outlook UI you need to call the Display methods after setting all properties via the Outlook object model.
If the other account is configured in Outlook (not just an additional store) you need to choose the MailItem.SendUsingAccount property which sets an Account object that represents the account under which the MailItem is to be sent. You could find the following sample code which demonstartes how to use it:
Sub SendUsingAccount()
Dim oAccount As Outlook.account
For Each oAccount In Application.Session.Accounts
If oAccount.AccountType = olPop3 Then
Dim oMail As Outlook.MailItem
Set oMail = Application.CreateItem(olMailItem)
oMail.Subject = "Sent using POP3 Account"
oMail.Recipients.Add ("someone#example.com")
oMail.Recipients.ResolveAll
Set oMail.SendUsingAccount = oAccount
oMail.Send
End If
Next
End Sub
The SendOnBehaulfOfName property doesn't require an account configured in Outlook. Only permissions are required for sending on behalf of another person in Exchange.

Modify HTMLBody of Outlook Email, based on Template, from Excel

I am trying to modify the HTML body of an Outlook email, based on a template, from Excel VBA.
My code is:
Sub Email_Button()
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItemFromTemplate("S:\some\path\to\file\Email.oft")
With OutMail
.Importance = olImportanceHigh
.Subject = "Subject " & Date
.Attachments.Add Application.ActiveWorkbook.FullName
.HTMLBody = WorksheetFunction.Substitute(OutMail.HTMLBody, "%target%", "replacement")
.Display
End With
' *** TIDY UP ***
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
The question is very similar to this.
I get
Run Time Error 287. Application-defined or object-defined error
on the .HTMLBody modification line.
If I remove this line the email is displayed for the user to check before hitting send.
I have referenced the Microsoft Outlook 15 Object Library.
I added:
With OutMail
.bodyFormat = olFormatHTML
But got the same error on the Substitute line so I changed the substitute to:
.HTMLBody = "<HTML><BODY>Some HTML text here</BODY></HTML>"
And the body of the email was updated.
So the error is only present when trying to use substitute or its to do with the oft.
It looks like from the debugger that there is no HTML body:
I have confirmed that body type is set to HTML both programmatically:
and by opening the oft message and checking:
The cause of the issue can be related to the Substitute method, so I'd suggest running the following code to make sure everything works correctly:
Sub CreateHTMLMail()
Dim OutApp As Outlook.Application
Set OutApp = CreateObject("Outlook.Application")
'Creates a new email item and modifies its properties.
Dim objMail As Outlook.MailItem
'Create email item
Set objMail = OutApp.CreateItemFromTemplate("S:\some\path\to\file\Email.oft")
With objMail
'Set body format to HTML
.BodyFormat = olFormatHTML
.HTMLBody = "<HTML><BODY>Enter the message text here. </BODY></HTML>"
.Display
End With
End Sub
Another aspect is Outlook security prompts. Read more about that in the "A program is trying to send an e-mail message on your behalf" warning in Outlook article.
The most probable cause is Outlook Security.
For security purposes, the HTMLBody, HTMLEditor, Body and WordEditor properties all are subject to address-information security prompts because the body of a message often contains the sender's or other people's e-mail addresses.
You can find the security configurations in HKCU\Software\Policies\Microsoft\office\16.0\outlook\security\
(change 16.0 to your office version)
There are two values that you can check, promptoomaddressbookaccess and promptoomaddressinformationaccess
Change them to 2 (or ask your system administrator), restart Outlook and try again.
More info https://support.microsoft.com/en-za/help/926512/information-for-administrators-about-e-mail-security-settings-in-outlo

Excel VBA save email after sent, only show preview email not sent email

I want to save the email in my local folder, and I saw this link
https://www.mrexcel.com/forum/excel-questions/361751-vba-saving-email-only-after-send-pushed.html
which basically use the class module to save the email after sending it out.
However the problem is, the email saved is the preview email (email that is being displayed before you send the email) instead of sent email (email in which you cannot edit anything anymore)
Dim cls_OL As New clsOutlook
Public objMail_SentMsg As Object
Public Emailpath As String
Sub SendEmail()
Dim OutMail As Object
Set cls_OL.obj_OL = CreateObject("Outlook.Application")
cls_OL.obj_OL.Session.Logon
Set OutMail = cls_OL.obj_OL.CreateItem(0)
Set objMail_SentMsg = OutMail
Emailpath = "V:\test\emailname.msg"
With OutMail
On Error Resume Next
'Assume this all strings variables are fine
.HTMLBody = strmsgContent1 & strmsgContent2
.to = ToEmail
.CC = CC
.BCC = BCC
.Subject = Subject
.Display
End With
Set OutMail = Nothing
End Sub
Option Explicit
Public WithEvents obj_OL As Outlook.Application
Private Sub obj_OL_ItemSend(ByVal Item As Object, Cancel As Boolean)
objMail_SentMsg.SaveAs Emailpath
Set obj_OL = Nothing
End Sub
It saved the email succesfully but as mentioned, only saved the preview/display email not the sent email.
Thank you so much for your help.
Instead of ItemSend monitor the SentItems folder with ItemAdd.
Do not save objMail_SentMsg, save the item identified by ItemAdd as being added to the folder.
If necessary to differentiate mail not to be saved, set up some unique characteristic in the mail when it is created.

Sending from secondary email account in outlook

I have an excel spreadsheet with some VBA that will send a marketing email to everyone with an email address in a spreadsheet.
When I send it through my main account it works absolutely perfectly but when I try to send it through our 'marketing#' email account, it only sends back to that account.
The emails send from my account (correctly) and appear in my sent items and when they are received in the marketing# account, the reply address is marketing# so everything appears to be working. It is just not sending to the recipient.
I have permission to SendAs in Exchange and this works (if I try a different address I get an access denied error message) but cannot work out why this isn't working for me at all.
This is my code:
Private Sub StackOverflow()
Dim OlApp As Outlook.Application
Dim olMail As Outlook.MailItem
Set OlApp = New Outlook.Application
Set olMail = OlApp.CreateItem(olMailItem)
With olMail
.SentOnBehalfOfName = """Marketing"" <marketing#>"
.To = "Recipient"
.Subject = "Test"
.Body = "Test"
.Display
End With
Set olMail = Nothing
Set OlApp = Nothing
End Sub
What changes I need to make to this code to get it to send to the recipient and not back to the account I'm sending from?
This has now been solved and it's nothing to do with Excel or Outlook.
It seems that somehow, a rule was placed on the server that re-routed anything sent from this account to the account instead. Now this rule has been removed, the code works perfectly.

Is there a way to prevent autoresolve in outlook?

I am sending email by automating outlook from excel and have managed to bypass the pesky warning message about viruses using sendkeys (with inspector activate just prior to call to sendkeys).
Now I sometimes get a message about allowing access to contacts.
I have the email addresses for the recipients and don't need to access the contacts, but outlook autoresolve kicks in and then a pop up about allowing access to the contacts appears. This doesn't have the 5 second delay, but it still prevents the system being fully automated.
I'm trying to avoid using 3rd party tools like redemption and I was wondering if anyone has found a way to turn autoresolve off.
I've read posts on other sites suggesting turning off autocomplete and automatic name checking, but outlook still attempts to resolve the address when the mail is sent.
Any pointers would be gladly received.
Edit 24/08/13
I have heard that if you outlook 2007 and above and a correctly installed system with a Microsoft approved virus scanner you will not see the message, but I don't have control over the installation of programs on the users machines.
The code that I have tried includes
Function Mailit(byval sMessageTo as String, byval sSamplerCenter as String, byval sFileSpec as String)
Dim olApp As outlook.Application
Dim objMail As Outlook.MailItem
Dim blnOLOpen As Boolean
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
blnOLOpen = True
On Error Goto 0
If olApp Is Nothing Then
Set olApp = CreateObject("Outlook.Application")
blnOLOpen = False
End If
Set objMail = olApp.CreateItem(olMailItem)
With objMail
.To = sMessageTo
.Subject = sSampleCenter
.Attachments.Add sFileSpec
.Send
End With
This causes the warning message about viruses and causes a 5 second wait before a user can choose to send the mail. The sendkeys method I use is the same up to the With objMail but then does the following:
Dim myInspector As Outlook.Inspector
With objMail
.To = MessageTo
.Subject = SampleCenter
.Attachments.Add FileSpec
.Display
End With
Set myInspector = objMail.GetInspector
myInspector.Activate
SendKeys "%s", True
I also have some code for checking that the number of items in the sent folder has increased and waiting/calling the inspector and sendkeys function if it hasn't.
This method doesn't lead to the warning, but often results in a dialog box asking if the user wishes to allow access to their contacts.
In Outlook: Go to Options -> E-Mail and disable the checkbox "Resolve names automatically".

Resources