opening contact form: event?

W

Willem Peper

Hi,
I want to add a command bar to the contact window as soon as it opens. This
command bar shall not display on mail or task windows.
Is there an event to trigger the macro or how can I achieve it?

Thanks
Willem
 
S

Sue Mosher [MVP-Outlook]

Use the Inspectors.NewInspector event and check the value of Inspector.CurrentItem.Class to determine what type of item was opened.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
K

Ken Slovak - [MVP - Outlook]

In the NewInspector event of the Inspectors collection check for
Inspector.CurrentItem.Class = olContact. If that is true then create your
button. I would most likely use the Inspector.Activate event to create the
button, setting a flag so the create code only runs on the first Activate.
 
W

Willem Peper

Sue and Ken
Thank you for your advice.
I put some code together which still has problems.
If I start Outlook and open a contact the addMyMenu is called but results in
a runtime error '91' object variable or with-block variable are missing.

If I start addMyMenu manually from the open contact it adds the popup menu
as requested.

What#s the fault?
Please check my code.

Thanks
Willem

VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "DieseOutlookSitzung"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = True
Option Explicit

Public myOlApp As New Outlook.Application
Public WithEvents myOlInspectors As Outlook.Inspectors
Attribute myOlInspectors.VB_VarHelpID = -1

Private Sub Application_Startup()
Set myOlInspectors = myOlApp.Inspectors
End Sub

Public Sub myOlInspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
If Inspector.CurrentItem.Class = olContact Then
addMyMenu
End If
End Sub

Public Sub addMyMenu()
Dim OlApp As Outlook.Application

Dim colCB As Office.CommandBars
Dim objCB As Office.CommandBar
Dim objCBMenu As Office.CommandBarPopup
Dim objCBMenuCB As Office.CommandBar
Dim objCBB As Office.CommandBarButton

Set OlApp = CreateObject("Outlook.Application")
Set colCB = OlApp.ActiveInspector.CommandBars '***** Runtime error '91'
Set objCB = colCB.Item("Menu Bar")
Set objCBMenu = objCB.Controls.Add(Type:=msoControlPopup,
Temporary:=True)

With objCBMenu
.Caption = "Spezial"
Set objCBMenuCB = .CommandBar

Set objCBB = objCBMenuCB.Controls.Add(Type:=msoControlButton, ID:=1,
Temporary:=True)
objCBB.Caption = "Besuchsbericht"
objCBB.OnAction = "erfassKontakt2"
Set objCBB = objCBMenuCB.Controls.Add(Type:=msoControlButton, ID:=1,
Temporary:=True)
objCBB.Caption = "Kontakt kopieren"
objCBB.OnAction = "copy_contact_info"
Set objCBB = objCBMenuCB.Controls.Add(Type:=msoControlButton, ID:=1,
Temporary:=True)
objCBB.Caption = "Angebot"
objCBB.OnAction = "Angebotsanfrage"

End With

Set OlApp = Nothing
Set colCB = Nothing
Set objCB = Nothing
Set objCBMenu = Nothing
Set objCBMenuCB = Nothing
Set objCBB = Nothing

End Sub
 
S

Sue Mosher [MVP-Outlook]

Use the intrinsic Application object (the one you invoke with the Application_Startup event handler). Don't create a new one with CreateObject.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
W

Willem Peper

Sorry,
but it doesn't fix the problem.

In the mean time I played around with the blocking line and got following
results.
ActiveExplorer and activeWindow work if triggere by opening a contact but
put the new menu in the wrong command bar.

Set colCB = myOlApp.ActiveExplorer.CommandBars ' triggered: new menu in
the main command bar / Manually: new menu in the main command bar
Set colCB = myOlApp.ActiveWindow.CommandBars 'triggered: new menu in the
main command bar / Manually: new menu in the contact command bar
Set colCB = myOlApp.ActiveInspector.CommandBars 'triggered: Runtime error
'91' / Manually: new menu in the contact command bar

Why does ActiveExplorer not work?
Regards
Willem




Use the intrinsic Application object (the one you invoke with the
Application_Startup event handler). Don't create a new one with
CreateObject.
 
W

Willem Peper

I forgot to mention that I use Outlook 2002 and Excel/Word 2000 on XP SP2.
Regards
Willem
 
S

Sue Mosher [MVP-Outlook]

I would never use ActiveWIndow.CommandBars, only ActiveExplorer.CommandBars or ActiveInspector.CommandBars.

I'm now confused, though, about what's not working and what ActiveExplorer has to do with this at all./

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
W

Willem Peper

The inspector for the selected customer was not active. after declaring
myItem as Contactitem with events and moving the command bar handling to
myItem_read
everything seems to work.

and if anybody is interessted, here is the working code which has to be
place in ThisOutlookSession.

Regards
Willem



Option Explicit

Public myOlApp As New Outlook.Application
Public WithEvents myOlInspectors As Outlook.Inspectors
Public WithEvents myItem As Outlook.ContactItem

Private Sub Application_Startup()
Set myOlInspectors = myOlApp.Inspectors
End Sub

Public Sub myOlInspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
If Inspector.CurrentItem.Class = olContact Then
Set myItem = Inspector.CurrentItem
End If
End Sub
Private Sub myItem_Read()
addMyMenu
End Sub
Public Sub addMyMenu()
Dim colCB As Office.CommandBars
Dim objCB As Office.CommandBar
Dim objCBMenu As Office.CommandBarPopup
Dim objCBMenuCB As Office.CommandBar
Dim objCBB As Office.CommandBarButton

Set colCB = myOlApp.ActiveInspector.CommandBars
Set objCB = colCB.Item("Menu Bar")
Set objCBMenu = objCB.Controls.Add(Type:=msoControlPopup,
Temporary:=True)

With objCBMenu
.Caption = "Spezial"
Set objCBMenuCB = .CommandBar

Set objCBB = objCBMenuCB.Controls.Add(Type:=msoControlButton, ID:=1,
Temporary:=True)
objCBB.Caption = "Besuchsbericht"
objCBB.OnAction = "erfassKontakt2"
Set objCBB = objCBMenuCB.Controls.Add(Type:=msoControlButton, ID:=1,
Temporary:=True)
objCBB.Caption = "Kontakt kopieren"
objCBB.OnAction = "copy_contact_info"
Set objCBB = objCBMenuCB.Controls.Add(Type:=msoControlButton, ID:=1,
Temporary:=True)
objCBB.Caption = "Angebot"
objCBB.OnAction = "Angebotsanfrage"

End With

Set myOlApp = Nothing
Set colCB = Nothing
Set objCB = Nothing
Set objCBMenu = Nothing
Set objCBMenuCB = Nothing
Set objCBB = Nothing

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top