Outlook Add-In and Windows Form collaboration - c#

I'm developing an outlook add in that collaborates with SharePoint. I've added a new item to the Outlook add in: a Windows Form. When a button is being clicked in the Windows Form, I want to perform an action with Outlook, like this:
Outlook.NameSpace outlookNameSpace = this.Application.GetNamespace("MAPI");
This isn't working, because I'm in the Windows Form instead of in the Outlook Add In. I'm not sure how to let those though collaborate.

The Outlook Application object passed to the add-in during initialization is already intrinsically declared in the ThisAddIn class within your VSTO project. You can access it from any other item in the project using Globals.ThisAddIn.Application. You must use this object and not a new Outloook Application object because it won't be fully trusted as it wasn't passed to the add-in initialization by Outlook.

Add a new parameter to your WinForm constructor, to receive a reference to the Outlook Application object:
using Outlook = Microsoft.Office.Interop.Outlook;
.
.
.
public partial class MyWinForm : Form
{
private Outlook.Application m_OutlookApp;
public MyWinForm(Outlook.Application OutlookApp)
{
m_OutlookApp = OutlookApp;
InitializeComponent();
}
Your add-in code to launch the form then becomes something like:
MyWinForm myWinForm = new MyWinForm(Application);
myWinForm.Show();
You can then use the Outlook Application within the WinForm:
private void button1_Click(object sender, EventArgs e)
{
Outlook.NameSpace outlookNameSpace = m_OutlookApp.GetNamespace("MAPI");
.
.
.
}

I think you can use something like this below,
var output = Globals.YourAddin.Application.GetNamespace("MAPI");
Hope this helps.

Related

c# VSTO Outlook email body from opened window

I've a problem with my VSTO application for Outlook. I want to process the email body from a selected e-mail.
For selected e-mails out of the "default" list this code works fine:
Object selItem = Globals.ThisAddIn.Application.ActiveExplorer().Selection[1];
Outlook.MailItem mailItem = (Outlook.MailItem)selItem;
return mailItem.Body;
But if a user opens an email from the list with a double click, the email is displayed in a new window. If the addin is executed in this window (over the ribbon), the email from the list is still used (which is now in the background).
Is there a way to find out if the plugin was started in a separate window and then get the email body from it?
Regards,
Florian
Coincidentally, I just dealt with something similar to this. My situation isn't identical, but since I could easily piece together what it seems like you're looking for see below. I haven't tested this, and obviously you'll have to handle passing the correct reference to your Outlook Application, but since i had this immediately available I figured it would pass it along with the hope that you'll find it helpful.
private static void ribbonButton_Click(object sender, RibbonControlEventArgs e)
{
Outlook.Application application = new Outlook.Application();
Outlook.Inspector inspector = application.ActiveInspector();
if (application.ActiveExplorer().Selection[1] is Outlook.MailItem explorerMailItem)
{
// Write code to handle message if sourced from explorer (i.e., Reading Pane)
}
else if (inspector.CurrentItem is Outlook.MailItem inspectorMailItem)
{
// Write code to hanlde message if sourced from inspector
// (i.e., openened (double-clicked) message
}
}
When you double click on email item you open an inspector window and you can access it by using Application.ActiveInspector() method. The Inspector object has CurrentItem property which represents the opened item.
Also, you should avoid using multiple dots in expressions and properly release COM objects.

can i change incoming mail label in outlook to another label using c#?

I am creating outlook add ons. I want when mail come inbox then change
the label, i use Outlook.ItemsEvents_ItemAddEventHandler(callBack)
method but in work only when outlook web app open. I want
Outlook.ItemsEvents_ItemAddEventHandler(callBack) call everytime
when mail comes in Inbox.
If there is ant Outlook Rest Api available for that problem.
private void Phishing_Load(object sender, RibbonUIEventArgs e)
{
outlookApp = new Outlook.Application();
inboxFolder = outlookApp.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
deleterFolder = outlookApp.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderJunk);
mailItems = inboxFolder.Items;//.Restrict("[Unread]=true");
mailItems.Sort("[ReceivedTime]", true);
inboxFolder.Items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(CallFunction);
}
private void CallFunction(object data)
{
MessageBox.Show("call function");
}
Outlook add-in can work only when Outlook application is running. It seems you can't get what you wish using an Outlook add-in - find an alternative way. If you run Outlook against on-site Exchange server try to create a so called transport agent for Exchange.

DLLImportAttribute C# - Outlook

