Can't put a button on a Contact toolbar

M

Mike

The following code is in a wrapper class (VB6) and is triggered when a
new inspector (contact) opens.

When setting objBar, the code pops an Error 91 [Object variable or
With block variable not set].

OlObject is equal to Outlook at the time of error.

Where am I going wrong?


Private Sub colInsp_NewInspector(ByVal Inspector As Inspector)

Dim objItem As Object
Set objInsp = Inspector
Set objItem = objInsp.CurrentItem

Select Case objItem.Class
Case olContact
Dim objBar As Office.CommandBar
Dim objButton As Office.CommandBarButton
*** Set objBar = OlObject.ActiveInspector.CommandBars.item("Standard")
Set objButton = objBar.Controls.item("Test Button")
If objButton Is Nothing Then
Set objButton = objBar.Controls.Add(msoControlButton,,,,True)
With objButton
.Visible = True
.BeginGroup = True
.FaceId = 1978
.Caption = "Test Button"
.Tag = "Test Button"
.Style = msoButtonIconAndCaption
End With
End If
End Select


Thanks
-mike
 
K

Ken Slovak - [MVP - Outlook]

Put your code in the first Inspector.Activate() event. NewInspector provides
a weak object reference for the Inspector, suitable mostly for
Inspector.CurrentItem.Class and .MessageClass testing to see what type of
item was opened.
 
M

Mike

Ken Slovak - said:
Put your code in the first Inspector.Activate() event. NewInspector provides
a weak object reference for the Inspector, suitable mostly for
Inspector.CurrentItem.Class and .MessageClass testing to see what type of
item was opened.


OK, I moved the code to objInsp_Activate().

I pop a contact and the sub responds but still getting the same error
when setting objBar, the code pops an Error 91 [Object variable or
With block variable not set].

Seems that oObject.ActiveInspector doesn't exist.


Private Sub objInsp_Activate()

Set objBar = oObject.ActiveInspector.CommandBars.item("Standard")
Set objButton = objBar.Controls.item("Test Button")
If objButton Is Nothing Then
Set objButton = objBar.Controls.Add(msoControlButton, , , , True)
With objButton
.Visible = True
.BeginGroup = True
.FaceId = 1978
.Caption = "Test Button"
.Tag = "Test Button"
.Style = msoButtonIconAndCaption
End With
End If


Thanks
-mike
 
M

Mark J. McGinty

Mike said:
Ken Slovak - said:
Put your code in the first Inspector.Activate() event. NewInspector
provides
a weak object reference for the Inspector, suitable mostly for
Inspector.CurrentItem.Class and .MessageClass testing to see what type of
item was opened.


OK, I moved the code to objInsp_Activate().

I pop a contact and the sub responds but still getting the same error
when setting objBar, the code pops an Error 91 [Object variable or
With block variable not set].

Seems that oObject.ActiveInspector doesn't exist.

Why would you use ActiveInspector inside of an Inspector object's event
handler? It would be practically impossible for objInsp to not be valid in
this context, and it is literally guaranteed to reference the Inspector in
which you're interested. Why take a chance on a reference that is more
indirect?

All of the Active[whatever] references should be used only when you have no
definitive way to get the object in question, and you should always store
the return in a local [preferably early bound] variable and test with If Not
obj Is Nothing, before attempting use.


-Mark
 
M

Mike

Mark J. McGinty said:
Why would you use ActiveInspector inside of an Inspector object's event
handler? It would be practically impossible for objInsp to not be valid in
this context, and it is literally guaranteed to reference the Inspector in
which you're interested. Why take a chance on a reference that is more
indirect?

All of the Active[whatever] references should be used only when you have no
definitive way to get the object in question, and you should always store
the return in a local [preferably early bound] variable and test with If Not
obj Is Nothing, before attempting use.

Because I didn't know any better :)

Thanks for the lesson and now that I have a button I have to make it
do something. Hopefully the rest is smooth sailing.

-mike
 

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