Outlook - Custom Add-In Button for "Save and Send" - c#

I am fairly new to C# as well as the Outlook Library. I've spent the past two weeks searching around reading documentation and examples yet I can't seem to achieve this simple (or so it seems) task.
The Intention:
The idea is to have a custom button that only appears in the ribbon of the New Mail window and when clicked, will prompt the user to choose an Outlook Folder. After the user chooses a folder and clicks OK, it will send the email and save a copy in the specified folder as well as the sent folder.
I am aware that currently, it is possible to specify which folder you want to save the outgoing message in. However, this will only save a copy in the specified folder, and not an additional copy in the Sent Items folder.
The Current Situation:
What I have currently will make a copy of the current email, move it to the specified folder, and send the original. However, the copied email will not show up as an email that's been sent. It is still editable which is not exactly what I am aiming for.
private void button1_Click(object sender, RibbonControlEventArgs e) {
MessageBox.Show("Save and Send button was clicked.");
Outlook.Application application = new Outlook.Application();
Outlook.Explorer explorer = application.ActiveExplorer();
Outlook.Inspector inspector = application.ActiveInspector();
Outlook._MailItem mailItem = inspector.CurrentItem;
Outlook.NameSpace nameSpace = application.GetNamespace("MAPI");
MessageBox.Show("Select a location to save the email");
Outlook.MAPIFolder folder = (Outlook.Folder)nameSpace.PickFolder();
Outlook._MailItem newMailCopy = mailItem.Copy();
newMailCopy.Move(folder);
mailItem.Send();
MessageBox.Show(newMailCopy + " was copied to " + folder);
}
I am creating a Ribbon (Visual Designer) in Visual Studio 2013 for an Outlook 2013 Add-In. What would be the best way to do this in C#?
Any help would be greatly appreciated! Regards,

Related

Outlook VSTO code crashes when click on the button for image

I am working on this Outlook VSTO Add-on, to add an image in the body of the email, but no luck at all for two weeks!!! It works with a normal path like c:\folder
but it doesn't work if I want to use the Resources folder inside of the app.
When I run it and click on the button, it crashes and goes to the:
document.Application.Selection.InlineShapes.AddPicture(ImagePath);
There is a Ribbon with a button. When user clicks, it should add the email signature in the body.
PLEASE HELP!!!!!!!!!!!!!!!!
private void button_Click(object sender, RibbonControlEventArgs e)
{
Outlook.Inspector inspector = Globals.ThisAddIn.Application.ActiveInspector();
Word.Document document = inspector.WordEditor;
string ImagePath = #"\Resources\Picture.jpg";
if (document.Application.Selection == null)
{
MessageBox.Show("Please select the email body");
}
else
{
document.Application.Selection.InlineShapes.AddPicture(ImagePath);
}
}
Word Object Model knows nothing about resources inside your assembly. It does not know and does not care about any external assemblies. It only understands files - you will need to extract the resource into a temp file and specify that file's fully qualified file name.

How Outlook shows the PaperClip icon for any attachment other than embedded images?

