M
Mo
Hi,
I wrote a simple add-in to copy the content of a selected email to
database. it works fine if you startup the outlook, select an email
and press the addin button. but any click after that does not do
anything. Any ideas on what I am doing wrong?
Here is my code
using System;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
namespace CreateOutlookTicket
{
public partial class ThisAddIn
{
myTableAdapters.TicketsTableAdapter Tickets = new
CreateOutlookTicket.myTableAdapters.TicketsTableAdapter();
private void ThisAddIn_Startup(object sender, System.EventArgs
e)
{
string COMMANDBAR_NAME = "addin";
Office.CommandBar myBar;
Office.CommandBarButton myButton;
myBar =
this.Application.ActiveExplorer().CommandBars.Add(COMMANDBAR_NAME,
Office.MsoBarPosition.msoBarTop, false, true);
myBar.Visible = true;
string BUTTON_NAME = "Create Ticket";
myButton =
(Office.CommandBarButton)myBar.Controls.Add(Office.MsoControlType.msoControlButton,
Type.Missing, Type.Missing, Type.Missing, true);
myButton.Caption = BUTTON_NAME;
myButton.Style =
Office.MsoButtonStyle.msoButtonIconAndCaption;
myButton.Enabled = true;
myButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(myButton_Click);
}
void myButton_Click(Microsoft.Office.Core.CommandBarButton
Ctrl, ref bool CancelDefault)
{
foreach (object item in
Application.ActiveExplorer().Selection)
{
Outlook.MailItem mi = item as Outlook.MailItem;
if (mi != null)
{
try
{
int
TicketID=Convert.ToInt32(Tickets.GetInsertID(mi.SenderName,
mi.SenderEmailAddress, "myadin", mi.SentOn, "General",
mi.Subject, mi.Body, null, "Open", null,
null));
Outlook.MAPIFolder outBoxfolder =
this.Application.GetNamespace("MAPI").GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderOutbox);
Outlook.MailItem mailitem =
(Outlook.MailItem)outBoxfolder.Items.Add(Outlook.OlItemType.olMailItem);
mailitem.Subject = "New Ticket
#"+TicketID.ToString()+" Has Been Created";
mailitem.Body = "A new ticket has been
created. Please visit ticketing system for details";
mailitem.To = "xxx.xx.xxx";
mailitem.Send();
System.Windows.Forms.MessageBox.Show("Ticket
"+TicketID.ToString()+" Created", "Ticket Manager");
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message, "Ticket Manager");
}
}
else
{
System.Windows.Forms.MessageBox.Show("Select an
Email", "Ticket Manager");
}
}
}
private void ThisAddIn_Shutdown(object sender,
System.EventArgs e)
{
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new
System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new
System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
I wrote a simple add-in to copy the content of a selected email to
database. it works fine if you startup the outlook, select an email
and press the addin button. but any click after that does not do
anything. Any ideas on what I am doing wrong?
Here is my code
using System;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
namespace CreateOutlookTicket
{
public partial class ThisAddIn
{
myTableAdapters.TicketsTableAdapter Tickets = new
CreateOutlookTicket.myTableAdapters.TicketsTableAdapter();
private void ThisAddIn_Startup(object sender, System.EventArgs
e)
{
string COMMANDBAR_NAME = "addin";
Office.CommandBar myBar;
Office.CommandBarButton myButton;
myBar =
this.Application.ActiveExplorer().CommandBars.Add(COMMANDBAR_NAME,
Office.MsoBarPosition.msoBarTop, false, true);
myBar.Visible = true;
string BUTTON_NAME = "Create Ticket";
myButton =
(Office.CommandBarButton)myBar.Controls.Add(Office.MsoControlType.msoControlButton,
Type.Missing, Type.Missing, Type.Missing, true);
myButton.Caption = BUTTON_NAME;
myButton.Style =
Office.MsoButtonStyle.msoButtonIconAndCaption;
myButton.Enabled = true;
myButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(myButton_Click);
}
void myButton_Click(Microsoft.Office.Core.CommandBarButton
Ctrl, ref bool CancelDefault)
{
foreach (object item in
Application.ActiveExplorer().Selection)
{
Outlook.MailItem mi = item as Outlook.MailItem;
if (mi != null)
{
try
{
int
TicketID=Convert.ToInt32(Tickets.GetInsertID(mi.SenderName,
mi.SenderEmailAddress, "myadin", mi.SentOn, "General",
mi.Subject, mi.Body, null, "Open", null,
null));
Outlook.MAPIFolder outBoxfolder =
this.Application.GetNamespace("MAPI").GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderOutbox);
Outlook.MailItem mailitem =
(Outlook.MailItem)outBoxfolder.Items.Add(Outlook.OlItemType.olMailItem);
mailitem.Subject = "New Ticket
#"+TicketID.ToString()+" Has Been Created";
mailitem.Body = "A new ticket has been
created. Please visit ticketing system for details";
mailitem.To = "xxx.xx.xxx";
mailitem.Send();
System.Windows.Forms.MessageBox.Show("Ticket
"+TicketID.ToString()+" Created", "Ticket Manager");
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message, "Ticket Manager");
}
}
else
{
System.Windows.Forms.MessageBox.Show("Select an
Email", "Ticket Manager");
}
}
}
private void ThisAddIn_Shutdown(object sender,
System.EventArgs e)
{
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new
System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new
System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}