A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Add-ins for Outlook
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Outlook Addin functions only one time after Outlook is started



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old February 24th 08, 07:14 AM posted to microsoft.public.outlook.program_addins
Mo
external usenet poster
 
Posts: 3
Default Outlook Addin functions only one time after Outlook is started

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.TicketsTableAd apter();
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_Clic kEventHandler(myButton_Click);



}

void myButton_Click(Microsoft.Office.Core.CommandBarBut ton
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.Se nderName,
mi.SenderEmailAddress, "myadin", mi.SentOn, "General",
mi.Subject, mi.Body, null, "Open", null,
null));

Outlook.MAPIFolder outBoxfolder =
this.Application.GetNamespace("MAPI").GetDefaultFo lder(Microsoft.Office.Interop.Outlook.OlDefaultFol ders.olFolderOutbox);
Outlook.MailItem mailitem =
(Outlook.MailItem)outBoxfolder.Items.Add(Outlook.O lItemType.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
}
}
Ads
  #2  
Old February 24th 08, 10:27 PM posted to microsoft.public.outlook.program_addins
Mark J. McGinty
external usenet poster
 
Posts: 56
Default Outlook Addin functions only one time after Outlook is started


"Mo" wrote in message
...
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?


C# I take it? I don't see any exception handling, any unhandled exceptions
within an AddIn will effectively shut it down. Try wrapping your code in a
try/catch block. (I seem to recall C# support for try/catch/finally.)


-Mark




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.TicketsTableAd apter();
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_Clic kEventHandler(myButton_Click);



}

void myButton_Click(Microsoft.Office.Core.CommandBarBut ton
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.Se nderName,
mi.SenderEmailAddress, "myadin", mi.SentOn, "General",
mi.Subject, mi.Body, null, "Open", null,
null));

Outlook.MAPIFolder outBoxfolder =
this.Application.GetNamespace("MAPI").GetDefaultFo lder(Microsoft.Office.Interop.Outlook.OlDefaultFol ders.olFolderOutbox);
Outlook.MailItem mailitem =
(Outlook.MailItem)outBoxfolder.Items.Add(Outlook.O lItemType.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
}
}



  #3  
Old February 25th 08, 12:53 AM posted to microsoft.public.outlook.program_addins
Mo
external usenet poster
 
Posts: 3
Default Outlook Addin functions only one time after Outlook is started

Mark,

I have try/catch in the code and there are no exceptions when I debug
it. The first time you click the add-in button everything works fine
and you can debug right through it. After that even in debug mode the
button click event is not triggered.

Mo
  #4  
Old February 25th 08, 03:15 AM posted to microsoft.public.outlook.program_addins
Mark J. McGinty
external usenet poster
 
Posts: 56
Default Outlook Addin functions only one time after Outlook is started


"Mo" wrote in message
...
Mark,

I have try/catch in the code and there are no exceptions when I debug
it. The first time you click the add-in button everything works fine
and you can debug right through it. After that even in debug mode the
button click event is not triggered.


Weird. Maybe put a call th OutputDebugString() (Win32 API) in the class
destructor, or maybe a do-nothing line of code that you can set a breakpoint
on -- some way to know when the class unloads. Or maybe add a dummy button,
to give yourself a way into a breakpoint/a way to examine the object in the
debugger after it breaks.

Sadly, you couldn't fill a thimble with what I know specifically about C#,
but logically (or so it seems to me) either your class is being unloaded, or
whatever hooks it into the button is being lost/cancelled/overwritten.

Does myButton need to be declared at class scope, rather than function
scope? (I'm just grasping at straws, now.)

Sorry I don't have more for you.


-Mark


Mo



  #5  
Old February 25th 08, 01:55 PM posted to microsoft.public.outlook.program_addins
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Outlook Addin functions only one time after Outlook is started

Your button object is going out of scope and is being garbage collected,
therefore it won't fire again on a click.

That's a very common problem in managed code if you don't declare your
objects at the correct scope level.

Declare your button object at the class level and not within Startup(). Then
in Startup() you instantiate the object, it will remain alive until you
release it explicitly.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


"Mo" wrote in message
...
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.TicketsTableAd apter();
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_Clic kEventHandler(myButton_Click);



}

void myButton_Click(Microsoft.Office.Core.CommandBarBut ton
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.Se nderName,
mi.SenderEmailAddress, "myadin", mi.SentOn, "General",
mi.Subject, mi.Body, null, "Open", null,
null));

Outlook.MAPIFolder outBoxfolder =
this.Application.GetNamespace("MAPI").GetDefaultFo lder(Microsoft.Office.Interop.Outlook.OlDefaultFol ders.olFolderOutbox);
Outlook.MailItem mailitem =
(Outlook.MailItem)outBoxfolder.Items.Add(Outlook.O lItemType.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
}
}


  #6  
Old February 25th 08, 02:44 PM posted to microsoft.public.outlook.program_addins
Mo
external usenet poster
 
Posts: 3
Default Outlook Addin functions only one time after Outlook is started

That did the trick. Thank you Ken.
 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Outlook regularly crashes the first time when started. Michael Moser Outlook - General Queries 2 January 22nd 08 07:03 PM
AddIn loading takse too much time after PC Restart j Add-ins for Outlook 3 December 11th 07 02:08 PM
Outlook 2007, Calendar functions Nigel Barton Outlook - Calandaring 1 January 21st 07 06:32 PM
List of available Outlook functions Dorci Outlook - Using Forms 1 June 26th 06 02:41 PM
Outlook VBA text functions Vaughan Outlook and VBA 5 February 14th 06 08:51 AM


All times are GMT +1. The time now is 09:03 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.