Menu Bar - Controls.Add works differently on different emails

  • Thread starter redryderridesagain
  • Start date
R

redryderridesagain

I am trying to add a button to the menu bar after opening an email
with varying results. I use the code;

Private Sub oOpenMail_Open(Cancel As Boolean)
'Event gets triggered if oOpenMail is opened!
Dim oMailBar As Office.CommandBar
'Set a reference to commandbar Standard and add the commandbutton

Set oMailBar = oMailInspector.CommandBars("Menu Bar")
'Clean up left over buttons if any
Call DeleteButtons(oMailBar)

'set oMailTagsButton
Set oMSubjectButton =
oMailBar.Controls.Add(Type:=msoControlButton)
oMailBar.Visible = True
'Set properties of "Msubject" button
With oMSubjectButton
.Caption = "MSubject"
On Error Resume Next
.Picture = LoadPicture(root & "spidey.bmp")
On Error GoTo 0
.Style = msoButtonIconAndCaption
End With
'Clean up
Set oMailBar = Nothing
End Sub

Email is either in rich text or html format there are either 8 or 9
items on the menu bar before I add my button. The icon is sometimes
displayed with caption and sometimes the caption by itself. The module
behind the button is sometimes triggered and aassumes foreground and
sometimes triggered and remains behind other windows. Sometimes no
action is triggered or at least the form which is the action does not
show up. Why so many behaviors for a message? All suggestions/comments
welcomed. Thanks
 
K

Ken Slovak - [MVP - Outlook]

For one thing, the event to use to create toolbar UI is
Inspector.Activate(), not item.Open().

For a second thing, you cannot use button.Picture or button.Mask for
WordMail 2003 or earlier versions of WordMail. If any of the items are
WordMail items you must use the button.PasteFace() method of putting the
image on the clipboard instead of using Picture or Mask.

Both Picture and Mask take an IPictureDisp object. IPictureDisp objects
cannot be passed across process boundaries. In the case of the Outlook
editor a COM addin will be running in-process with Outlook so you can use
Picture and Mask. If WordMail is being used the editor is a subclassing of
word.exe and runs out of process with Outlook, therefore attempts to pass an
IPictureDisp object will fire an exception.
 
R

redryderridesagain

re activate vs open - I am triggering the procedure with the following
code;

Private Sub oAppInspectors_NewInspector(ByVal Inspector As Inspector)
'Event gets triggered every time a Window or Item is opened in
Outlook Interface
'Like: E-mail, Contacts, Tasks
....
ElseIf Inspector.CurrentItem.Class = 43 Then
'Deal with Mail Items...else exit
'Set a reference to the e-mail to trap the Open event
Set oOpenMail = Inspector.CurrentItem
Set oMailInspector = Inspector
'MsgBox "new mail inspector"

Do you mean that I should create a procedure;

Private Sub oOpenMail_Activate(Cancel As Boolean)
'Event gets triggered if oOpenMail is opened!

Re: PasteFace
The CopyBitmapAsButtonFace function is not defined - do you know what
the library name is so I can reference it?

Thanks
 
K

Ken Slovak - [MVP - Outlook]

In Outlook 2007 and generally with WordMail you only get a weak object
reference for the Inspector passed to you in the NewInspector() event
handler. That means that all properties may not reliably be there. In
general you should only use the .Class or .EntryID or .MessageClass
properties from Inspector.CurrentItem in that event handler.

When Inspector.Activate() fires is when you should be creating UI for your
Inspector. There are lots of examples of Inspector wrappers and event
handlers on the Web, depending on what version of Outlook you are
programming for you can find examples for that version.

CopyBitmapAsButtonFace is not a library function that you can reference,
it's a function that has to be developed using Win32 API calls or copied
from an example of the function.
 
R

redryderridesagain

Ken,

You are advising me to set up my UI through the use of the
inspector.activate (event?) The defn below suggests that this would be
triggered in the course of executing a main program rather than
through an "external" event (newinspector)

Inspector.Activate Method
Outlook Developer Reference

Activates an inspector window by bringing it to the foreground and
setting keyboard focus.

The source of my example is;

http://www.vbaexpress.com/kb/getarticle.php?kb_id=502

I just want to change the UI on opening a mail or appointment item. Is
there a way of doing this. Is there some code I can crib from?

Thanks
 
K

Ken Slovak - [MVP - Outlook]

No, not the Activate() method, the Activate event.

I don't know if you're using VB or VBA but there are examples of Inspector
handling as I mentioned, Just do a Google search on "Inspector wrapper".

The example you mention doesn't use the Inspector events instantiated from
Inspectors.NewInspector(), it uses item events. I'm suggesting using
something like this, leaving aside the notion of handling multiple open
Inspectors which is what an Inspector wrapper is used for:

Private Sub oAppInspectors_NewInspector(ByVal Inspector As Inspector)
'Event gets triggered every time a Window or Item is opened in Outlook
Interface
'Like: E-mail, Contacts, Tasks

If Inspector.CurrentItem.Class <> olMail Then
'Only deal with Email Items...else exit
Exit Sub
End If

Set oMailInspector = Inspector
End Sub

Private Sub oMailInspector_Activate()

' UI creation here

End Sub
 
R

redryderridesagain

Ken and others,

I fear most of my unexpcted results stem from using VBA instead of VB;

WRT CopyBitmapAsButtonFace - This code sample ... is designed for VB
only (not VBA)

WRT to oMailInspector_Activate() - I got it working, however, the
behavior seems to be pretty much the same as before i.e. order of
windows changed and one in focus is not on top but otherwize
functional.

I learned some things. Thanks for your help.
 
K

Ken Slovak - [MVP - Outlook]

There's nothing in the CopyBitmapAsButtonFace() that won't run in VBA code.
I just looked at that code again. It's using Win32 API calls to work with
the Clipboard, so it should work fine. Where do you see that it won't work
with VBA code?
 
R

redryderridesagain

It said so in the code i.e. "This code sample ... is designed for VB
only (not VBA) ".

The compiler also choked on things such as the following during
compile;

- constants such as vbPicTypeBitmap
- code such as ' Let VB copy the bitmap to the clipboard (for the
CF_DIB).
Clipboard.SetData picSource, vbCFDIB

Thanks for looking at it again
 
K

Ken Slovak - [MVP - Outlook]

Sorry. I missed that reference to Clipboard, which isn't available in VBA
code, only in VB code.

To replace that you'd have to work entirely with the Win32 API clipboard,
which is much more complicated to work with. That's probably out of scope
for what you're doing. I guess I'd probably recommend switching from VBA
code to a compiled language such as VB6 or one of the .NET languages.
 
K

Ken Slovak - [MVP - Outlook]

Supposedly, but I've had nothing but problems with it when I've tried it so
I stopped recommending it years ago.
 
R

redryderridesagain

Ken, Michael,

I really appreciate help from both of you on this problem. I am
writing to say that use of VB6 or .NET is probably not an option
because of corporate policy and 2) I would like everything that I
write to be freely useable/modifiable by those who only have the
Office suite.

Regards
 

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