I'm trying to develop a snippet in C # code that enables the "voting option" function of Outlook.
This code will be used by a platform called Blue Prism.
The "vote" function of Outlook is in the Microsoft.Office.Interop.Outlook namespace, so I need to import it using C#, but I dont have enough knowledge to develop this.
I tried to do something like this but it is giving an error.
Here is the code:
public class program {
[DllImport(#"C:\Program Files\Blue Prism Limited\Blue Prism Automate\Microsoft.Office.Interop.Outlook.dll", EntryPoint = "VotingOptions")]
public static extern string Outlook(uint type);
static void Main()
{
// Create the Outlook application.
Outlook.Application oApp = new Outlook.Application();
oApp.VotingOption = "Yes; No";
}
}
So, can someone help me?
The VotingOptions property belongs to the MailItem class , not Outlook Application. Voting options on messages are used to give message recipients a list of choices and to track their responses. To create voting options programmatically, set a string that is a semicolon-delimited list of values for the VotingOptions property of a MailItem object. The values for the VotingOptions property will appear under the Vote command in the Respond group in the ribbon of the received message.
private void OrderPizza()
{
Outlook.MailItem mail = (Outlook.MailItem)Application.CreateItem(
Outlook.OlItemType.olMailItem);
mail.VotingOptions = “Cheese; Mushroom; Sausage; Combo; Veg Combo;”
mail.Subject = “Pizza Order”;
mail.Display(false);
}
Also you may find the C# app automates Outlook (CSAutomateOutlook) sample project helpful, it shows how to automate Outlook in C#.
Com objects aren't accessed through DLLImport. They're accessed using references. From the sample Eugene linked:
Create a Console application and reference the Outlook Primary Interop Assembly (PIA). To reference the Outlook PIA, right-click the project file and click the "Add Reference..." button. In the Add Reference dialog, navigate to the .NET tab, find Microsoft.Office.Interop.Outlook 12.0.0.0 and click OK.
Now you'll have access to the Microsoft.Office.Interop.Outlook object.
If you are using Blue Prism then rather than having to specify DLL references you may also chose to go the GetObject or CreateObject way, you will be able to interact with Outlook just like Blue Prism does with Excel. The drawback of this approach is that you have to use VB.NET (unless I am mistaken) and that you will not be able to use text representation of enum values (so for OlItemType you will not be able to use olMailItem but only its numeric value, which is 0).
Please note that Blue Prism has released a new version recently (6.3) and with it a new VBO for interaction with Outlook. It's nothing revolutionary, but it may provide some insight.

How to programmatically submit a password to a stand alone program in C#

I am making a C# application (using Microsoft Visual Studio 2015, using WinForms) that needs to open a password protected .docx file in C#. I know you can do:
Process.Start("WINWORD.EXE", "filename.docx");
To start word (I'm using Microsoft Word 2016) with that file loaded in. But I also want to submit the password to Word so it opens without me doing anything. I know it's not this simple, but this is what imagining right now:
public void submitPassword(string password)
{
Process.Start("WINWORD.EXE", "filename.docx" //something like this: System.Password.Sumbit("WINWORD.EXE", password));
}
There is a COM type library called Microsoft Word Object Library. In Visual Studio, you can add a reference to it from the solution explorer. Right click References, click Add Reference, go to the COM tab, and search for Word.
With the package Microsoft.Office.Interop.Word, you can create an Application instance and tell it to open a document using a password.
using Microsoft.Office.Interop.Word;
...
public void SubmitPassword(string password)
{
Application app = new Application();
app.Documents.Open(FileName: #"filepath", PasswordDocument: password);
}
The answer provided to me by Andrew Piliser got me close, but not there. This is what works:
using Word = Microsoft.Office.Interop.Word;
...
public void submitPassword()
{
var wordApp = new Word.Application();
wordApp.Visible = true;
wordApp.Documents.Open(FileName: #"filepath", PasswordDocument: "filepassword");
}

Change Outlook Inbox-icons envelope icons

following this link - change outlook MailItem icon
I managed to change my inbox icons.
Here's what I did step by step.
1) Created a custom message class for new mail that arrives from the Internet
The class is IPM.Note.Internet
Outlook.NameSpace outlookNameSpace;
Outlook.MAPIFolder inbox;
Outlook.Items items;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
outlookNameSpace = this.Application.GetNamespace("MAPI");
inbox = outlookNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
items = inbox.Items;
items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(items_ItemAdd);
}
void items_ItemAdd(object Item)
{
Outlook.MailItem mailitem = (Outlook.MailItem)Item;
String EmailHeader = mailitem.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E");
if (Item != null && EmailHeader.Contains("Look for a string in the headers here that we set for incomming mail") == true)
{
if (mailitem.MessageClass == "IPM.Note")
{
mailitem.MessageClass = "IPM.Note.Internet";
mailitem.Save();
}
}
}
2) Created a replacement Outlook Form Region matching the MessageClass. In this case I used IPM.Note.Internet
3) Assign the Icons in the Properties Pane of the Form Region Designer.
4) Debugged project and the next message that arrived from the internet was stamped with my custom icons after the message class was updated.
My issue now is that I can't preview or open the messages where I changed the message class. Similar to this post that's unanswered - Change Inbox-icons in Outlook at runtime
I think the issue is that my replacement Outlook Form Region is blank so the message is not able to be previewed.
If this is true than here's my question. What is the best way to export the standard IPM.Note message class template into visual Studio. I thing I need to overwrite my IPM.Note.Internet Outlook Form Region design.
There is an option when creating an Outlook Form Region-
To import an ".OFS" file. I was attempting to figure out how to export the file from the Outlook 2010 Client (Developer Tools) but I can't find a way to save the templates to that specific format. I can save to OFT (office template) but not .OFS
Thanks in advance for any help!
Rather then adding a form region and changing the message class I just ended up adding the PR_ICON_INDEX property and setting it's value. As outlined here in option #2 by Dmitry Link
There are many icons to choose from here. I couldn't locate a list with the integer values so I just entered random numbers for the PR_ICON_INDEX property in Outlook Spy changing the value till located the icon I wanted. There are many icons to choose from. Many from the 600-700 and 1000 and up range.
Here's the line I used to set the PR_ICON_INDEX property on the message-
mailitem.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x10800003", 4); // change the 4 to something like 600,601...etc to experiment

Categories

Resources