Add a hidden tag to outgoing mail - vba

I create a mail by VBA when sending invoices and automated newsletters by Outlook (2019).
For reimporting the mails from the Sent folder into the database it would be convenient to read a hidden tag including the customer number which I would like to embed into the mail.

You can add a custom property either using MailItem.UserProperties.Add or by setting your custom property directly using MailItem.PropertyAccessor.SetProperty.
Keep in mind adding a user property can force Outlook to send in the RTF format (the infamous winmail.dat attachment), so the latter solution is preferable. You just need to make sure you get the DASL property name (to be used with MailItem.PropertyAccessor.SetProperty) right. You can use the former approach (MailItem.UserProperties.Add), take a look at the message in the Sent Items folder with OutlookSpy (I am its author - click IMessage button), then use the DASL property name replacing MailItem.UserProperties.Add with MailItem.PropertyAccessor.SetProperty

Related

How to Move Emails Containing Name of a Folder?

We manually create a folder each time we get a new client.
I'd like if an email contains a keyword that matches the name of a folder, then move the email to said folder.
E.g.
If email in inbox contains keyword "Apple" and an "Apple" folder exists, then move email to the "Apple" folder. Else nothing.
Similarly, if email in inbox contains keyword "Google" and a "Google" folder exists, then move the email to the "Google" folder. Else nothing.
Without having to set rules for each new folder that is created.
Yes, it is possible. In VBA you can scan all folders for email with a specific keyword and if a corresponding folder exists in Outlook you could move it to that folder. Let's consider what needs to be done for that. First, you need to scan all folders. The AdvancedSearch method of the Application class allows to perform a search based on a specified DAV Searching and Locating (DASL) search string. The key benefits of using the AdvancedSearch method in Outlook are:
The search is performed in another thread. You don’t need to run another thread manually since the AdvancedSearch method runs it automatically in the background.
Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The Restrict and Find/FindNext methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook).
Full support for DASL queries (custom properties can be used for searching too). To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the IsInstantSearchEnabled property of the Store class).
You can stop the search process at any moment using the Stop method of the Search class.
Read more about that in the Advanced search in Outlook programmatically: C#, VB.NET article.
So, you could run a search for items in the background and then at some point when the search is completed you may be notified.
To find the target folder you could iterate over all folders in Outlook recursively. See Enumerate folders for more information on that.
Finally, to move items you can use the Move method which moves a Microsoft Outlook item to a new folder.

VSTO Outlook Plugin updates Appointment.Body and thus Outlook shows text instead of html part of multipart message

I receive multipart invitations which include calendar and text and html part. When I open those invitation mails in outlook it shows the html part.
Now my code updates the text part with:
myAppointment.Body = myAppointment.Body.Replace(OutlookAddIn4.Resources.Resources.LinkToMeeting + " " + meetingLink, "");
myAppointment.Save();
This text is normally NOT included in the text part. Therefore this call DOES NOT change anything.
Although Outlook now shows the text part instead of the HTML part.
My questions:
1.) How can avoid this?
2.) Is there a chance to determine in my Plugin whether Outlook shows the html part (RTFBody that is, right) or the text part?
Thanks
Hannes
Firstly, before unconditionally resetting the body, check if there is something to replace and only then do anything.
Secondly, you can take a look at the RtfBody property and set it instead. If HTML is used, RTF will have the "\fromhtml" header. Latest versions of Outlook support HTML, but HTMLBody property has not been added to the AppointmentItem object. In theory, you can set the PR_HTML property, but PropertyAccessor.SetProperty won't let you set it. If using Redemption is an option (I am its author), you can either set the RDOAppointment.HTMLBody property or set PR_HTML.

Where does Windows Search look for file properties?

We've implemented a shell property handler for a custom file format, with some standard properties and some custom. When we browse our files in Explorer our properties display nicely, but when our files appear in search results they don't. It seems that Explorer isn't querying our property handler when it displays search results.
My question is this: When Explorer displays an item in search results, where does it look for the item's properties? Does it query the appropriate handler, or does it look somewhere in some kind of search cache?
If it queries the handler then I'll investigate why our handler isn't called. (Maybe it's a registration fault.) If it looks in a search cache then I'll investigate why our files aren't indexed properly.
A secondary question: Is the search results pane restricted to a limited subset of the standard properties? In other words, should we just give up trying to display interesting properties here?
Some observations:
Our file format is just .msg (Outlook email message) renamed to .msga. Our handler knows how to read the .msg file format.
Property values display correctly in Browse mode.
They don't display in Search mode.
Property values are displayed correctly in Explorer's details pane, in either mode.
Some standard properties display OK in the search results, but not others, and no custom properties do. For example, these all display OK but are not served by our handler: System.ItemName, System.ItemUrl, System.Size, System.DateCreated. These are available in our handler but do not display: System.Title, System.Message.FromAddress, System.Message.DateSent.
Explorer is clearly reading our HKCR\<progid> registry key, because the display adapts as we edit ContentViewModeForSearch etc.
Our .msga file type is listed in Control Panel as an indexed file type, and our files are in an indexed directory. And I rebuilt the entire index this morning.
We haven't implemented a filter. Control Panel says that it's using "Office Outlook MSG IFilter" for .msga. (I don't know how it figured this out - maybe I copied a registry setting early in the project, or maybe Windows inferred it because we use Outlook for preview.)
Our handler implements IPropertyStore but its methods seem never to be called. (We've implemented some logging, to a file that the search indexer should have access to, but we can't be sure about that.)
Event Viewer shows gatherer error 3036 during indexing: "The content source <csc://{S-1-5-21-2153095834-1917727522-598830505-500}/> cannot be accessed." I think that SID is my user account, and so I wonder whether it's trying to index something to which it doesn't have privileges.

How to change salutation for multiple recipients in Outlook with c#

I want to code an outlook addin and try to replace a placeholder in an outlook mail with the salutation of the recipient. I get the salutation from AD. I tried this in ItemSend, but the body for different recipients contains always the last replacement.
Then i generated a new mail per recipient. That seems to be the right way, but there is the next problem. The mailitem.htmlbody contains not the same content like shown in the outlook message window. All styles of the original message are lost and for instance my signature is bad formatted.
Has anyone an idea to solve my problem?
First of all, if want to send an individual "salutation" for each recipient you need to send separate emails. For example, you can check out the list of recipients in the ItemSend event and if it contacts more than one entry in the collection you can cancel the send operation setting the Cancel parameter to true and prepare an individual email to each recipient from the collection.
You may consider using the Word object model instead of modifying the raw HTML markup of the body. The WordEditor of the Inspector class returns an instance of the Document class from the Word object model. The Chapter 17: Working with Item Bodies describes all possible ways of working with item bodies.

Dynamically add sender email to Message Header rule in Outlook 2007 via right-click

I would like VBA code for Outlook 2007 to right-click a message in any mail view, choose a rule to add it to, and have the sender email be added to the Message Header contains value for that rule.
Only particular rules use Message Header as a criteria, so only those applicable rules should show when I right-click a message.
With a little effort, you are able to manipulate the context menu with VBA.
Any Outlook version prior to 2007 has no object model for rules.
But since OL2007, each Store object exposes GetRules() method, which returns a Rules collection. Try to iterate that collection and fill a CommandBarPopup object with all the rules you want. The Rule objects can be manipulated, so in theory you should be able to do the job.