Outlook CommandBarButton Picture & Mask

N

Nathan Hapke

Hi all,

I'm working on an Outlook (target versions are 2003 and 2007) addin to
add a CommandBarButton to the Standard toolbar of the main Explorer
window, and Mail Inspector windows. I'm writing this in C#.

I want a custom icon on the button, I initially used the
Clipboard.SetDataObject(...) and button.PasteFace(), which does seem to
work, however, the fact that it overwrites the clipboard is a bit of a
show stopper.

I'm trying to rewrite my code to use the AxHost, and hopefully be able
to set a Mask as well to get transparency:

ResourceManager rm = MyProject.Properties.Resources.ResourceManager;
stdole.IPictureDisp tempImage = null;
ImageList newImageList = new ImageList();
newImageList.Images.Add((Icon)rm.GetObject("buttonIcon"));
tempImage = ConvertImage.Convert(newImageList.Images[0]);
button.Picture = tempImage;
//repeat to set the .Mask

sealed public class ConvertImage : System.Windows.Forms.AxHost {
public ConvertImage() : base(Guid.Empty.ToString()) {
}
public static stdole.IPictureDisp Convert
(System.Drawing.Image image) {
return (stdole.IPictureDisp)System.
Windows.Forms.AxHost.GetIPictureDispFromPicture(image);
}
}

but doing so crashes Outlook with a 0x8000FFFF, which is what I see from
http://support.microsoft.com/kb/286460/en-us.

Can anyone see anything wrong with what I've done?

Thanks in advance,
Nathan
 
K

Ken Slovak - [MVP - Outlook]

Are you crashing on all items or just on WordMail items?

In Outlook 2003 WordMail is really an instance of a subclassed msword.exe,
used as the editor. In Outlook 2007 WordMail is the only option and is a
dll, so it runs in-process with Outlook.

For all other types of items including emails with the Outlook editor and
all emails on Outlook 2007 using AxHost should work. For WordMail 2003 items
you are crossing process boundaries between Outlook and Word, so AxHost will
fail.

You have 2 alternatives. You can use AxHost for everything other than
WordMail 2003 items and use the clipboard for that. Or you can get
Word.Application from WordEditor.Parent and run your code in-process with
that Word instance to be able to use AxHost. That however may require a Word
addin or a Word macro template.
 
N

Nathan Hapke

Ken, thanks very much for your prompt reply.

I'm also crashing when I set an icon on a button on the Outlook Explorer
window, I haven't investigated contacts/calendar entries too closely,
because at this point they don't fall within the realm of my addin. On
my 2003 development environment I have the 'Use Word 2003' to read &
edit emails, does it make sense that it uses word anyway?

I saw some sample code that grabbed the WordEditor and proceeded to use
wordApp.GetType().InvokeMember(...) to invoke a method on a word
document template. Have you had any experience with this approach?

Thanks,
Nathan
 
K

Ken Slovak - [MVP - Outlook]

I haven't tried that with wordApp.GetType().InvokeMember(...), so I can't
speak to that.

I can see crashes on emails in WordMail using AxHost, that would be
expected. For command bar controls it would not be expected for Explorers
and other Inspectors. You are using images that meet the standard of 16x16
pixels and 256 colors?

Here's the AxHost code I use in my C# addins.

private class MyHost : AxHost
{
public MyHost() : base("59EE46BA-677D-4d20-BF10-8D8067CB8B33")
{
}

public static IPictureDisp GettIPictureDispFromPicture(Image
image)
{
return (IPictureDisp) GetIPictureDispFromPicture(image);
}
}

button.Picture = MyHost.GettIPictureDispFromPicture(image);

"image" is an image file stored in a resource of my project. The MyHost
class is in a cs class that is called CustomIconCreator and is called from
that class:

internal class CustomIconCreator
{
internal static void SetCustomIcon(CommandBarButton button, string
fileName, bool isWordEditor)
{
 
N

Nathan Hapke

Hmm. The icons I was using were 16x16 at 24bpp, but I just tried at 256
colours, and didn't have any better results.

Up until this point I have been under the understanding that for any of
this iconing stuff to work, we need to be in a Single Thread Apartment.

ThreadStart ts = new ThreadStart(this.AddIcon);
Thread t = new Thread(ts);
t.SetApartmentState(ApartmentState.STA);
t.Start();

Is this wrong, or insufficient for what we're doing?

--

I also just noticed upon re-reading
http://support.microsoft.com/kb/286460/en-us that "the Mask and Picture
properties can only be called in-process (VBA macros, Automation
Add-ins, and ActiveX DLLs run in-process)". This listing seems to
exclude C# COM addins... would this explain why my code does not work
even on Explorers and non-WordMail Inspectors?

Nathan
 
K

Ken Slovak - [MVP - Outlook]

I don't think the threading model has anything to do with it. I use both
VB.NET and C# to create command bar button pictures for Explorer and
non-WordMail Inspectors with no problems using AxHost and the correct icons,
using the Mask and Picture properties and passing them IPictureDisp objects,
so it's not impossible at all.

Did you try the code I posted? It was taken from working addins.
 
N

Nathan Hapke

Ken said:
I don't think the threading model has anything to do with it. I use both
VB.NET and C# to create command bar button pictures for Explorer and
non-WordMail Inspectors with no problems using AxHost and the correct
icons, using the Mask and Picture properties and passing them
IPictureDisp objects, so it's not impossible at all.

Did you try the code I posted? It was taken from working addins.

Ahh, I just worked with your code now, and have got it working for 2003
Explorers and non-wordmail inspectors. Thanks.

Now we'll need to figure out what approach we want to take with regards
to 2003 wordmail inspectors, be it the Clipboard, or Word Addin/Macro
template. Have you done the Word add-in/macro approach before? (If so,
any hiccups I should be aware of?)

Thanks,
Nathan
 
K

Ken Slovak - [MVP - Outlook]

I use the clipboard for WordMail button images, I haven't used a Word macro
or Word code.
 
N

Nathan Hapke

Ken,

Thanks for all your help so far, AxHost is working well. (We've decided
to delay on bothering with WordMail for now.)

For now, the 256 colour will be okay, but down the road I can see it
being necessary to have 24bit colour icons. What would this entail?

Thanks in advance,
Nathan
 
K

Ken Slovak - [MVP - Outlook]

It would entail using the ribbon and Office 2007.

The spec for Office CommandBarButton images is not going to change and is
fixed at 256 color maximum. I use 32-bit color images (PNG) files for ribbon
images.
 
N

Nathan Hapke

Good to know. Though, interestingly when I was using the Clipboard
approach, 24bpp images seemed to work.

Nathan
 
K

Ken Slovak - [MVP - Outlook]

If they would work at all they'd downgrade to 8-bit color, that's all a
CommandBarButton can use as an image whether it's from an IPictureDisp
object (Picture or Mask or AxHost) or the clipboard.
 
N

Nathan Hapke

Ken said:
If they would work at all they'd downgrade to 8-bit color, that's all a
CommandBarButton can use as an image whether it's from an IPictureDisp
object (Picture or Mask or AxHost) or the clipboard.

Hmm. Are there any formats that allow an adaptive palette? I ask
because when I was using the clipboard with a 24bpp image, I was getting
much better colour fidelity than with 256 colours.

Nathan
 
K

Ken Slovak - [MVP - Outlook]

Not that I've ever heard of, and not in any published specs for the images.
 

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