I'm creating a Microsoft Office 2013 Add-in that does some behaviour similar to the Address book in an Active Mail item:
I'm working with Microsoft.Office.Interop.Outlook.MailItem and using the Recipients property, and the Recipients.ResolveAll() method to load and remove addresses to it, everything seems to work fine until I click OK on my loaded from so I send control back to the active mail Item. At this point Outlook goes crazy and doesn't place the addresses properly in the To or the CC, or some of them go missing.
I'm thinking that one solution to the problem could be to trigger the Check Names when the user is ready to click OK and send all the addresses from my form to the Active mail item.
How can I trigger this action / button?
I couldn't find anything that could do that in the MailItem class. I would like something similar to this (but to use from within my poped-out windows form in Outloook):
UPDATE:
This are some bits of my code to add more context:
I use the method AddRecipientToActiveItem for each item that I have. It will verify if it already exists (it was already added), if not it will resolve it and if it is correct add it.
private void AddRecipientToActiveItem(string recipientAddress, Recipients recipientList, OlMailRecipientType recipientType)
{
Recipient recipientObject = default(Recipient);
if (!string.IsNullOrWhiteSpace(recipientAddress) && !EmailRecipientAlreadyExists(recipientAddress, recipientType))
{
recipientObject = recipientList.Add(recipientAddress);
recipientObject.Resolve();
if (recipientObject.Resolved)
{
recipientObject.Type = (int)recipientType;
recipientList.ResolveAll();
}
else
{
recipientObject.Delete();
}
}
}
for this, I have to iterate through each element in the list of recipients and compare by address + type (From,To,CC,BCC) pair:
private bool EmailRecipientAlreadyExists(string fullEmailAddress, OlMailRecipientType recipientType)
{
foreach (Microsoft.Office.Interop.Outlook.Recipient recipientObject in ActiveMailItem.Recipients)
{
if (GetRecipientEmailAddress(recipientObject) != null)
{
if (GetRecipientEmailAddress(recipientObject).Equals(fullEmailAddress) && recipientObject.Type == (int)recipientType)
return true;
}
}
return false;
}
But the user can also add addresses to their email item manually, and some of them exist in the exchange server, but others are simple smtp address, so when comparing I have to handle both scenarios:
private string GetRecipientEmailAddress(Microsoft.Office.Interop.Outlook.Recipient recipientObject)
{
Outlook.ExchangeUser objExchangeUser = null;
if (recipientObject.Address != null && recipientObject.AddressEntry != null)
objExchangeUser = recipientObject.AddressEntry.GetExchangeUser();
if (recipientObject.Address == null && objExchangeUser == null)
return recipientObject.Name;
if (objExchangeUser == null)
return recipientObject.Address;
return objExchangeUser.PrimarySmtpAddress;
}
}
It seems like this what I've done is not enough to keep consistent states of the email addresses when I read them from the mail item and put them in a text box of my form, then add more addresses and send them back to the mail item and close the form, and repeat the process.
The Namespace class provides the GetSelectNamesDialog method which obtains a SelectNamesDialog object for the current session. It displays the Select Names dialog box for the user to select entries from one or more address lists, and returns the selected entries in the collection object specified by the property SelectNamesDialog.Recipients.
Sub SelectRecipients()
Dim oMsg As MailItem
Set oMsg = Application.CreateItem(olMailItem)
Dim oDialog As SelectNamesDialog
Set oDialog = Application.Session.GetSelectNamesDialog
With oDialog
.InitialAddressList = _
Application.Session.GetGlobalAddressList
.Recipients = oMsg.Recipients
If .Display Then
'Recipients Resolved
oMsg.Subject = "Hello"
oMsg.Send
End If
End With
End Sub
The dialog box displayed by SelectNamesDialog.Display is similar to the Select Names dialog box in the Outlook user interface. It observes the size and position settings of the built-in Select Names dialog box. However, its default state does not show Message Recipients above the To, Cc, and Bcc edit boxes. For more information on using the SelectNamesDialog object to display the Select Names dialog box, see Display Names from the Address Book.
Related
I want to build an Outlook AddIn in C# that has a button in the calendar ribbon that the user clicks to create a new meeting with one of their employees. We want the user (the manager) to be able to select the employee from a filtered list of only their own employees and not have to search through the entire directory. What is the best way to do this?
addendum:
I did some searching and I came across a potential method for the filter.
I know that the "SelectNamesDialog" function will get me an Address Book dialog box:
Outlook.SelectNamesDialog snd = Application.Session.GetSelectNamesDialog();
I want to combine that with a piece of code I found. I modified it to return the names of all the manager's direct reports (the employees under the manager).
I think I'm on the right track, but I'm uncertain of what to do next. How do I now allow the user to select one of these names through the GetSelectNamesDialog? It is OK if your answer is in psuedocode.
// source: "How to: Get Information About Direct Reports of the Current User's Manager"
// https://msdn.microsoft.com/en-us/library/ff184617.aspx
private List<string> GetManagerDirectReports()
{
List<string> AddressNames = new List<string>();
Outlook.AddressEntry currentUser = Globals.ThisAddIn.Application.Session.CurrentUser.AddressEntry;
if (currentUser.Type == "EX")
{
Outlook.ExchangeUser manager = currentUser.GetExchangeUser().GetExchangeUserManager();
if (manager != null)
{
Outlook.AddressEntries addrEntries = manager.GetDirectReports();
if (addrEntries != null)
{
foreach (Outlook.AddressEntry addrEntry in addrEntries)
{
//System.Windows.Forms.MessageBox.Show(addrEntry.Name);
AddressNames.Add(addrEntry.Name);
}
}
}
}
return AddressNames;
}
Address book won't let you restrict the list to a subset of some users, so you'd need to come up with you own window that prompts the user for the selection from a pre-filtered list.
I now seem to be able to post an answer to my own question.
I added a dropdown in the Form Region and added this code to populate the dropdown with names of the manager's direct reports:
// Get Outlook list of employees who report to manager, using Exchange data.
List<string> mgrAddressNames = GetManagerDirectReports();
if (mgrAddressNames.Count >= 1)
{
try
{
// System.Windows.Forms.BindingSource bindingSource1;
// Create a Binding Source to the ComboBox to make values in ComboBox match the results of the list of direct reports.
System.Windows.Forms.BindingSource bindingSource1 = new System.Windows.Forms.BindingSource();
bindingSource1.DataSource = mgrAddressNames;
EmployeeInvited.DisplayMember = "Value";
EmployeeInvited.ValueMember = "Key";
EmployeeInvited.DataSource = bindingSource1.DataSource;
bindingSource1.Dispose();
(etc)
I have a word document, that uses several textboxes, which contain mail merge fields.
I want to mail merge programmatically into a word document, by using a system that I have made, that searches for any mail merge fields in a document and then inserts the appropriate value into the mail merge fields and then saves the document as a new file.
By using
Document.StoryRanges
I am able to do the above process for 1 text box.
But if I make several text boxes, it only seems to insert the value into one of the textboxes consistently. The texbox that receives the value does not change. If I try to delete that textbox, the mail merging process does not work and then I have to fiddle with some of the other textboxes to get it to work. For example I have to bring the textbox backwards and then forwards for the system to mail merge into only of the textboxes.
I have tried creating a foreach loop to go into each textbox, without much success. So I did some debugging and found that the system is only reading the entire document and one of the textboxes as StoryRanges.
Here's a solution I used recently. You can get to the mail merge fields through Document.Shapes. I'm not sure exactly what's going on here, but it worked for me.
public static List<Field> getMailMergeFields(Document document)
{
List<Field> mailMergeFields = new List<Field>();
foreach (Shape shape in document.Shapes)
{
if (shape.TextFrame.HasText != 0)
{
foreach (Field field in shape.TextFrame.ContainingRange.Fields)
{
if (isMailMergeField(field)) mailMergeFields.Add(field);
}
}
}
foreach (Field field in document.Fields)
{
if (isMailMergeField(field)) mailMergeFields.Add(field);
}
return mailMergeFields;
}
public static bool isMailMergeField(Field field)
{
string fullField = field.Code.Text.Trim();
if (!fullField.StartsWith("MERGEFIELD")) return false;
if (!fullField.EndsWith(#"\* MERGEFORMAT")) return false;
return true;
}
Hope this helps.
I work on Outlook addin that is connected to an Owncloud (Caldav, Cardav).
I use eventhandlers to detect when a user delete, create or update a contact item.
In this case this will notify the server and do an update on his side.
storage.Aitems[storage.Aitems.Count - 1].ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(contact_item_add);
storage.Aitems[storage.Aitems.Count - 1].ItemChange += new Outlook.ItemsEvents_ItemChangeEventHandler(contact_item_change);
((MAPIFolderEvents_12_Event)storage.Afolders[storage.Afolders.Count - 1]).BeforeItemMove += new MAPIFolderEvents_12_BeforeItemMoveEventHandler(contact_item_before_move);
When i find new contacts items from the server i add them right the addressbook folder. Outlook detect a new item or a update and then fire the eventshandlers. This is good in case there is only one call (I can say this from the server dont notify again the server or this is from the user notify the server) but i can't because the events are fired multiple times.
static void contact_item_change(object Item) {
Microsoft.Office.Interop.Outlook.ContactItem contact = (Microsoft.Office.Interop.Outlook.ContactItem)Item;
System.Diagnostics.Debug.WriteLine("[Modification contact]" + contact.FullName);
// Need to know if item was created by code (server) or user
Main.SyncContact(contact);
}
Is it possible to know if an item is created trough the GUI or in my code ?
I can't set a variable to know if it was created by user or by my code because of the multiple calls of events.
BTW i already succeed to fire add and delete only one time :
static void contact_item_add(object Item) {
Microsoft.Office.Interop.Outlook.ContactItem contact = (Microsoft.Office.Interop.Outlook.ContactItem)Item;
if (Contact.GetCreatedByUserProperty(contact, "USER")) {
if (storage.PreviousAddedContactId != contact.EntryID)
{
System.Diagnostics.Debug.WriteLine("[Ajout contact]" + contact.FullName);
Main.SyncContact(contact);
storage.PreviousAddedContactId = contact.EntryID;
}
}
}
static void contact_item_before_move(object item, MAPIFolder destinationFolder, ref bool cancel) {
if ((destinationFolder == null) || (IsDeletedItemsFolder(destinationFolder))) {
ContactItem contact = (item as ContactItem);
if (storage.PreviousDeletedContactId != contact.EntryID)
{
System.Diagnostics.Debug.WriteLine("[Suppression du contact]" + contact.FullName);
Main.DeleteContact(contact);
storage.PreviousDeletedContactId = contact.EntryID;
}
}
}
Thank you !
You can read MailItem.LastModificationTime immediately after calling MailItem.Save. When ItemChange event fires, you can compare the new modification time with what you cached and check if it is greater than some delta (1 second?).
In case of Exchange store, you can also retrieve PR_CHANGE_KEY (DASL name http://schemas.microsoft.com/mapi/proptag/0x65E20102) property - it will change after each modification.
Is it possible to know if an item is created trough the GUI or in my code ?
No, there is no property or method which can help in identifying the way which was used to create an Outlook item. But you may add a user property which may indicate that the item was created by your code programmatically. User created item will not have this property set.
I followed the answer of Dmitry Streblechenko
In fact ContactItem have a LastModificationTime property. I don't know if i'm wrong but i guess outlook never change this Datetime when he makes changes. So it's easy to check if user have touch the contact. That allow me to notify the server and it works very well ! Thank you !
static void contact_item_change(object Item) {
Microsoft.Office.Interop.Outlook.ContactItem contact = (Microsoft.Office.Interop.Outlook.ContactItem)Item;
// Need to know if item was created by code (server) or user
var seconds = (DateTime.Now - contact.LastModificationTime).TotalSeconds;
if (seconds < 2) {
System.Diagnostics.Debug.WriteLine("[Modification contact]" + contact.FullName);
Main.SyncContact(contact);
}
}
You only need to focus on properties you want on that Item. When ItemChange event
fired, you can compare properties of that Item with your server Item, if they are difference => do Main.SyncContact(contact);
I wrote an addin a while back for Outlook that adds/removes an optional tagline below the signature in an outlook message. This add-in works with no issues.
I'm writing a second add-in that needs to potentially add information below that (whether or not the optional signature is there) and am again referencing the _MailAutoSig bookmark from the Word editor. The issue I'm running into is that this bookmark no longer seems to appear, nor does the bookmark from my other add-in.
One difference in the two pieces of code below is that the first one has the MailItem being converted from an object passed by ItemSend, whereas the second is processed BEFORE the ItemSend event.
Here is the code from what I am currently writing:
Word.Document toMsg = msg.GetInspector.WordEditor as Word.Document;
foreach (Word.Bookmark b in toMsg.Bookmarks)
Debug.Print(b.ToString());
Word.Range r_toMsg;
try
{
string oBookmark = "_MailAutoSig"; // Outlook internal bookmark for location of the e-mail signature`
object oBookmarkObj = oBookmark;
if (toMsg.Bookmarks.Exists(oBookmark) == true)
Debug.Print("sigbookmark");
r_toMsg = toMsg.Bookmarks.get_Item(ref oBookmarkObj).Range;
}
catch
{
string oOffsiteBookmark = "OffsiteBookmark";
object oOffsiteBookmarkObj = oOffsiteBookmark;
if (toMsg.Bookmarks.Exists(oOffsiteBookmark) == true) // if the custom bookmark exists, remove it
Debug.Print("offsite bookmark");
}
finally
{
r_toMsg = toMsg.Range(missing,missing);
}
and here is code from my working add-in:
void InsertOffsiteSig(Outlook.MailItem oMsg)
{
object oBookmarkName = "_MailAutoSig"; // Outlook internal bookmark for location of the e-mail signature
string oOffsiteBookmark = "OffsiteBookmark"; // bookmark to be created in Outlook for the Offsite tagline
object oOffsiteBookmarkObj = oOffsiteBookmark;
Word.Document SigDoc = oMsg.GetInspector.WordEditor as Word.Document; // edit the message using Word
string bf = oMsg.BodyFormat.ToString(); // determine the message body format (text, html, rtf)
// Go to the e-mail signature bookmark, then set the cursor to the very end of the range.
// This is where we will insert/remove our tagline, and the start of the new range of text
Word.Range r = SigDoc.Bookmarks.get_Item(ref oBookmarkName).Range;
object collapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
r.Collapse(ref collapseEnd);
string[] taglines = GetRssItem(); // Get tagline information from the RSS XML file and place into an array
// Loop through the array and insert each line of text separated by a newline
foreach (string taglineText in taglines)
r.InsertAfter(taglineText + "\n");
r.InsertAfter("\n");
// Add formatting to HTML/RTF messages
if (bf != "olFormatPlain" && bf != "olFormatUnspecified")
{
SigDoc.Hyperlinks.Add(r, taglines[2]); // turn the link text into a hyperlink
r.Font.Underline = 0; // remove the hyperlink underline
r.Font.Color = Word.WdColor.wdColorGray45; // change all text to Gray45
r.Font.Size = 8; // Change the font size to 8 point
r.Font.Name = "Arial"; // Change the font to Arial
}
r.NoProofing = -1; // turn off spelling/grammar check for this range of text
object range1 = r;
SigDoc.Bookmarks.Add(oOffsiteBookmark, ref range1); // define this range as our custom bookmark
if (bf != "olFormatPlain" && bf != "olFormatUnspecified")
{
// Make the first line BOLD only for HTML/RTF messages
Word.Find f = r.Find;
f.Text = taglines[0];
f.MatchWholeWord = true;
f.Execute();
while (f.Found)
{
r.Font.Bold = -1;
f.Execute();
}
}
else
{
// otherwise turn the plain text hyperlink into an active hyperlink
// this is done here instead of above due to the extra formatting needed for HTML/RTF text
Word.Find f = r.Find;
f.Text = taglines[2];
f.MatchWholeWord = true;
f.Execute();
SigDoc.Hyperlinks.Add(r, taglines[2]);
}
r.NoProofing = -1; // disable spelling/grammar checking on the updated range
r.Collapse(collapseEnd);
}
The problem is that Microsoft converts the "Office HTML" (I'm not sure the proper term) to normal HTML before it fires the ItemSend() event, which causes the _MailAutoSig bookmark to disappear.
The only way to get the _MailAutoSig bookmark back is to first CANCEL the ItemSend() event, then fire off a timer to run a function which will in turn access the email object and manipulate it how you want, add a user property to mark the email has been processed, and then send the email again.
For example:
Dim modItem As Object 'need to hold the item somewhere so the timer can access it
Sub object_ItemSend(ByVal Item As Object, Cancel As Boolean)
If Item.UserProperties.Item("isModded") Is Nothing Then
'User has composed a mail and hit "Send", we need to make our modifications to the signature though
modItem = item
Cancel = True 'cancel the Send so we can make the modifications
mytimer.Enabled = True 'fire off a timer to make the modifications
Exit Sub
Else
Item.UserProperties.Item("isModded").Delete 'this flag will keep the email from ping-ponging between ItemSend and the timer
End If
End Sub
'10 millisecond timer? I think the cancel is almost instant, but experiment
Sub mytimer_Timer()
mytimer.Enabled = False
If Not modItem Is Nothing Then
modItem.HtmlBody = ...... the signature bookmark will be intact again, so make your modifications ......
modItem.UserProperties.Add("isModded", olText) 'no ping-pong
modItem.Send 'send it again
modItem = Nothing
End If
End Sub
I had to do something similar for a project where some Outlook fields were not set until I was in the ItemSend() event, so I was forcing the email to send, get my information, then cancel the send. It worked great.
Now, this was written off the top of my head, so I'm sure the code above will not be perfect, but it should give you the idea of what needs to be done.
I'm developing an application that needs to perform some processing on the user's Outlook contacts. I'm currently accessing the list of Outlook contacts by iterating over the result of MAPIFolder.Items.Restrict(somefilter), which can be found in Microsoft.Office.Interop.Outlook.
In my application, my user needs to choose several contacts to apply a certain operation on. I would like to add a feature that will allow the user to drag a contact from Outlook and drop it on a certain ListBox in the UI (I work in WPF but this is probably is more generic issue).
I'm very new to C# and WPF - how can I:
Receive a dropped item on a ListBox
Verify it's a ContactItem (or something that wraps ContactItem)
Cast the dropped item to a ContactItem so I can process it
Thanks
I tried this with a TextBox (no difference with a ListBox in practice).
Summary :
Searching in all outlook contacts for the one recieved dragged as text.
The search here is based on the person's FullName.
condition(s):
When you drag a contact, it must show the FullName when selected in outlook. The only catch is when two persons have the same full names!! If it's the case you can try to find a unique identifier for a person by combining ContactItem properties and searching them in the dragged text.
private void textBox1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetData("Text") != null)
{
ApplicationClass app;
MAPIFolder mapif;
string contactStr;
contactStr = e.Data.GetData("Text").ToString();
app = new ApplicationClass();
mapif = app.GetNamespace("MAPI").GetDefaultFolder(OlDefaultFolders.olFolderContacts);
foreach (ContactItem tci in mapif.Items)
{
if (contactStr.Contains(tci.FullName))
{
draggedContact = tci; //draggedContact is a global variable for example or a property...
break;
}
}
mapif = null;
app.Quit;
app = null;
GC.Collect();
}
}
of course this code is to be organized-optimized, it's only to explain the method used.
You can try using the Explorer.Selection property combined with GetData("Text") [to ensure it's coming from Outlook, or you can use GetData("Object Descriptor") in the DragOver Event, decode the memory stream, search for "outlook", if not found cancel the drag operation] And why not drag multiple contacts!
private void textBox1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetData("Text") != null)
{
ApplicationClass app;
Explorer exp;
List<ContactItem> draggedContacts;
string contactStr;
contactStr = e.Data.GetData("Text").ToString();
draggedContacts = new List<ContactItem>();
app = new ApplicationClass();
exp = app.ActiveExplorer();
if (exp.CurrentFolder.DefaultItemType == OlItemType.olContactItem)
{
if (exp.Selection != null)
{
foreach (ContactItem ci in exp.Selection)
{
if (contactStr.Contains(ci.FullName))
{
draggedContacts.Add(ci);
}
}
}
}
app = null;
GC.Collect();
}
}
An Outlook contact, when dropped, supports the following formats:
(0): "RenPrivateSourceFolder"
(1): "RenPrivateMessages"
(2): "FileGroupDescriptor"
(3): "FileGroupDescriptorW"
(4): "FileContents"
(5): "Object Descriptor"
(6): "System.String"
(7): "UnicodeText"
(8): "Text"
The most interesting looking one on that list (for me) is Object Descriptor, which then led me to someone with a similar sounding problem:
http://bytes.com/topic/visual-basic-net/answers/527320-drag-drop-outlook-vb-net-richtextbox
Where it looks like in that case, they detect that it's an Outlook drop, and then use the Outlook object model to detect what's currently selected, with the implication that that must be the current drop source.
What you could probably do is accept the drag and drop event in your .wpf app and then get the selected item(s) from outlook and pull them into your application.
Update
Add the Outlook PIA references to you app.
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.Explorer activeExplorer = app.ActiveExplorer();
Microsoft.Office.Interop.Outlook.Selection currentSelection = activeExplorer.Selection;
Then you can iterate over the currentSelection collection to see what the user has dragged over.