For my use case, I need to find out if a mail item in outlook has any embedded image as an attachment. I am working on Outlook 2013+ version. It is my understanding that for all other embedded attachments, they can only be added in Rich Text format and Outlook marks them as attachment (Marked as PaperClip icon). But if the mail contains only a embedded image in HTML format then the PaperClip icon does not appear. Please feel free me to point me if I am wrong with this observation.
My question however is, how is outlook itself deciding and showing the paperclip icon? I used Outlook Spy but the PR_HasAttach is set to true for these mails. I have also read other solutions where people have pointed out that you can try to find tag in the HTML Mail body which mostly works. But has any body ever wondered about the PaperClip icon?
I am having this doubt because when I apply the DASL filter urn:schemas:httpmail:hasattachment = true, such mails are filtered out by the query. Same thing happens if you use has attachments filter in outlook itself. But if I select the same mail item, and try to explore this property either by code or by Outlook Spy, it comes as true. That is why I am thinking that it might have to do with the icon Or atleast If I can find the property which sets the icon.
Edit 1:
When I use the following code it does not work for embedded or hidden attachments. For others it works.
IEnumerator eNum = listOfSearchedOutlookItems.GetEnumerator();
while (eNum.MoveNext())
{
Outlook.MailItem mailItem;
try
{
mailItem = (Outlook.MailItem)eNum.Current;
bool b = mailItem.PropertyAccessor.GetProperty(#"http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B");
}
catch
{
continue;
}
}
However strangely the following works for all the cases (As many as I have tested). So in a way this problem is solved thanks to Dmitry's answer. But still wondering why is the previous one not working.
Outlook.Table table = MyFolder.GetTable(SomeFilterIfNeeded);
table.Columns.RemoveAll();
table.Columns.Add(#"http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B");
while (!table.EndOfTable)
{
Outlook.Row nextRow = table.GetNextRow();
bool b = (bool)nextRow[#"http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B"];
}
To hide the paperclip icon, Outlook sets SmartNoAttach named MAPI property (DASL name http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B).

HtmlAnchor control not opening in new window

I have the following code to dynamically create an HtmlAnchor and assign its HRef property. When I right click the link and do "save target as" it is downloading the pdf, but when clicking the link is not opening in a new tab.
HtmlAnchor htmlanc = new HtmlAnchor();
htmlanc.HRef = "file:\\arts\Shared\Let";
htmlanc.Title = "Letter Link";
htmlanc.InnerText = "file:\\arts\Shared\Let";
htmlanc.Target = "_blank";
pnlLet.Controls.Add(htmlanc);
when i click on the link it is generation an error which in the below image.
And the path is in a network folder which is shared. Is the error generating because my application cannot access the path. I checked with the networking guys but they say that the application have full access to the network shared folder, but i doubt it.
I wanted this to be a comment but it seems you are struggling to understand our help so I wanted to expand it here.
I have copied your code sample and put on Page_Load of a blank WebForms solution.
protected void Page_Load(object sender, EventArgs e)
{
var htmlanc = new HtmlAnchor();
htmlanc.HRef = "http://stackoverflow.com/questions/29281667/htmlanchor-control-not-opening-in-new-window";
htmlanc.Title = "Open Question";
htmlanc.InnerText = "Open Question";
htmlanc.Target = "_blank";
Controls.Add(htmlanc);
}
When running it you can right-click on the link:
Then you can see all of the attributes and see the target="_blank":
When I click this link it correctly shows in a new tab. If you follow the above steps can you check your link has the correct target set and expand on what happens when you click it? Depending on your browser it may be set to download based on mime-type rather than opening a new tab etc.

Form Regions for Outlook and VSTO remove WebViewPane

I have just purchased: http://www.add-in-express.com/outlook-regions/
The reason why I have purchased that product is because I need to show a custom form on the main view of outlook when a specific folder is selected. I have managed to do so by doing the following:
Create a new Project int visual studio of type (Outlook 2010 Add-in)
Once that project is created I add:
Because I want that form to show on the main folder view of mail (replace all other views) I select this option:
I click next and follow all the defaults in order to create the form.
Once that form is created I add the buttons and images I need. In this example I will just add a button.
when I then run outlook and select my inbox folder this is what shows up:
(In other words every time I select a folder of type olMailItem that form shows up)
So now I solve my goal to display my custom form on the "main view of outlook"
Now my question is how can I show that form only on specific folders. For example I do not want to show that form when the folder "Inbox" is selected but I do want to show it when the folder "Outbox" is selected for example
Let's try to hide the form and show the default view when the button is clicked.
In order to solve that problem I have looked at: http://www.add-in-express.com/forum/read.php?FID=5&TID=4540
I have done the same steps but the form shows up again! In other words the code behind of the button looks like:
private void button1_Click(object sender, EventArgs e)
{
// get current folder in this case its inbox the one that is selected
MAPIFolder currentFolder = Globals.ThisAddIn.Application.ActiveExplorer().CurrentFolder;
Globals.ThisAddIn.ADXOlForm1Item.FolderName = string.Empty;
// clear web properties DO NOT SHOW WEB VIEW
currentFolder.WebViewURL = string.Empty;
currentFolder.WebViewOn = false;
// RESET FOLDER BY SELECTING A DIFFERENT ONE THEN THE SAME ONE
NameSpace nameSpace = Globals.ThisAddIn.Application.GetNamespace("MAPI");
MAPIFolder outboxFolder = nameSpace.GetDefaultFolder(OlDefaultFolders.olFolderOutbox);
Globals.ThisAddIn.Application.ActiveExplorer().CurrentFolder = outboxFolder; // CHANGE FOLDER TO A DIFFERNT ONE
System.Windows.Forms.Application.DoEvents();
Globals.ThisAddIn.Application.ActiveExplorer().CurrentFolder = currentFolder; // SET INBOX AGAIN
}
when I run that code the default view shows up for 1 second then it get's replaced with the form!
-------------------------------------------Edit-------------------------------------------
When I add the form region Addin Express adds a ADXOlFormsManager and a ADXOlFormsCollectionItem for the form that I created. Based on your answer I have done:
#region ADXOlForm1
// TODO: Use the ADXOlForm1Item properties to configure the region's location, appearance and behavior.
// See the "The UI Mechanics" chapter of the Add-in Express Developer's Guide for more information.
ADXOlForm1Item = new ADXOlFormsCollectionItem();
ADXOlForm1Item.FolderName = "MyCustomFolder"; // <---- ADDED THIS LINE HOPING TO SHOW THIS FORM ONLY WHEN THAT FOLDER IS SELECTED
ADXOlForm1Item.ExplorerLayout = ADXOlExplorerLayout.WebViewPane;
ADXOlForm1Item.ExplorerItemTypes = ADXOlExplorerItemTypes.olMailItem;
ADXOlForm1Item.UseOfficeThemeForBackground = true;
ADXOlForm1Item.FormClassName = typeof(ADXOlForm1).FullName;
this.FormsManager.Items.Add(ADXOlForm1Item);
#endregion
I was hoping for that form to only show up in MyCustomFolder but it does shows up when selecting any folder of type ADXOlExplorerItemTypes.olMailItem. Perhaps I am doing something wrong....
In other words I was hoping for only the MAPIFolder MyCustomFolder folder had the properties
WebViewOn=true;
WebViewURL = "...AppData\Local\Temp\AddinExpress\ADXOlFormGeneral.html"
but as I traverse all the folders in outlook I can see that all of them have those properties even after specifying ADXOlForm1Item.FolderName = "MyCustomFolder";
Thank you for choosing Add-in Express Regions.
I understand the "main view of outlook" as the folder that Outlook shows when it is started. By default, that folder is the top-level folder of the message store. Note that this can be changed, see File | Options | Advanced | Start Outlook in this folder. I use this setting so that my Outlook shows me the Inbox at start-up.
The below is a citation from the section Context-Sensitivity of Your Outlook Form, see see the PDF file in the folder {Add-in Express}\Docs\ on your development PC:
ADXOlFormsCollectionItem provides a number of properties that allow specifying the context settings for your form. Say, you can specify item types for which your form will be shown. Note that in case of explorer, the item types that you specify are compared with the default item type of the current folder. In addition, you can specify the names of the folders for which your form will be shown in the FolderName and FolderNames properties; these properties also work for Inspector windows – in this case, the parent folder of the Outlook item is checked. An example of the folder path is "\Personal Folders\Inbox".
A special value in FolderName is an asterisk ('*'), which means "all folders". You can also specify message class(es) for which your form will be shown. Note that all context-sensitivity properties of an ADXOlFormsCollectionItem are processed using the OR Boolean operation. That is, specifying e.g. folder names extends, but not limits, the list of contexts for which your form will be shown.
That is, if you need to show the form for a given folder, specify the path to that folder in the FolderName/*FolderNames* property.
Regards from Belarus (GMT+3),
Andrei Smolin, Add-in Express Team Leader

How to set the Selected item in the tree view on the Left side of CHM file

I have a CHM helpfile for my WPF Application. My CHM file contains "htm" files for each page of my application. I want to open the help file for the corresponding page when the user presses F1 on that page. Right now I am able to locate the page and open that page by using the following code:
Help.ShowHelp(this, helpfile, keywordText);
where keywordText contains the URL of my htm file for the selected page.
But the problem is, that the panel on the left side (contents tab in a tree view) is not expanded to the page that opened in the right window. The panel on the left side always remains the same.
How Can I expand the tree view on the left side to the selected page?
Have a look at the code and the small GUI (snap attached). The topics are refreshed after the users button click (e.g. Help Topic1).
Please note the help file created with Microsoft HTMLHelp Workshop may need a auto sync parameter.
private void btnHelpTopic1_Click(object sender, EventArgs e)
{
// sHTMLHelpFileName_ShowWithNavigationPane = "CHM-example_ShowWithNavigationPane.chm"
// This is a HelpViewer Window with navigation pane for show case only
// created with Microsoft HTMLHelp Workshop
helpProvider1.HelpNamespace = Application.StartupPath + #"\" + sHTMLHelpFileName_ShowWithNavigationPane;
Help.ShowHelp(this, helpProvider1.HelpNamespace, #"/Garden/tree.htm");
}
private void btnHelpTopic2_Click(object sender, EventArgs e)
{
helpProvider1.HelpNamespace = Application.StartupPath + #"\" + sHTMLHelpFileName_ShowWithNavigationPane;
Help.ShowHelp(this, helpProvider1.HelpNamespace, #"/Garden/flowers.htm");
}
For download I provide a C# VS2008 Project including the code above and the help files with different help viewer windows (different CHM files for show case only).
Thanks.
The Problem is in the URL.
Before that I gave a URL like this
"Help.CHM::/html/MyHelp.htm"
And when I removed "Help.CHM::/" from the URL and Everything Worked Well..

Categories