Checking if Outlook mail item is active - vba

The following code is the code for sending Outlook mail:
Sub SendEmail()
Dim OutlookApp As Object
Dim MItem As Object
Set OutlookApp = CreateObject("Outlook.Application")
Dim Sendrng As Range
Set Sendrng = Worksheets("Test").Range("A1").SpecialCells(xlCellTypeVisible)
Sendrng.Copy
Set MItem = OutlookApp.CreateItem(0)
With MItem
.To = "test#email.com"
.Subject = "Test"
.Display
Threading.Thread.Sleep(2000)
End With
End Sub
How can I check if the Outlook mail item is active?
I am looking for a code like this:
.Display
Threading.Thread.Sleep(2000)
If MItem is not active then
exit sub
End if
In other words, display is still displaying?
Because I don`t want the users to close the opened Outlook mail screen.

So essentially you want to find out when the message is closed? Call Display specifying TRUE as the parameter (it defaults to false if not specified) - that will cause Display to be modal. The line .Display needs to be changed to .Display(true)

Related

Make outlook-instance visible in Word-VBA?

I'm making a word macro that runs on Document_Close(). I want the macro to open a outlook- "new message" window with no recipient, no subject, just a floder attached including some saved PDF's of the word template.
I've tried to do it this way:
Sub Document_Close()
ActiveDocument.MailMerge.MainDocumentType = wdNotAMergeDocument
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
On Error Resume Next
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If
Set oItem = oOutlookApp.CreateItem(olMailItem)
With oItem
.Subject = "New subject"
.Attachments.Add Source:="C:\temp\PDFSaves", Type:=olByValue
End With
End Sub
I know it opens an instance, because I printed it once, although I'd like it to pop UP on te screen so that i can manually enter recipient etc, and confirm that the correct PDF-folder was attached.
It would be nice if there was a oIten.Visible command...
Use the MailItem.Display Method.
oItem.Display

VBA outlook - send email and categorize the CC email

I have been using public folder and sending emails on outlook. Whenever we send emails with the public folder email address (e.g info#company.com), we will CC info#company.com. So that we will keep the email in our public folder.
I have created some VBA code to send outlook email automatically.
My question is: Is it possible to automatically categorize (e.g. customer-inquiry) the email in our public folder when I send the email without showing the category to recipient (as .
Sub outlookActivate1()
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Set OutApp = CreateObject("Outlook.Application")
With OutMail
.BCC = ""
.BodyFormat = olFormatHTML
.Categories = "customer-inquiry"
.ShowCategoriesDialog
.Save
.display
End With
On Error GoTo 0
Set OutMail = Nothing
Set oItem = Nothing
End Sub
No, it is not. You may consider adding a user property to the mail item instead. See UserProperties for more information.

Sending Emails from Excel VBA - Names Not Recognized

I am using the below code to send an email from excel using outlook:
Private Sub SendEmail()
Set OutlookApp = CreateObject("Outlook.Application")
Set OlObjects = OutlookApp.GetNamespace("MAPI")
Set newmsg = OutlookApp.CreateItem(olMailItem)
newmsg.Recipients.Add ("name#domain.com; name2#domain.com; name3#domain.com")
newmsg.Subject = "Test Mail"
newmsg.Body = "This is a test email."
'newmsg.Display
newmsg.Send
End Sub
The code works just fine, however I get the below error from Outlook when trying to send the email:
ErrorScreen http://im58.gulfup.com/GRENlB.png
The strange thing is that if I leave the new message open for two or three minutes the names automatically get resolved:
Working http://im74.gulfup.com/qmOYGQ.png
However this doesn't suit me as I don't want the message to be displayed before it's sent. I am looking to have it send as soon as I run the code.
Any suggestions or workarounds will be appreciated.
As a side note: I have tried enabling the "Allow commas as email separators" option in outlook, and then using the commas instead of the semicolons, but I am still facing the same problem.
UPDATE:
Below is the working code, as per Dmitry Streblechenko's answer:
Private Sub SendEmail()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OlObjects = OutApp.GetNamespace("MAPI")
Set OutMail = OutApp.CreateItem(olMailItem)
On Error Resume Next
With OutMail
.To = ("name#domain.com; name2#domain.com; name3#domain.com")
.Subject = "Test Mail"
.Body = "This is a test email."
'.Display
.Send
End With
End Sub
You cannot pass multiple names to Recipients.Add - you get a single recipient with the name of "name#domain.com; name2#domain.com; name3#domain.com". Either call Recipients.Add 3 times once for each recipient or set the To property - it will parse multiple names.
You should add a call to ResolveAll to explicitely resolve all recipients.
Otherwise, resolution is done automatically after a short waiting period.
Example:
Sub CheckRecipients()
Dim MyItem As Outlook.MailItem
Dim myRecipients As Outlook.Recipients
Dim myRecipient As Outlook.Recipient
Set myItem = Application.CreateItem(olMailItem)
Set myRecipients = myItem.Recipients
myRecipients.Add("Aaron Con")
myRecipients.Add("Nate Sun")
myRecipients.Add("Dan Wilson")
If Not myRecipients.ResolveAll Then
For Each myRecipient In myRecipients
If Not myRecipient.Resolved Then
MsgBox myRecipient.Name
End If
Next
End If
End Sub
Code copied from here.

Forward current message as attachment then delete original message

I get a lot of spam messages on my work Outlook 2010 account. I am provided with our spam blocker address to forward the spam (as an attachment) to.
I'd like to click on an icon on the ribbon (I already have this) and have VBA code run that takes the current message, attaches it to a new message, adds an address to the new message, sends the new message and then deletes the original message. (Deleting can be either putting the message in the "Deleted Items" folder or permanently deleting it.)
SOLVED!!!!
Here is code that does exactly what I want. I found it on the net and modified it to meet my needs.
Sub ForwardAndDeleteSpam()
'
' Takes currently highlighted e-mail, sends it as an attachment to
' spamfilter and then deletes the message.
'
Set objItem = GetCurrentItem()
Set objMsg = Application.CreateItem(olMailItem)
With objMsg
.Attachments.Add objItem, olEmbeddeditem
.Subject = "SPAM"
.To = "spamfilter#schools.nyc.gov"
.Send
End With
objItem.Delete
Set objItem = Nothing
Set objMsg = Nothing
End Sub
Function GetCurrentItem() As Object
On Error Resume Next
Select Case TypeName(Application.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = Application.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = Application.ActiveInspector.CurrentItem
Case Else
' anything else will result in an error, which is
' why we have the error handler above
End Select
Set objApp = Nothing
End Function
You can use this to go through a selection of emails, rather than just one by adapting the code as follows
Sub ForwardSpamToNetworkBox()
On Error Resume Next
Dim objItem As Outlook.MailItem
If Application.ActiveExplorer.Selection.Count = 0 Then
MsgBox ("No item selected")
Exit Sub
End If
For Each objItem In Application.ActiveExplorer.Selection
Set objMsg = Application.CreateItem(olMailItem)
With objMsg
.Attachments.Add objItem, olEmbeddeditem
.Subject = "SPAM"
.To = "spam#host.co.uk"
.Send
End With
objItem.Delete
Next
Set objItem = Nothing
Set objMsg = Nothing
End Sub
This was created with info from http://jmerrell.com/2011/05/21/outlook-macros-move-email
Ideally, instead of deleting, I'd move it to a subfolder called "Submitted" but I can't get that to work in Public Folders

VBA Outlook Mail .display, recording when/if sent manually

My code displays a message with basic subject, body, attachment. Next the user manually updates and customizes the message and should send it. I want to record when (if) the email is sent. Is this possible or any tips?
My environment is Office 2007 with an excel based macro going to Outlook.
[Excerpt]
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = Email '.CC =
.Subject = Subj
.BodyFormat = olFormatHTML
.Body = Msg '.HTMLBody = Msg
If Not FileAttach = vbNullString Then .Attachments.Add (FileAttach)
.Display
End With
This is entirely possible, using the _Send event in the Outlook.MailItem class.
The way I use it, I create a class called EMail Watcher, so when I create the email and do the .Display, I then create a new EMailWatcher object and tell it to watch that email for send, then report back when it happens.
Here's the class as I use it. Basically, I also optionally can set the BoolRange so that if the user sends the email, that Excel range gets updated with True. I can also have the class update an Excel range with the time the email is sent.
Public BoolRange As Range
Public DateRange As Range
Public WithEvents TheMail As Outlook.MailItem
Private Sub TheMail_Send(Cancel As Boolean)
If Not BoolRange Is Nothing Then
BoolRange.Value = True
End If
If Not DateRange Is Nothing Then
DateRange.Value = Now()
End If
End Sub
And here's how I use it:
With oMail
.To = addr
.Subject = "CCAT eVSM Utilities License Code"
.Body = "Message body"
.Display
End With
Set CurrWatcher = New EmailWatcher
Set CurrWatcher.BoolRange = Range("G12")
Set CurrWatcher.TheMail = oMail
Hopefully that helps...