PDA

Click to See Complete Forum and Search --> : .Net Addin for Outlook


GRussell31
Jan 11th, 2006, 10:51 AM
Hi,

I am trying to write an AddIn for Vb.Net which will display the 'Select Names' dialog (the one that appears when you click on To when creating a blank email).

I have currently created an Addin to create a specific type of mail message, but I could not get the dialog box to appear. To resolve this I had to create my own version of the dialog box, but this is very slow when populating.

My only other thought is to create a message using a form created in Outlook, and try basing it on that.

Any suggestions would be great!

Many thanks,

-Graham

RobDog888
Jan 11th, 2006, 06:48 PM
Welcome to the Forums.

I had written code to do this exact thing over at another forum just a day or two ago.

What version of .NET are you running?

GRussell31
Jan 12th, 2006, 03:16 AM
Hi,

I am using .Net 2003, but have access to 2005 (what ever version you have will be great!).

Many thanks,

-Graham

RobDog888
Jan 12th, 2006, 10:04 AM
Ok, but you can not make an Add-In for Outlook with VS.NET. You need VSTO 2005 w/Outlook Beat 1 Add-In in order to create one in .NET. I havent installed it since its Beta but you can write one in VB 6.

GRussell31
Jan 12th, 2006, 10:10 AM
Hi,

It is possible to write an addin in vs 2003 but it does effectively use COM to communicate, which makes me think I cannot make the dialog box appear.

Is it possible to use a newly created Outlook form and subclass it to obtain the events (i.e. when a person presses a key in a textbox?). As I can't seem to pick these up with Outlook 2003.

Sadly upgrading to the new version of Outlook isn't possible for me. :(

-Graham

RobDog888
Jan 12th, 2006, 10:19 AM
A COM Add-In for Outlook would be in the form of a .dll file but there is no template or other functionality to create an Add-In for Outlook. The Visual Studio Tools for Office 2005 contains support for the other extra Add-In for Outlook but its VSTO Outlook Beta 1 stll.

You can just use automation in VB.NET to interact with Outlook but it wont be an actual Add-In. ;)

GRussell31
Jan 12th, 2006, 10:24 AM
You can create an addin in Vb 2003 using a template.

Create a new project, select project type as 'Other Projects' and then select 'Extensibility Projects' and then Shared Add-in.

You then get a wizard which you can select Outlook from.

-Graham

RobDog888
Jan 12th, 2006, 10:35 AM
Yes, you are correct. I cant believe that I missed that feature of .NET :blush: I have read on Microsofts site that an Outlook Add-In was unsupported until 2005's VSTO.

Thanks for the info :thumb: Let the programming begin. :D

Ok, give me some time as I have written some VB 6 code to display the "Select Names" dialog earlier this week. All I have to do is convert it to .NET and test. :)

RobDog888
Jan 12th, 2006, 10:56 AM
Are you adding a reference to Outlook or using Late Binding? What version of Outlook are you running?

GRussell31
Jan 13th, 2006, 03:53 AM
I am using Outlook 2003, and it Vb imports the Microsoft.Office.Core namespace, although most of the objects it defines by default are late bound (so defined as objects via vb.net).

If you have already written a vb6 addin, can you send me the code from that so that I can save you some work?

Many thanks for your help.

-Graham

RobDog888
Jan 13th, 2006, 10:05 AM
But shouldnt you be setting a reference to Outlook and using the .Interop class?
I havent written a VB6 AddIn but just a VB6 code snippet that does it.

Option Explicit
'Add a reference to MS Outlook xx.0 Object Library
'Add a reference to MS Office xx.0 Object Library
'Note: Menu Items are spelling and language specific
' Written on Outlook 2003.
Private Sub Command1_Click()
Dim oApp As Outlook.Application
Dim oCB As Office.CommandBar
Dim oCBTools As Office.CommandBarPopup
Dim oCBSelect As Office.CommandBarButton
Dim oInsp As Outlook.Inspector
Dim oCont As Outlook.MailItem

Set oApp = New Outlook.Application
oApp.GetNamespace("MAPI").Logon "", "", False, True

Set oCont = oApp.CreateItem(olMailItem)
Set oInsp = oCont.GetInspector
oInsp.Display vbModeless
oInsp.WindowState = olNormalWindow
oInsp.Left = -10000 'Set the Inspector off screen.

Set oCB = oInsp.CommandBars("Menu Bar")
Set oCBTools = oCB.Controls("&Tools")
Set oCBSelect = oCBTools.Controls("Address &Book...")
oCBSelect.Execute
MsgBox oCont.To 'Just for display example. Set into a variable or ?
oInsp.Left = 250 'Set to 250 or ? to return it to viewable location
oCont.Close olDiscard
Set oCont = Nothing
Set oCBSelect = Nothing
Set oCBTools = Nothing
Set oCB = Nothing
Set oApp = Nothing
End Sub

