I am developing an Outlook utility to forward the current message a user has open to a preset address. This could be done with a macro but they are not as easily deployable to a user base as add ins are. Currently I have created the UI for the add-in through Visual Studio ribbon design. I have a button on the ribbon that I wish to use for this process. Currently I am trying to put in button action similar to what is described here: forwarding MailItem Outlook Addinn issue. How can I get the current selection to be forwarded? Thanks in advance.
public partial class Ribbon1
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
private void button1_Click(object sender, RibbonControlEventArgs e)
private void sendMail(Outlook.MailItem mail)
Outlook.Application Application = Globals.ThisAddIn.Application;
Outlook.MailItem newmail = Application.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
newmail = mail.Forward();
First of all, there is no need to create a new Outlook Application instance if you develop an add-in. Instead, you need to use the Application property which is available everywhere. See Global Access to Objects in Office Projects for more information.
In case if you deal with inspector windows you can use the following sequence of calls:
where the ActiveInspector method which returns the topmost Inspector object on the desktop. Use this method to access the Inspector object that the user is most likely to be viewing. For example:
Sub CloseItem() Dim myinspector As Outlook.Inspector Dim myItem As Outlook.MailItem Set myinspector = Application.ActiveInspector Set myItem = myinspector.CurrentItem myItem.Close olSave End Sub
But if you need to get the currently selected item in the Explorer window you can use the following sequence:
where the ActiveExplorer method returns the topmost Explorer object on the desktop. This method is also useful for determining when there is no active explorer, so a new one can be opened.