Kulvinder

Hi,

I want to know that if i create an Outlook Addin, how do i make sure that it is loaded eveytime i open Outlook Lets assume that i have PIAs and eveything required installed.

The problem occurs when the Outlook object is opened by an Outlook Automation e.g. Active Sync. In that case, no toolbar is loaded. Is there any way i can make sure that my toolbar is always there

Regards

Kulvinder Singh




Re: Visual Studio Tools for Office Best practices for VSTO SE Addins

Kulvinder

Is there any way which can ensure that Outlook Addins are always loaded even if Outlook is started with or Without UI assuming that all the prereqisites are installed correctly.




Re: Visual Studio Tools for Office Best practices for VSTO SE Addins

Ji Zhou – MSFT

Hi Kulvinder,

I have read about your issue carefully. As you described, the Outlook Add In created by VSTO SE was not loaded when you open Outlook through an Outlook Automation. I have also reproduced the scenario and in fact the AddIn was loaded, just the toolbar is not there. Below is my explaination.

Firstly, Outlook Automation does not perform like Word and Excel. There is not a Visible property of Outlook.Application Object, so you have to use such codes to let Outlook be Visible(I believe you are using the similar ones):

Code Snippet

private void Form1_Load(object sender, EventArgs e)

{

Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.Application();

Outlook.NameSpace ns = myApp.GetNamespace("MAPI");

Outlook.Folder folder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;

Outlook.Explorer explorer = myApp.Explorers.Add(folder, Microsoft.Office.Interop.Outlook.OlFolderDisplayMode.olFolderDisplayNormal);

explorer.Activate();

}

After creating the object myApp, the Starup event in your AddIn is raised, and till now there is no explorer in your Office Automation.So if we add the toolBar in the ThisAddIn_Startup method, the toolbar which depends on at least one explorer will not appear. The solution is that add the toolBar in functions hanld the active event of every explorer. To do this ,you should register every explorer's active event when it is created. Following are my codes in VSTO SE AddIn:

Code Snippet

Outlook.Explorers myExplorers = null;

Office.CommandBar myCommandBar = null;

Office.CommandBarButton myCommandBarButton = null;

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

myExplorers = this.Application.Explorers;

myExplorers.NewExplorer += new Microsoft.Office.Interop.Outlook.ExplorersEvents_NewExplorerEventHandler(Explorers_NewExplorer);

if (this.Application.Explorers.Count == 1)

{

AddMenuButton();

}

}

void myCommandBarButton_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)

{

MessageBox.Show("Test Button Clicked!");

}

void Explorers_NewExplorer(Microsoft.Office.Interop.Outlook.Explorer Explorer)

{

((Outlook.ExplorerEvents_10_Event)Explorer).Activate += new Microsoft.Office.Interop.Outlook.ExplorerEvents_10_ActivateEventHandler(ThisAddIn_Activate);

AddMenuButton();

}

void ThisAddIn_Activate()

{

AddMenuButton();

}

private void AddMenuButton()

{

try

{

myCommandBar = this.Application.ActiveExplorer().CommandBars["Menu Bar"];

myCommandBar.Reset();

Office.CommandBarControl myCommandBarControl = myCommandBar.Controls.Add(Office.MsoControlType.msoControlPopup, 1, missing, missing, true);

myCommandBarControl.Visible = true;

myCommandBarControl.Caption = "Test";

myCommandBarButton = ((Office.CommandBarPopup)myCommandBarControl).CommandBar.Controls.Add(Office.MsoControlType.msoControlButton, 1, "", 1, true) as Office.CommandBarButton;

myCommandBarButton.Caption = "test";

myCommandBarButton.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(myCommandBarButton_Click);

}

catch (Exception ex)

{

}

}

Hope this is what you want :-)

Thanks

Ji






Re: Visual Studio Tools for Office Best practices for VSTO SE Addins

Kulvinder

Thanks for your reply but the above code didnt work. If i use you code, it laucnhes Outlook itself with no addin.

Did you actually run the above code There is no Outlook.Folder property BTW.

Can you help furthur






