Create Mail and set focus to this Window - excel

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.

Related

Is it possible to have an excel macro open outlook in browser and populate the fields of a new message as well as attach a file?

Question: Is it possible to create a macro that opens outlook in a web browser and populates the fields of a new message as well as attach a file? The outlook portion of the current macro only opens outlook in a browser.
ActiveWorkbook.FollowHyperlink Address:="https://outlook.office365.com/mail/**shared mailbox address**"
Background: I am trying to update an excel macro that currently saves a pdf of the sheet, opens the outlook application, fills out the necessary fields and attaches the saved pdf to the email. This macro has worked fine, but we have recently moved to using a shared mailbox to send the message. Now the users have encountered problems sending from the shared mailbox using the outlook application. The solution is to use outlook in the browser (edge), but the macro I currently have can only open outlook in the browser and requires the user to fill out all the fields and find and attach the saved pdf. There have been problems with this and I was hoping there was a way to automate the process like our old macro would.
Old macro:
Set OlApp = CreateObject("Outlook.Application")
Set NewMail = OlApp.CreateItem(0)
On Error Resume Next
With NewMail
.To = ReportName
.CC = ""
.Subject = TempFileName
.Body = ""
.Attachments.Add FileFullPath '--- full path of the pdf where it is saved
.Display '.Send or use .Display to show you the email before sending it.
End With
On Error GoTo 0
Well, Yes and No.
Yes, you can get VBA to do this... but No, it won't be remotely as easy as running it through the desktop application.
A workaround that still uses desktop application, is to
Give all users that need to send the email access to this inbox from their own desktop apps.
Use the ".SendUsingAccount" property
Sub ExampleSub()
Dim OLApp As Object
Dim NewMail As Object
Set OLApp = CreateObject("Outlook.Application")
Set NewMail = OLApp.CreateItem(0)
On Error Resume Next
With NewMail
.SentOnBehalfOfName = "MySecondaryAddress#Domain.com"
.To = "someaddress#gmail.com"
.CC = ""
.Subject = "Example Subject"
.Body = "Good Morning..."
'.Attachments.Add FileFullPath '--- full path of the pdf where it is saved
.Display '.Send or use .Display to show you the email before sending it.
End With
On Error GoTo 0
End Sub

Automate Outlook mail scheduler

I came across a code in Excel VBA which sends mail via Outlook based on given time ranges in the code. The "TO, CC, Subject and Mail Body" are all input in excel only.
The code works fine but .Send giving error 287-Application or object defined error. The code works well in my colleague's laptop, so I am guessing it is some setting error in my outlook or excel. I keep my outlook open while executing the code. The code is written below. Can anyone point to what might be wrong?
Sub Send_Email()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Sheet1")
'''''''''' Update Next ''''''''''''''
Call Update_Next_Schedule_Time
Application.OnTime sh.Range("K20").Value, "Send_Email"
''''''''''''''''''''''''''''''''''''''
Dim oa As Object
Dim msg As Object
Set oa = CreateObject("outlook.application")
Set msg = oa.createitem(0)
With msg
.To = sh.Range("C2").Value
.CC = sh.Range("C4").Value
.Subject = sh.Range("C6").Value
.Body = sh.Range("C8").Value
.display
.send
End With
End Sub
I believe this is due to your Outlook security policy. Check if it allows programmatic access. If not, and you cannot convince your IT department to make any change, then you should automate the opened outlook app rather that creating a new Outlook instance. Also, are you trying to send the email to yourself, by any chance?

Attaching unsaved Excel file to email

I have an Excel form for users to fill and send as an attachment (without having to save it locally on their computer).
The code works.
Dim Names()
Names = Array("testmail#gmail.com")
ActiveWorkbook.SendMail _
Recipients:=Names(), _
Subject:="Test subject"
I would like the email just to be created and not sent until the users have attached an additional file (found on their local computer).
I wrote the following code:
Dim olapp As Object
Dim olmail As Object
Dim wb As Workbook
Set olapp = CreateObject("outlook.application")
Set olmail = olapp.CreateItem(olMailItem)
Set wb = ActiveWorkbook
With olmail
.To = "testmail#gmail.com"
.Subject = "Test Subject"
.Body = ""
.Attachments.Add wb.FullName
.Display
My problem is that only the latest saved copy will be attached to the created email, and since the users will not have the form/Excel file stored locally on their computer, an empty form (or the last saved form) will be attached to the email.
Is there any way for an email to be created, with a copy of the workbook, but not to send it?
i tried this one-liner in the immediate window and it managed to send an unsaved file: Application.Workbooks("Book2").SendMail("my.email#company.com","Test Subject") you can use wb from your code instead of Application.Workbooks("Book2") in my example. Note that this will send the email, without the possibility to edit it.

VBA Code to select Sender and Signature

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.

Send an email from Excel 2007 VBA using an Outlook Template & Set Variables

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.

Resources