Outlook VBA - Create an email, allow the user to send, then execute further code - vba

I have an outlook Macro that collects some information from existing emails and a local database, then serves up a templated email with the information. The user can then review the email, make changes if they want to and then send or not sent (quit/cancel). Is there a way, I can keep the macro running and then execute more code if the user sends the email?
I've created some psuedo code below on how I'm hoping it might work:
Function CreateEmailThenExecuteCode()
Dim newEmail As MailItem
Set newEmail = Application.CreateItem(olMailItem)
newEmail.Display
'Allow user to review and send email
'If they 'send', then execute further code.
If Not Sent Then Exit Function
'Further code
End Function
I know that I can create a new macro that runs every time a user sends an email - but it would be much easier if I can keep the existing macro running, as otherwise I need a way of saving the data from the running macro.
I also know that I can create a custom user form that mimics an email user form - but I would prefer to keep the functionality of the full email user form, especially with access to email address lists etc.
Is it possible?

The Display method doesn't prevent your code from running until you pass true to make the Inspector window modal. The default value is false.
At the same keeping the code running after the Display call doesn't allow users to deal with Outlook UI because the VBA code will be run on the main thread (UI).
You may consider setting up an event handler for the Send event which is fired when the user selects the Send action for an item, or when the Send method is called for the item. So, you will be aware when the item is sent out.
Also you may find the MailItem.Close event helpful. It is fired when the inspector associated with an item is being closed. So, you will also be aware when the item is closed.

Related

Outlook Forms: Recipient does not receive form, only message body //How to store form's data in an email?

I am currently trying to create a form in Outlook. I want to be able to send this form to different people so that they can make changes to the form and send those changes back to me as a response.
I have a published form in the meantime. My current problem: The recipient does not see the form until he "undocks" the email from the Outlook app and the changes made there are not transmitted to me; I just get an empty form. How or where can I save this data that the recipients enter? I am grateful for any help!
The form definition includes all the fields and the code that you add to the form. As a general rule, publish the form definition to a forms library instead of sending the form definition with the item. If you cannot publish your form to a forms library, you can select the Send form definition with item check box on the Properties page so that other users can see the form pages when they receive items that are composed by using the form.
Forms that you only intend to use once and not publish are referred to as one-off forms. Because of security concerns with one-off forms, users might not see the form correctly when they open items sent to them with a one-off form. In this case, sending the form definition with the one-off form provides the necessary information required to display the form correctly for the users.
To change how users reply to your form, click the Actions page. The Actions page lists the default Reply forms that are available. You can also add your own custom Reply forms. For example, forms based on a new email message have built-in Reply, Reply to All, Forward, and Reply to Folder forms. When users receive your form, the form contains buttons and menu commands so that users can respond to the form. You can disable some or all of these default forms and set attributes that define how these Reply forms appear.
Read more about that in the Create an Outlook Form section of MSDN.

Outlook Add-In to Save Emails After Sending

I'm looking to create an Add-In for Outlook that allows users to save emails, which are saved against a company in our system. Currently this works by the user opening an email and clicking a button I've created which opens the taskpane containing a form (which asks the user to select a company against which the email will be saved, and optionally provide an email description), whereupon pressing 'save' the email's ID and other relevant information are sent to an API which fetches the email from the rest API and saves the file in our system.
This technically works, though ideally I would like some functionality along the lines of:
User sends an email
my add-in to ask the user if they want to save the email immediately after they sending
If 'yes' is clicked, for the email saving form to appear in either the taskpane or a separate window so it can be saved
This is simply so they don't have to navigate through to their sent messages after sending and save them manually.
Currently I have tried using the ItemSend event for this, however it seems this is more for appending things to the email before it sends rather than after, and I don't seem to be able to use this event to get the email item or its ID. I had thought about potentially using the event to send the email to a custom mailbox and have it save as a result of this using a flow of some kind, however I think this would be rather difficult to implement due to needing user input to save the email in our system.

how to display some message once user starts to compose new email

For Office 365 Outlook Web App, we would like to convert attachments to links once end user uploads them, however we didn't see an attachment notification event at current Outlook Addin.
Another approach we could do is to ask them to use our own buttons for uploading, however we need to guide end user to use our own buttons, could we add some message at top of email body like below once new email message box is displayed?
Somehow based on our research, Outlook Addin only has one event right now which is SendEvent, could someone confirm this? if so, it is rather limited.
To display a custom message use Office.context.mailbox.item.notificationMessages object. For example to add a message to current item the code may looks like ...
Office.context.mailbox.item.notificationMessages.addAsync("information", {
type: "informationalMessage",
message : "My custom message.",
icon : "iconid",
persistent: false
});
Be aware there are a maximum of 5 notifications per message and maximum length of each message text is 150 characters per Office.NotificationMessageDetails interface.
For the secondary question you would need to look at available events in Office.EventType enum. Over here you'll see few events available to Outlook app. One of them you are interested in is AttachmentsChanged which is currently available only in Preview (not released yet, but will be soon).

Verify if an Outlook Email Sent From VB.net was Actually Sent

I'm writing some code in VB.net which will zip some files with a password & email them to the recipient(s) & then send a second email to the recipient(s) with the password for the zip file. The email is sent through Outlook.
The people who would be using this also have some VBA code in the Application_ItemSend event procedure in Outlook which checks if there's attachments being sent to external email addresses & if so it gives the user the details & the option to Cancel sending the email - this is done using the Cancel property/argument (sorry I'm not good with the correct terminology for things in VBA/VB.net) of the Application_ItemSend event procedure.
If the user does cancel the first email then I'd like to stop the second email from going out aswell. I've tried checking (directly after invoking MailItem.Send()) MailItem.Sent.ToString but it bugs out on that line & gives this exception - System.Runtime.InteropServices.COMException: 'The item has been moved or deleted.
My first thought was that once MailItem.Send() was invoked that MailItem became nothing, but I've just put a watch for MailItem Is Nothing & it's returning False. I've tried searching but not been able to find anything.
I can work around by displaying the second email & leave it to the user to send or cancel but if possible I'd rather have the code take care of it.
Instead of sending the second email immediately, wait until Items.ItemAdd event fires on the Sent Items folder. Once the first message raises that event, you can create and send the second email.

How can the Add.Item event works at Outlook opening?

I got an issue with the macro I did in Outlook. To resume, the macro starts every time I receive an email. Then it will run a few others Sub, modify an Excel file and so one. When Outlook is running and I receive a new email, everything works perfectly. The problem occurs when I open Outlook and I receive more than one email at the same time.
I suppose the macro doesn't have enough time to end what it's doing with the first email and already try to start again with the next one.
Is there a way to keep the next emails in suspend in order to run the macro for each email, each one has its turn ? Or maybe you have another solution ?
Thank you.
PS: I can provide the code but it's very long.
The problem occurs when I open Outlook and I receive more than one email at the same time.
The NewMailEx event of the Application class is fired once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem, MeetingItem, or SharingItem. The EntryIDsCollection string contains the Entry ID that corresponds to that item.
The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs. You can use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item.
Make sure VBA macros are enabled and allowed to run when Outlook is started. Check out the Trust Center settings in Outlook.