I have a created a custom outlook form using VBA.
I want send a mail and then close the form just by by clicking a button.
The mail should be sent and following that the form should get closed.
The have written the following code, but this close the form after prompting the user to save changes.
The user hast to close the prompted kinda MsgBox and then the form.
Sub cmdbut2_Click
Set myItem = Application.CreateItem(0)
myItem.Recipients.Add "Test Test"
myItem.Close 2
End Sub
I want to close the form without and prompted messages. How can I do this?
You should pass olSave value to MailItem.Close method.
Sub cmdbut2_Click
Set myItem = Application.CreateItem(0)
myItem.Recipients.Add "Test Test"
'myItem.Close 2 close with prompt for save
myItem.Close olSave
End Sub
Related
I have a macro that runs on the Application_NewMail event - but I've seen it have weird impacts if the user is currently composing an email or reply - sometimes crashing outlook and losing their progress.
Is there a way that I can detect whether the user is currently composing an email?
This would allow me to cancel the macro and avoid interrupting the user.
I was able to find bits and pieces from related questions, but nothing that took into account both the pop-up email editor and the inline-response. Here's the solution I pulled together (which seems to cover all bases):
Private Function IsUserEditing() As Boolean
' Check if the user is composing an email. Don't interrupt them if we are.
' 1. Check if the user has the pop-up email 'inspector' window open
If Not (Application.ActiveInspector Is Nothing) Then
Dim OpenWindow As Variant
Set OpenWindow = Application.ActiveInspector.CurrentItem
If TypeOf OpenWindow Is MailItem Then
Dim NewMail As MailItem
Set NewMail = OpenWindow
' Check if the mail they're viewing is not 'Sent' (i.e. being edited)
If Not (NewMail.Sent) Then
IsUserEditing = True
Exit Function
End If
End If
' 2. Check if the user is replying to an email using the 'inline response' feature
ElseIf Not (Application.ActiveExplorer.ActiveInlineResponse Is Nothing) Then
IsUserEditing = True
Exit Function
End If
IsUserEditing = False
End Function
It can be used like this:
Private Sub Application_NewMail()
Debug.Print "New mail received..."
' Check if the user is composing an email. Don't interrupt them if we are.
If IsUserEditing Then
Debug.Print "User appears to be composing an email. Cancelling..."
Exit Sub
End If
' Otherwise Proceed
PerformOnNewMailActions
End Sub
Hope this helps others!
I have a script to process emails. User's can kick this script off by using a form.
I want them to only be able to use the form if they have an email open and in focus. So how can I check that the CurrentItem in:
objApp.ActiveInspector.CurrentItem
Is an email and is not another open window?
To work with mail item that is open and has focus, use ActiveInspector method
Example blew to print subject if Item is Mailitem
Option Explicit
Sub Item_Info()
Dim Active_Item As Object
Set Active_Item = Application.ActiveInspector.CurrentItem
If TypeOf Active_Item Is Outlook.MailItem Then
Debug.Print Active_Item.Subject
End If
End Sub
Within Outlook 2013, if you want to add a custom flag to an item you get this dialog:
I am trying to figure out how to open this dialog using VBA? I can either open it for a selected e-mail item or if there is a way to open it directly and retrieve the date/data the user selected.
You can simulate pressing buttons with ExecuteMso
Private Sub AddReminderDialog_ExecuteMso()
Dim objItem As Object
On Error Resume Next
Set objItem = ActiveInspector.currentItem
On Error GoTo 0
If Err <> 0 Then
ActiveInspector.CommandBars.ExecuteMso ("AddReminder")
Else
ActiveExplorer.CommandBars.ExecuteMso ("AddReminder")
End If
End Sub
You can see the "AddReminder" when you hover over the button when adding to the Quick Access Toolbar or a ribbon.
I often send emails on behalf of another user. I'd like to use VBA to automatically CC that user every time I send an email from/on behalf of that user.
I'm not familiar with VBA for Outlook but I'm thinking you could write an if statement that says "if sending message from UserX, cc UserX". The code should run automatically any time an email is sent on behalf.
SentOnBehalfOfName is tricky. It is usually empty until the item has been sent.
With this code in ThisOutlookSession you should find it blank.
Private Sub Application_ItemSend(ByVal item As Object, Cancel As Boolean)
Dim myRecipient As Recipient
Debug.Print " item.SentOnBehalfOfName - " & item.SentOnBehalfOfName
If item.SentOnBehalfOfName = "someone#somewhere.com" Then
Set myRecipient = item.Recipients.Add("Someone Else")
myRecipient.Type = olCC
item.Recipients.ResolveAll
End If
End Sub
At least one way to get around this:
Sub createSentOnBehalf()
Dim objMsg As mailitem
Set objMsg = Application.CreateItem(olMailItem)
objMsg.SentOnBehalfOfName = "someone#somewhere.com"
objMsg.Display
Set objMsg = Nothing
End Sub
Sub replySentOnBehalf()
Dim objMsg As mailitem
Set objMsg = ActiveInspector.currentItem.reply
objMsg.SentOnBehalfOfName = "someone#somewhere.com"
objMsg.Display
Set objMsg = Nothing
End Sub
Edit: Just realized you could set the cc while creating / replying rather than waiting until ItemSend.
Edit2: Move the cc code from itemsend
Sub createSentOnBehalf()
Dim objMsg As mailitem
Dim myRecipient As Recipient
Set objMsg = Application.CreateItem(olMailItem)
objMsg.SentOnBehalfOfName = "someone#somewhere.com"
Set myRecipient = objMsg.Recipients.Add("Someone Else")
myRecipient.Type = olCC
objMsg.Recipients.ResolveAll
objMsg.Display
Set objMsg = Nothing
End Sub
This will do what you are looking for (It is the first Google results of "always CC myself Outlook")
http://www.extendoffice.com/documents/outlook/1108-outlook-auto-cc.html
Launch your outlook 2013 or 2010, and make sure that you are in the mail section. Then click Home > Rules > Manage Rules & Alerts.
After selecting Manage Rules & Alerts option, the Rules and Alerts dialog will popup. Under E-mail Rules, click New Rule option.
In the Rules Wizard, click Apply rule on messages I send then click Next to continue.
Then another dialog pops up.
(1.) In Step 1, check through the specified account box. In Step 2, please click on the word - specified.
(2.) And then click the Account drop down list to choose the account that you want to apply this rule.
After selecting the account, and click OK to return to the previous window, you will see the selected account showing in the Rules Wizard. Then click on Next button.
(1.) In this wizard, check Cc the message to people or public group box, and then click on people or public group in step 2.
(2.) In the Rule Address dialog box, double click your cc recipient to add the address to the To-> text box, (If I want to cc myself, I will select or type my own email address in the To-> column.), finally click OK.
It returns to the previous window, and you can see the cc recipient address appearing. Then click Finish button.
Now, it returns to the very beginning dialog, click OK button, then the cc rule will be created. If you don’t want to enable the rule, uncheck it.
Then after sending or forwarding an email message to others with your specified account, your account or your specific cc recipient will always receive the same message.
It looks like you need to handle the ItemSend event of the Application class. It is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks the Send button) or when the Send method for an Outlook item, such as MailItem, is used in a program. Note, the Cancel parameter allows to cancel the process of sending the email.
In the ItemSend event handler you can check out the SentOnBehalfOfName property of the item passed as a parameter and add the CC recipient using the Recipients property of the MailItem class. The Recipients collection provides the Add method for adding recipients.
Set myRecipient = myItem.Recipients.Add("Dan Wilson")
myRecipient.Type = OlMailRecipientType.olCC
After don't forget to call the Resolve or ResolveAll method of the Recipient class to resolve a Recipient object against the Address Book.
See How To: Fill TO,CC and BCC fields in Outlook programmatically for more information.
Set olMessage = olApp.CreateItem(olMailItem)
olMessage.To = strEmailTo
olMessage.CC = strEmailCC
olMessage.Subject = strSubject
olMessage.Body = strBody
olMessage.Importance = olImportanceHigh
olMessage.Sensitivity = olConfidential
olMessage.Display ' Line With Error
The above code is inside a button on my User Form. The error says
A dialog box is open. Close it and try again
When I place either code alone in a macro it works fine and opens a new email etc, but when I set the macro to load the userform and place the code under a commandbutton, it gives an error dialog box:
Run-time error '2147467259 (80004005)'
A dialog box is open. Close it and try again.
Outlook is already open and the macro is on the toolbar.
Module 1 (code)
sub email()
Load userform3
userform3.show
end sub
loads my menu fine
If you are running this entirely from Outlook (and not using another application to automate the sending of an email, i.e., from Excel or PowerPoint, etc.) then this is the problem:
You are displaying your UserForm3 modally. This means that the application is essentially on hold, while the form is displayed.
To avoid this error, display it modelessly, like:
userform3.show vbModeless
Note: This allows the user to interact with the Outlook Application while the form is displayed. This may not be desired, in which case I think you will have to close/hide the userform before you display the email. Just add Unload Me preceding the .Display command:
Set olMessage = olApp.CreateItem(olMailItem)
olMessage.To = strEmailTo
olMessage.CC = strEmailCC
olMessage.Subject = strSubject
olMessage.Body = strBody
olMessage.Importance = olImportanceHigh
olMessage.Sensitivity = olConfidential
Unload Me
olMessage.Display ' Line With Error
Both above methods avoid the error. Which one you elect to use depends on your specific needs.
Alternatively you could simply hide the form before executing the code:
Private Sub cmdButton_Click()
UserForm3.hide
Set olMessage = olApp.CreateItem(olMailItem)
olMessage.To = strEmailTo
olMessage.CC = strEmailCC
olMessage.Subject = strSubject
olMessage.Body = strBody
olMessage.Importance = olImportanceHigh
olMessage.Sensitivity = olConfidential
olMessage.Display ' Line With Error
End Sub
Hiding the UserForm bypasses the "Open Dialog Box" error and preserves user input. It's also simpler than dealing with VB modalities and prevents unwanted behaviour.