msoutlook.org Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Outlook Addin functions only one time after Outlook is start

 
Post new topic   Reply to topic    msoutlook.org Forum Index -> Outlook Programs Add-Ins
Author Message
Mo



Joined: 24 Feb 2008
Posts: 3

PostPosted: Sun Feb 24, 2008 4:14 am    Post subject: Outlook Addin functions only one time after Outlook is start Reply with quote

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

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InternalStartup()
{
this.Startup += new
System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new
System.EventHandler(ThisAddIn_Shutdown);
}

#endregion
}
}

Archived from group: microsoft>public>outlook>program_addins
Back to top
View user's profile Send private message
Mo



Joined: 24 Feb 2008
Posts: 3

PostPosted: Sun Feb 24, 2008 9:53 pm    Post subject: Re: Outlook Addin functions only one time after Outlook is s Reply with quote

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
Back to top
View user's profile Send private message
Mark J. McGinty



Joined: 13 Aug 2007
Posts: 8

PostPosted: Mon Feb 25, 2008 12:15 am    Post subject: Re: Outlook Addin functions only one time after Outlook is s Reply with quote

"Mo" wrote in message @c33g2000hsd.googlegroups.com...
> 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
Back to top
View user's profile Send private message
Ken Slovak - [MVP - Outlo



Joined: 12 Aug 2007
Posts: 405

PostPosted: Mon Feb 25, 2008 1:55 pm    Post subject: Re: Outlook Addin functions only one time after Outlook is s Reply with quote

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 @q78g2000hsh.googlegroups.com...
> 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
>
> ///
> /// Required method for Designer support - do not modify
> /// the contents of this method with the code editor.
> ///
> private void InternalStartup()
> {
> this.Startup += new
> System.EventHandler(ThisAddIn_Startup);
> this.Shutdown += new
> System.EventHandler(ThisAddIn_Shutdown);
> }
>
> #endregion
> }
> }
Back to top
View user's profile Send private message
Mo



Joined: 24 Feb 2008
Posts: 3

PostPosted: Mon Feb 25, 2008 11:44 am    Post subject: Re: Outlook Addin functions only one time after Outlook is s Reply with quote

That did the trick. Thank you Ken.

Back to top
View user's profile Send private message
Display posts from previous:   
Related Topics:
C++ Outlook Addin (New Folder Code) Hi, I'm developing a Outlook Addin, simply I want to create a new folder, at same level of Inbox folder, I'd tried some code, but the folder doesn't show. Neither if I try creating this folder inside the inbox folder, I'll like to get some code sample, i

Addin will not load in Vista/Outlook 2007 Hello. I have an addin that works fine on XP/Outlook 2007, but once we install it on Vista, it won't load in Outlook 2007. Looking in the Tools --> Trust Center --> Addins, etc., I see the generic message "Not loaded. A runtime error occurred during th

Compile Addin for OL 2000 thru 2007 I have an addin for Outlook that works in 2000 thru 2003. It also works in 2007 with a few changes. However, the 2007 version puts the buttons in the Add-Ins tab. I have made changes to customize the Ribbon with my own tab & everything works if I compile

Outlook und Outlook Connector? Fehler! Hallo zusammen Ich habe Office 2003 (ohne Outlook) und Office 2007 (mit Outlook) installiert. Nun wollte ich heute den Outlook Connector installieren. Bei der Installation ergaben sich eigentlich keine Probleme. Ich kann jetzt nur kein Konto hinzufügen. W

Outlook Connector I've been using the Outlook Connector for Domino for over a year with pretty good success. This past weekend it looks like the Domino changed something and all of my Domino mail now looks different. instead of showing the sender's short nam
Post new topic   Reply to topic    msoutlook.org Forum Index -> Outlook Programs Add-Ins All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group