Re: Visual Studio Tools for Office Best practices for VSTO SE Addins

Ji Zhou – MSFT

Hi,

I did use all the codes above on my environment. Why there is no Outlook.Folder property It seems that you are using Outlook2003, right OK, I will write some codes in Outlook 2003 and post later.

Thanks

Ji






Re: Visual Studio Tools for Office Best practices for VSTO SE Addins

Ji Zhou – MSFT

Hi Kulvinder,

Yes, the codes I posted above only works on Outlook 2007. The Outlook.Folder object is also only available in Outlook 2007. I have tested on Outlook2003 just now, and it is very complicated about the Outlook's starup behavior, you can refer whitechapel's blog to get detailed information.

I also find a trick which maybe solve your issue more or less: you can startup an instance of Outlook in FormLoad method or ButtonClick method,all denpends on you. And then get the handle to the instance to do automation. I have write some codes for your information:

Code Snippet

Outlook.Application myApp = null;

private void Form1_Load(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(@"C:\Program Files\Microsoft Office\OFFICE11\OUTLOOK.EXE");
}

private void button1_Click(object sender, EventArgs e)
{
myApp = (Outlook.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application.11");
MessageBox.Show("Connected!");
}

private void button2_Click(object sender, EventArgs e)
{
Outlook.MAPIFolder inbox = myApp.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Items myItems = inbox.Items;
Outlook.MailItem mailItem = myItems.Add(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
mailItem.Subject = "Test";
mailItem.Save();
mailItem.Move(inbox);
}

In my codes, there are two buttons on a form. In the Form_Load event handle method, I run an new instance of Outlook with the statement System.Diagnostics.Process.Start(), then I should click button1, so we will get the myApp pointed the instance we created before(by using System.Runtime.InteropServices.Marshal.GetActiveObject()Wink, so I can do automation in the button2_Click method(In my example, I Add a new mail into Inbox).

Thanks

Ji






Re: Visual Studio Tools for Office Best practices for VSTO SE Addins

Sue Mosher - Outlook MVP

The problem with this approach, though, is that if the goal is to build an add-in that doesn't trigger Outlook security prompts, the only "trusted" instance of Outlook.Application is the one passed by the add-in architecture. Getting it with GetActiveObject won't be good enough.



Re: Visual Studio Tools for Office Best practices for VSTO SE Addins

Kulvinder

Thanks a lot for your reply. I tried your code in Outlook 2007 and it works great. Thanks again.

Can you provide me a simple resolution for Outlook 2003 as well

Regards

Kulvinder Singh






Re: Visual Studio Tools for Office Best practices for VSTO SE Addins

Kulvinder

The problem with Outlook 2007 is that my Toolbar is displayed once and if i close Outlook (i havent exited my Windows App) and i reopen Outlook, The toolbar gets disappered again.




Re: Visual Studio Tools for Office Best practices for VSTO SE Addins

Kulvinder

Hi Zhou,

Thanks a lot for your reply. I tried your code in Outlook 2007 and it works great. Thanks again.

Can you provide me a simple resolution for Outlook 2003 as well

Regards

Kulvinder Singh






Re: Visual Studio Tools for Office Best practices for VSTO SE Addins

Ji Zhou – MSFT

Hi Kulvinder,

Sadly to say, I also tried a lot in Outlook2003, but no luck for your issue. The best approach I can find is as my previous post. Create an Outlook process, and then use GetActiveObject to get the reference of Outlook instance.

Thanks

Ji






Re: Visual Studio Tools for Office Best practices for VSTO SE Addins

Kulvinder

Hi Zhou,

I just want to know how i can load the toolbar everytime in Outlook 2003 automation Do you mean that all the Addins doesnt get loaded by AddinLoader when Outlook 2003 is run by Automation

Thanks

Kulvinder Singh






Re: Visual Studio Tools for Office Best practices for VSTO SE Addins

Kulvinder

Any updates




Re: Visual Studio Tools for Office Best practices for VSTO SE Addins

Ji Zhou – MSFT

Hi Kulvinder,

Look at this thread: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1413814&SiteID=1

Thanks