GRussell31
Jan 13th, 2006, 10:34 AM
Thank you, I've got the code working in .Net (I haven't tried it in my add in yet).

I have never used the Inspector object, and I hadn't even thought of using the command to select it from the menu item. I'd been digging around the object model but getting nowhere.

Thank you very much.

-Graham
P.S. Is it possible to amend the address book so that instead of showing the business phone number first, it uses the mobile number field.
:D

RobDog888
Jan 13th, 2006, 10:38 AM
Can you post your .NET version of it?

How so do you mean? To preselect the combo at the top right corner of the dialog or the number being used in the contact item?

The Inspector is only the container that displays a email message, appointmentitem, contactitem, etc.

GRussell31
Jan 13th, 2006, 10:49 AM
This is my very quick and dirty version of the code, I'll tart it up and make look pretty on Monday (when I am back at work).

Anyway here goes (sorry, don't know the code to format this and make it nice and readable):

Imports Microsoft.Office.Core

Public Class Form1
Inherits System.Windows.Forms.Form


Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim oInsp As Outlook.Inspector
Dim oMail As Outlook.MailItem

mOutlook = New Outlook.Application
oMail = mOutlook.CreateItem(Outlook.OlItemType.olMailItem)
oInsp = oMail.GetInspector

oInsp.Display(0)
oInsp.WindowState = Outlook.OlWindowState.olNormalWindow
oInsp.Left = -10000

Dim oCB As CommandBar
Dim oCBTools As CommandBarPopup
Dim oCBSelect As CommandBarButton

oCB = oInsp.CommandBars("Menu Bar")
oCBTools = oCB.Controls("&Tools")
oCBSelect = oCBTools.Controls("Address &Book...")
oCBSelect.Execute()

MsgBox(oMail.To)

End Sub
End Class


Sorry, bad explanation before...

In the dialog it shows the business phone number as the second column in the listview. I would like to change that to show the mobile phone number that is stored for the person. Which can be accessed by double clicking the persons name from the To list (to bring up their properties), select the 'Phone/Notes' tab, the mobile number is displayed there.
I think this is installed with exchange 2003, as the mobile numbers are stored in active directory under each user.

I doubt this is possible, but anyhelp would be great.

Many thanks for all of your help.

-Graham

RobDog888
Jan 13th, 2006, 11:02 AM
I dont have Exchange running but Im unsure on how to set the preference. :(

Oh, I thought you were going to post more of the app. :D

GRussell31
Jan 13th, 2006, 11:50 AM
I dont have Exchange running but Im unsure on how to set the preference. :(

Installing ex 2003 adds some parts to active directory, which is what I want to display in the dialog. Is it possible to amend that dialog at all?

[color=navy]Oh, I thought you were going to post more of the app. :D

Ah, sorry. :o
I didn't have a chance to implement it before I left work today.

When I am back at work next week, I'll make it look a bit nicer than it is currently post that. :)

Once again, many thanks for all of your help!

-Graham

RobDog888
Jan 13th, 2006, 11:52 AM
I think if you ned to highly customize the dialog, then you would want to create your own form and duplicate what you want. You can load it with the same information, not too hard.

GRussell31
Jan 16th, 2006, 03:46 AM
I think if you ned to highly customize the dialog, then you would want to create your own form and duplicate what you want. You can load it with the same information, not too hard.

Thats is what I originally was doing for the form, it was just taking a long time to populate the list view.

I'll add the ameded code for the addin later today.

-Graham

RobDog888
Jan 16th, 2006, 03:49 AM
What if you prepopulated it upon the start of Outlook or loading of your addin? Then its like a .Show and.Hide thing and not reloading each time?

NP, I'll be back later this morning and I'll check out the thread. :)

GRussell31
Jan 16th, 2006, 04:01 AM
What if you prepopulated it upon the start of Outlook or loading of your addin? Then its like a .Show and.Hide thing and not reloading each time?

NP, I'll be back later this morning and I'll check out the thread. :)

You can do that, but due to a bug (not sure if it is in .Net or Outlook) the event when outlook is closed does not fire if there are open references open (so you can't use it to clean up as you would normally).

-Graham

RobDog888
Jan 16th, 2006, 09:54 AM
Oh, thats correct. It retains your apps instance and doesnt close it. This is supposed to be fixed in the VSTO Outlook Add-In.

GRussell31
Jan 18th, 2006, 11:20 AM
Sorry for the delay, but I have been allocated to a different project now, and it doesn't look like I will be working on this again for a few weeks. :(

Anyway, the code as I have it at the moment (if it is of anyuse to anyone):

Connect.Vb (This class is mostly written by VB when the addin is created, but I have added some stuff to make an icon appear on the Outlook toolbar).

Please forgive any dodgy code, as it is very much work in progress (and it isn't very nice in some places!) :blush:
Connect.Vb

Imports Microsoft.Office.Core
imports Extensibility
Imports System.Runtime.InteropServices


#Region " Read me for Add-in installation and setup information. "
' When run, the Add-in wizard prepared the registry for the Add-in.
' At a later time, if the Add-in becomes unavailable for reasons such as:
' 1) You moved this project to a computer other than which is was originally created on.
' 2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.
' 3) Registry corruption.
' you will need to re-register the Add-in by building the SMSOutlookSetup project
' by right clicking the project in the Solution Explorer, then choosing install.
#End Region

<GuidAttribute("EF730A67-8CCE-4F1F-9EFA-F5880F767DD0"), ProgIdAttribute("SMSOutlook.Connect")> _
Public Class Connect

Implements Extensibility.IDTExtensibility2

Public applicationObject As Object
Public addInInstance As Object
Public WithEvents cmdbarMyAddin As CommandBarButton
Private frmMyForm As New frmMine

Public ReadOnly Property OutlookObject()
Get
applicationObject = OutlookObject
End Get
End Property

Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnBeginShutdown
On Error Resume Next
' Notify the user you are shutting down, and delete the button.
frmMyForm.Dispose()
frmMyForm = Nothing
cmdbarMyAddin.Delete()
cmdbarMyAddin= Nothing
End Sub

Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnAddInsUpdate
End Sub

' This event fires (I think) when Outlook has finished loading. Most of the code was auto generated by the
'AddIn (On error resume next), how nice.
' Basically it will add a button called MyAddin to the Outlook main window.
Public Sub OnStartupComplete(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnStartupComplete
Dim oCommandBars As CommandBars
Dim oStandardBar As CommandBar

On Error Resume Next
' Set up a custom button on the "Standard" command bar.
oCommandBars = applicationObject.CommandBars
If oCommandBars Is Nothing Then
' Outlook has the CommandBars collection on the Explorer object.
oCommandBars = applicationObject.ActiveExplorer.CommandBars
End If

oStandardBar = oCommandBars.Item("Standard")

' In case the button was not deleted, use the exiting one.
cmdbarMyAddin = oStandardBar.Controls.Item("My Addin")
If cmdbarMyAddin Is Nothing Then

cmdbarMyAddin = oStandardBar.Controls.Add(1)
With cmdbarMyAddin
.Caption = "My Addin"
'.Style = MsoButtonStyle.msoButtonIcon
.Style = MsoButtonStyle.msoButtonCaption

' The following items are optional, but recommended.
' The Tag property lets you quickly find the control
' and helps MSO keep track of it when more than
' one application window is visible. The property is required
' by some Office applications and should be provided.

.Tag = "My Addin"

' The OnAction property is optional but recommended.
' It should be set to the ProgID of the add-in, so that if
' the add-in is not loaded when a user clicks the button,
' MSO loads the add-in automatically and then raises
' the Click event for the add-in to handle.

.OnAction = "!<MyCOMAddin.Connect>"

.Visible = True
End With
End If

' Display a simple message to show which application you started in.

oStandardBar = Nothing
oCommandBars = Nothing
End Sub

Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnDisconnection
On Error Resume Next

If RemoveMode <> Extensibility.ext_DisconnectMode.ext_dm_HostShutdown Then _
Call OnBeginShutdown(custom)

applicationObject = Nothing
End Sub

Public Sub OnConnection(ByVal application As Object, ByVal connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection
applicationObject = application
addInInstance = addInInst

' If you aren't in startup, manually call OnStartupComplete.
If (connectMode <> Extensibility.ext_ConnectMode.ext_cm_Startup) Then _
Call OnStartupComplete(custom)

End Sub

Private Sub cmdbarMyAddin_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles cmdbarMyAddin.Click
Try
Dim frmMyForm As New frmMine
frmMyForm = New frmSms
frmMyForm.Outlookref = CType(applicationObject, Outlook.Application)
frmMyForm.Show()
Catch ex As Exception
MsgBox("An error has occured with the AddIn." & vbCrLf & "Error details : " & Err.Description)
End Try
End Sub

End Class


Then in the form (which I have made to look similar to a normal email).
Clicking on a button on that I will have to use the code you gave me above to create a new EMail message and then bring up the Address book.

I have had to strip out some peices of the code (so I hope it compiles) just my work doesn't like it if I post their name and code.
Saying that, I am happy to help as much as I can and I hope that is of some use.

Many thanks Master Yoda for all of your help! :thumb:

-Graham

RobDog888
Jan 18th, 2006, 11:30 AM
Thanks! :thumb: What I did notice is that your using the .Core instead of the .Interop. Interop is better if your running Ourlook 2002 or 2003.
Imports Microsoft.Office.Interop

GRussell31
Jan 18th, 2006, 11:33 AM
Thanks! :thumb: What I did notice is that your using the .Core instead of the .Interop. Interop is better if your running Ourlook 2002 or 2003.
Imports Microsoft.Office.Interop

That'll be the lovely auto generated code!

Good spot, I just hope I can work on it soon before I forget all this again!

Thanks,

-Graham