Use link from email outlook to fire VBA code - vba

is there any chance to make the following scenario working
user gets email in Outlook containing link/text/object
clicking the object runs simple VBA code (i.e. replace string in txt) file on users computer
Thank you.

No, it is not possible. This is a potential path for malware.
Instead, you can handle the NewMailEx event of the Application class in Outlook VBA macros where can detect such mails (with a specific text or links) and run your business logic accordingly.
The NewMailEx event fires 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 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.

Related

Saving Outlook Attachments Automatically Based On Attachment File Name Only

I need to save an Outlook file attachment with a static file name to a specific network location automatically when that email arrives. This file will be saved in a network location for upload using a monthly SSI server package. I hope to do this automatically without any interaction but not opposed to manually running a macro. I am unsure of references that are needed in VBA to get this to execute. I am also unfamiliar with the "ThisOutlookSession" configuration that I've seen in similar threads
I have attempted to use the existing script that I've seen here with no luck. ( I can get them to run without error but do not get any results ) I want to search all incoming email and only have it take action if the email has an attachment and that attachment has a specific unchanging file name. I have the developer tab enabled in Outlook and can access VBA through it. Looking for a solid simple solution. Constants are the file name and extension as well as the network folder. Variables would be the sender and date of delivery. Office 365 running Windows 10 in a professional environment. Any help or direction would be greatly appreciated.
You can handle incoming emails in Outlook in the NewMailEx event handler. The event fires 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. Use the Entry ID string to call the NameSpace.GetItemFromID method and process the item. The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs.
Also, as a possible workaround, you may consider handling the ItemAdd event on the Inbox folder. This event is fired when one or more items are added to the specified collection. This event does not run when a large number of items are added to the folder at once. For example:
Public WithEvents myOlItems As Outlook.Items
Public Sub Application_Startup()
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts).Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
MsgBox Item.Attachments.Count
End Sub

VSTO Outlook ItemAdd not being fired when incoming e-mail is moved by rule to subfolder

I have an Outlook VSTO add-in. I want to respond to incoming emails. This works quite well with the declaration
Public WithEvents items As Outlook.Items
And the definition for the items that are observed. (I'm afraid that's why only "Inbox" is watched):
inbox = objOutlook.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
items = inbox.Items
and the eventhandler
Private Sub Items_ItemAdd(ByVal item As Object) Handles items.ItemAdd
Even if Outlook is closed, an event is triggered for each new email when Outlook is started.
I've now noticed that some users of the add-in have created a rule that moves incoming emails to a subfolder of "Inbox". In this case, the Items_ItemAdd event is not fired when a new email arrives.
How can I also capture these new emails that are moved via a rule?
The item is moved on the server side by the server side rule, and even if Outlook was running, it could've been moved before Outlook downloaded the original item to the OST store. So if ItemAdd event fires, it only fires on the folder where the item was moved to, not in the Inbox folder.
Besides watching the Inbox folder, you'd need to watch the other folders that the rules might point to - retrieve the rules using Store.GetRules(), loop through all rules, check if Rule.MoveToFolder action is enabled, and if yes, retrieve the target folder from MoveOrCopyRuleAction.Folder property.
Use the NewMailEx event of the Application class which 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. Use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item.
Also you may track the items in the folders by combining multiple mechanisms like this event handler plus getting new items at startup to not skip any of them that was added silently. The Find/FindNext or Restrict methods of the Items class can help you with such tasks. Read more about that in the series of articles:
Outlook NewMail event unleashed: the challenge (NewMail, NewMailEx, ItemAdd)
Outlook NewMail event: solution options
Outlook NewMail event and Extended MAPI: C# example
Outlook NewMail unleashed: writing a working solution (C# example)

Is there a way to reply to a sender with his own message?

In outlook, I want all the people sending me emails with the object as "testintro" let's say, to be replied with their OWN email. I want them to receive exactly what they sent me. Is it possible to create that rule?
Yes, it is possible. But not with a rule. Instead, you can develop a VBA macro.
You can create a VBA macro where you could handle the NewMailEx event of the Application class. The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs. Use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item. This event fires 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.
So, you may get an instance of the arrived mail item and call the Reply method to reply instantly. The method creates a reply, pre-addressed to the original sender, from the original message. You may find the following articles helpful:
How To: Create and send an Outlook message programmatically
How To: Fill TO,CC and BCC fields in Outlook programmatically
How To: Change an Outlook e-mail message before sending using C# or VB.NET

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.

Create Msg Read View Outlook

I'm writing an Outlook Addin Ribbon in VB.net and getting on with it rather well, however I've come to a point where I'm stuck.
When sending an item, I also need to save the file in the reading format which it would appear in the 'Sent Items'.
If have tried mailItem.SaveAs("path", olSaveAsType) but it only saves as a draft item. Whereby you can edit the text.
I tried to loop through the sent items in the 'sent items' folder after sending, however Outlook hangs whilst my code is executing, thus holding up the actual sending of the email.
Is there a way this can be done?
Any help, hints or tips would be appreciated!
Message submission is an asynchronous process. Set up the Items.ItemAdd event handler on the Sent Items folder (Namespace.GetDefaultFolder(olFolderSentMail)) and process the sent message in the ItemAdd event handler.