Cyrex4046

hi

i have problem i start new project outlookAddIn

Private WithEvents _Explorers As Outlook.Explorers

Private WithEvents _Explorer As Outlook.Explorer

Private _contactFolder As Outlook.Explorer

Private _mapiContact As Outlook.MAPIFolder

Private _helpMenuIdex As Object

Private _menuBar As Office.CommandBar

Private WithEvents _Email As Outlook.MailItem

Private Const COMMAND_BAR As String = "EDC"

Private Const BUTTON_NAME As String = "Send_EDC"

Private WithEvents myButton As Office.CommandBarButton

Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

_menuBar = applicationObject.ActiveExplorer().CommandBars.Add(COMMAND_BAR, Office.MsoBarPosition.msoBarTop, False, True)

_menuBar.Visible = True

End Sub

when i run the add-in and the outlook start i get this msg

An unhandled exception occurred in your application. Contact your administrator or the author of this document for further assistance.

please some ine can help me




Re: Visual Studio Tools for Office help with the outlook add-in in vs2005

Ken Slovak

Always use try...catch blocks when you do things with COM objects, like creating CommandBar UI or working with Outlook objects. Then at minimum you will know what line throws the exception and what exception was thrown.

Assuming that you do have an Explorer open, not always true, you should wait for Explorer.Activate before creating any UI for that Explorer. If this is the first Explorer, when Outlook is opened with a UI, then Activate may not fire. In that case you use SelectionChange, which does fire after the Explorer is activated.

For Inspector UI you must also wait for the initial Activate event, the weak object reference in NewInspector doesn't provide a reliable handle to the CommandBars collection of the Inspector. For Explorers you handle both the first Activate and SelectionChange, that way you always get an event when the Explorer CommandBars collection is available.

Before assigning any Explorer objects always for for ActiveExplorer IsNot Nothing or check the Explorers.Count property.

Ken Slovak





Re: Visual Studio Tools for Office help with the outlook add-in in vs2005

Cyrex4046

first TNX for the help

look i am very begginer in .net i my boss drop on me big task

anyway can you tell me what is worng in this code:

this code send me msgbox "Object reference not set to an instance of an object"

and after i debug it is seem to be

Imports System.Collections.Generic

Imports Outlook = Microsoft.Office.Interop.Outlook

Imports Office = Microsoft.Office.Core

Imports Microsoft.Office.Tools.Outlook

public class ThisAddIn

Dim applicationObject As Outlook.Application

Private menuBar As Office.CommandBar

Private newMenuBar As Office.CommandBarPopup

Private buttonOne As Office.CommandBarButton

Private menuTag As String = "A unique tag"

Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e _

As System.EventArgs) Handles Me.Startup

RemoveMenubar() -------------------------------------app error return "Object reference not set to an instance of an object"

AddMenuBar()--------------------------------------------app error return "Object reference not set to an instance of an object"

End Sub

Private Sub AddMenuBar()

Try

menuBar = applicationObject.ActiveExplorer().CommandBars.ActiveMenuBar

newMenuBar = menuBar.Controls.Add( _

Office.MsoControlType.msoControlPopup, _

Temporary:=False)

If newMenuBar IsNot Nothing Then

newMenuBar.Caption = "New Menu"

newMenuBar.Tag = menuTag

buttonOne = newMenuBar.Controls.Add( _

Office.MsoControlType.msoControlButton, _

Before:=1, Temporary:=True)

With buttonOne

.Style = Office.MsoButtonStyle.msoButtonIconAndCaption

.Caption = "Button One"

.FaceId = 65

.Tag = "c123"

End With

AddHandler buttonOne.Click, AddressOf ButtonOne_Click

newMenuBar.Visible = True

End If

Catch Ex As Exception

MessageBox.Show(Ex.Message)

End Try

End Sub

Public Sub ButtonOne_Click(ByVal buttonControl As Office. _

CommandBarButton, ByRef Cancel As Boolean)

MessageBox.Show("You clicked: " & buttonControl.Caption, _

"Custom Menu", MessageBoxButtons.OK)

End Sub

Private Sub RemoveMenubar()

Try

' If the menu already exists, remove it.

Dim foundMenu As Office.CommandBarPopup = _

applicationObject.ActiveExplorer().CommandBars.ActiveMenuBar. _

FindControl(Office.MsoControlType.msoControlPopup, _

System.Type.Missing, menuTag, True, True)

If foundMenu IsNot Nothing Then

foundMenu.Delete(True)

End If

Catch Ex As Exception

MessageBox.Show(Ex.Message)

End Try

End Sub

End Class

best regards






Re: Visual Studio Tools for Office help with the outlook add-in in vs2005

Ken Slovak

As I mentioned, you're trying to get at the Explorer.CommandBars collection too early, before that collection is fully available to you. You need to defer access to that collection until you get either an Explorer.Activate or Explorer.SelectionChange event, then do your thing with Explorer.CommandBars.

If you want you can download a template I have for VSTO 2005 SE and VB.NET on my Web site, it shows how to use an Explorer wrapper class and sorted list collection to maintain Explorers and how to handle UI for Explorers. It also shows handling the NewExplorer and NewInspector events and an Inspector wrapper in addition to the Explorer wrapper.

The templates I posted from my book are at http://www.slovaktech.com/outlook_2007_templates.htm.

The VSTO/VB.NET template should show you how to do what you want.

Ken Slovak