Ribbon custom images from VB6

  • Thread starter Peter Karlström
  • Start date
P

Peter Karlström

Hi

Does anyone have a clue on how to load a custom image to a Ribbon control
during runtime from a VB6 COM Addin?

I have the images in a resource file in the project and have tried this:
(XML parts)
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"
onLoad="Ribbon_OnLoad" loadImage="LoadImage">

<toggleButton id="btnLang1" label="Swedish" image="btnSve"
onAction="OnActionTglButton" getPressed="GetPressedTglButton"
getEnabled="GetStatus" />
<toggleButton id="btnLang2" label="English" image="btnEng"
onAction="OnActionTglButton" getPressed="GetPressedTglButton"
getEnabled="GetStatus" />

(VB6 Addin code)
Function LoadImage(imageId As String) As IPictureDisp
Select Case imageId
Case "btnSve"
Set LoadImage = LoadResPicture(1001, vbResBitmap)
Case "btnEng"
Set LoadImage = LoadResPicture(1002, vbResBitmap)
End Select
End Function

The images in the resource file is of bitmap type.

The Word userinterface responds with an exception error message, and the
images is not loaded.

Anybody?

Thanks in advance
 
P

Peter Karlström

Hi Ken

Thanks for your quick reply.

Neither LoadImage for the Ribbon tab nor the GetImage for the Togglebuttons
seems to work. VB-code runs but Ribbon doesn't abbrove with the supplied
image.

I'm beginning to believe that it's something wrong with the picture format
of my resource pictures. They are previously used in a standard Word 2003
Toolbar and are in BMP 16x11 format with 24 bit True colour.

Where can I find suggestions and limitations concerning Ribbon custom images?

Best Regards
--
Peter Karlström
Midrange AB
Sweden


Ken Slovak - said:
This is the signature for that callback as shown at
http://msdn.microsoft.com/en-us/library/aa338202.aspx#OfficeCustomizingRibbonUIforDevelopers_Images,
although the code there is VB.NET and not VB6:

Public Function GetImage(ByVal imageName As String) As stdole.IPictureDisp

I usually use a getImage callback specified in each button definition in the
XML so I don't use getImage all that often. I prefer using getImage because
that lets me change images if needed on the fly depending on application
states when I invalidate either one or all controls.

However, you might want to try this alternate signture for the callback and
see if it works for you:

Public Sub LoadImage(imageId As String, ByRef image)
Set image = LoadPicture(CurrentProject.Path & "\" & imageId)
End Sub

That signature assumes the imageID contains a full image name (myImage.BMP),
so you'd need
to add the file extension or complete file name.

See if that works better for you.
 
K

Ken Slovak - [MVP - Outlook]

Those images should be OK. I usually use PNGs that use 32-bit color and much
larger design surfaces when I create ribbon button images.

For CommandBarButton images the spec calls for 16x16x256 colors as BMP
images.

Here's some code I have for a Word COM addin in VB6 that adds some ribbon
controls to the Office area under Send. This is the signature for the
getImage callback:

Public Function WordRibbon_getImage(ByVal control As IRibbonControl) As
stdole.IPictureDisp
Dim strPath As String
Dim ResourceNumber As Integer
Dim ResourceType As String
Dim lngReturn As Long

On Error Resume Next

#If DEBUGPROC Then
Set WordRibbon_getImage = Nothing
Exit Function
#End If

ResourceType = "CUSTOM"

strPath = GetTempDir() 'ends with final "\"

Select Case control.Id
Case "Word2007ZipAttach"
ResourceNumber = RIBBON_SEND_AS_ZIP_ICON '102
strPath = strPath & RIBBON_SEND_AS_ZIP_FILE_NAME '
"InsertFileZippedIcon.png"
Case "Word2007SaveAsZip"
ResourceNumber = RIBBON_SAVE_AS_ZIP_ICON '101
strPath = strPath & RIBBON_SAVE_AS_ZIP_FILE_NAME ' "SaveAsZipIcon.png"
Case Else
ResourceNumber = 0
End Select

If ResourceNumber <> 0 Then
' save CUSTOM PNG image to file system in Temp folder
lngReturn = SaveResItemToDisk(ResourceNumber, ResourceType, strPath)
If lngReturn = 0 Then
' get saved image back from file system as IPictureDisp object
Set WordRibbon_getImage = LoadPicturePlus(strPath)
Else
Set WordRibbon_getImage = Nothing
End If
Else
Set WordRibbon_getImage = Nothing
End If

Kill strPath 'delete temporary image file
End Function

Note that I disable setting the image when running in debug mode since that
adds the marshaling done by the IDE so you're out of process at that point
and trying to pass an IPictureDisp object will cause an exception.

Now here's the XML I'm using for that ribbon setup:

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"
onLoad="WordRibbon_OnLoad">
<ribbon>
<officeMenu>
<menu idMso="FileSendMenu">
<button id="Word2007ZipAttach" insertAfterMso="FileSendAsAttachment"
getLabel="WordRibbon_getLabel" onAction="WordRibbon_onAction"
getSupertip="WordRibbon_getSuperTip" getImage="WordRibbon_getImage"
getVisible="WordRibbon_getVisible" getEnabled="WordRibbon_getEnabled" />
</menu>
<menu idMso="FileSaveAsMenu">
<button id="Word2007SaveAsZip"
insertAfterMso="FileSaveAsOtherFormats" getLabel="WordRibbon_getLabel"
onAction="WordRibbon_onAction" getSupertip="WordRibbon_getSuperTip"
getImage="WordRibbon_getImage" getVisible="WordRibbon_getVisible"
getEnabled="WordRibbon_getEnabled" />
</menu>
</officeMenu>
</ribbon>
</customUI>

In this case the images are PNG's at 32x32 at 256 colors, but I've used
32-bit and 24-bit colors. For a BMP I'd probably stick with 256 colors.
 
P

Peter Karlström

Hi Ken

I'm very sorry I haven't got back to you but I got no notification of your
post.

I think you hit the spot here. The cause of my problem was that I was
running in debugmode all the time.

In compiled mode it works as a charm.

Even my old call to LoadPicture(resourceID, resourceType) works as expected.

Again, thank you for your patience Ken.

Best Regards
--
Peter Karlström
Midrange AB
Sweden


Ken Slovak - said:
Those images should be OK. I usually use PNGs that use 32-bit color and much
larger design surfaces when I create ribbon button images.

For CommandBarButton images the spec calls for 16x16x256 colors as BMP
images.

Here's some code I have for a Word COM addin in VB6 that adds some ribbon
controls to the Office area under Send. This is the signature for the
getImage callback:

Public Function WordRibbon_getImage(ByVal control As IRibbonControl) As
stdole.IPictureDisp
Dim strPath As String
Dim ResourceNumber As Integer
Dim ResourceType As String
Dim lngReturn As Long

On Error Resume Next

#If DEBUGPROC Then
Set WordRibbon_getImage = Nothing
Exit Function
#End If

ResourceType = "CUSTOM"

strPath = GetTempDir() 'ends with final "\"

Select Case control.Id
Case "Word2007ZipAttach"
ResourceNumber = RIBBON_SEND_AS_ZIP_ICON '102
strPath = strPath & RIBBON_SEND_AS_ZIP_FILE_NAME '
"InsertFileZippedIcon.png"
Case "Word2007SaveAsZip"
ResourceNumber = RIBBON_SAVE_AS_ZIP_ICON '101
strPath = strPath & RIBBON_SAVE_AS_ZIP_FILE_NAME ' "SaveAsZipIcon.png"
Case Else
ResourceNumber = 0
End Select

If ResourceNumber <> 0 Then
' save CUSTOM PNG image to file system in Temp folder
lngReturn = SaveResItemToDisk(ResourceNumber, ResourceType, strPath)
If lngReturn = 0 Then
' get saved image back from file system as IPictureDisp object
Set WordRibbon_getImage = LoadPicturePlus(strPath)
Else
Set WordRibbon_getImage = Nothing
End If
Else
Set WordRibbon_getImage = Nothing
End If

Kill strPath 'delete temporary image file
End Function

Note that I disable setting the image when running in debug mode since that
adds the marshaling done by the IDE so you're out of process at that point
and trying to pass an IPictureDisp object will cause an exception.

Now here's the XML I'm using for that ribbon setup:

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"
onLoad="WordRibbon_OnLoad">
<ribbon>
<officeMenu>
<menu idMso="FileSendMenu">
<button id="Word2007ZipAttach" insertAfterMso="FileSendAsAttachment"
getLabel="WordRibbon_getLabel" onAction="WordRibbon_onAction"
getSupertip="WordRibbon_getSuperTip" getImage="WordRibbon_getImage"
getVisible="WordRibbon_getVisible" getEnabled="WordRibbon_getEnabled" />
</menu>
<menu idMso="FileSaveAsMenu">
<button id="Word2007SaveAsZip"
insertAfterMso="FileSaveAsOtherFormats" getLabel="WordRibbon_getLabel"
onAction="WordRibbon_onAction" getSupertip="WordRibbon_getSuperTip"
getImage="WordRibbon_getImage" getVisible="WordRibbon_getVisible"
getEnabled="WordRibbon_getEnabled" />
</menu>
</officeMenu>
</ribbon>
</customUI>

In this case the images are PNG's at 32x32 at 256 colors, but I've used
32-bit and 24-bit colors. For a BMP I'd probably stick with 256 colors.
 
P

Peter Karlström

Hi Ken

I'm very sorry I haven't got back to you but I got no notification of your
post.

I think you hit the spot here. The cause of my problem was that I was
running in debugmode all the time.

In compiled mode it works as a charm.

Even my old call to LoadPicture(resourceID, resourceType) works as expected.

Again, thank you for your patience Ken.

Best Regards
--
Peter Karlström
Midrange AB
Sweden


Ken Slovak - said:
Those images should be OK. I usually use PNGs that use 32-bit color and much
larger design surfaces when I create ribbon button images.

For CommandBarButton images the spec calls for 16x16x256 colors as BMP
images.

Here's some code I have for a Word COM addin in VB6 that adds some ribbon
controls to the Office area under Send. This is the signature for the
getImage callback:

Public Function WordRibbon_getImage(ByVal control As IRibbonControl) As
stdole.IPictureDisp
Dim strPath As String
Dim ResourceNumber As Integer
Dim ResourceType As String
Dim lngReturn As Long

On Error Resume Next

#If DEBUGPROC Then
Set WordRibbon_getImage = Nothing
Exit Function
#End If

ResourceType = "CUSTOM"

strPath = GetTempDir() 'ends with final "\"

Select Case control.Id
Case "Word2007ZipAttach"
ResourceNumber = RIBBON_SEND_AS_ZIP_ICON '102
strPath = strPath & RIBBON_SEND_AS_ZIP_FILE_NAME '
"InsertFileZippedIcon.png"
Case "Word2007SaveAsZip"
ResourceNumber = RIBBON_SAVE_AS_ZIP_ICON '101
strPath = strPath & RIBBON_SAVE_AS_ZIP_FILE_NAME ' "SaveAsZipIcon.png"
Case Else
ResourceNumber = 0
End Select

If ResourceNumber <> 0 Then
' save CUSTOM PNG image to file system in Temp folder
lngReturn = SaveResItemToDisk(ResourceNumber, ResourceType, strPath)
If lngReturn = 0 Then
' get saved image back from file system as IPictureDisp object
Set WordRibbon_getImage = LoadPicturePlus(strPath)
Else
Set WordRibbon_getImage = Nothing
End If
Else
Set WordRibbon_getImage = Nothing
End If

Kill strPath 'delete temporary image file
End Function

Note that I disable setting the image when running in debug mode since that
adds the marshaling done by the IDE so you're out of process at that point
and trying to pass an IPictureDisp object will cause an exception.

Now here's the XML I'm using for that ribbon setup:

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"
onLoad="WordRibbon_OnLoad">
<ribbon>
<officeMenu>
<menu idMso="FileSendMenu">
<button id="Word2007ZipAttach" insertAfterMso="FileSendAsAttachment"
getLabel="WordRibbon_getLabel" onAction="WordRibbon_onAction"
getSupertip="WordRibbon_getSuperTip" getImage="WordRibbon_getImage"
getVisible="WordRibbon_getVisible" getEnabled="WordRibbon_getEnabled" />
</menu>
<menu idMso="FileSaveAsMenu">
<button id="Word2007SaveAsZip"
insertAfterMso="FileSaveAsOtherFormats" getLabel="WordRibbon_getLabel"
onAction="WordRibbon_onAction" getSupertip="WordRibbon_getSuperTip"
getImage="WordRibbon_getImage" getVisible="WordRibbon_getVisible"
getEnabled="WordRibbon_getEnabled" />
</menu>
</officeMenu>
</ribbon>
</customUI>

In this case the images are PNG's at 32x32 at 256 colors, but I've used
32-bit and 24-bit colors. For a BMP I'd probably stick with 256 colors.
 
P

Peter Karlström

Hi Ken

I'm very sorry I haven't got back to you but I got no notification of your
post.

I think you hit the spot here. The cause of my problem was that I was
running in debugmode all the time.

In compiled mode it works as a charm.

Even my old call to LoadPicture(resourceID, resourceType) works as expected.

Again, thank you for your patience Ken.

Best Regards
--
Peter Karlström
Midrange AB
Sweden


Ken Slovak - said:
Those images should be OK. I usually use PNGs that use 32-bit color and much
larger design surfaces when I create ribbon button images.

For CommandBarButton images the spec calls for 16x16x256 colors as BMP
images.

Here's some code I have for a Word COM addin in VB6 that adds some ribbon
controls to the Office area under Send. This is the signature for the
getImage callback:

Public Function WordRibbon_getImage(ByVal control As IRibbonControl) As
stdole.IPictureDisp
Dim strPath As String
Dim ResourceNumber As Integer
Dim ResourceType As String
Dim lngReturn As Long

On Error Resume Next

#If DEBUGPROC Then
Set WordRibbon_getImage = Nothing
Exit Function
#End If

ResourceType = "CUSTOM"

strPath = GetTempDir() 'ends with final "\"

Select Case control.Id
Case "Word2007ZipAttach"
ResourceNumber = RIBBON_SEND_AS_ZIP_ICON '102
strPath = strPath & RIBBON_SEND_AS_ZIP_FILE_NAME '
"InsertFileZippedIcon.png"
Case "Word2007SaveAsZip"
ResourceNumber = RIBBON_SAVE_AS_ZIP_ICON '101
strPath = strPath & RIBBON_SAVE_AS_ZIP_FILE_NAME ' "SaveAsZipIcon.png"
Case Else
ResourceNumber = 0
End Select

If ResourceNumber <> 0 Then
' save CUSTOM PNG image to file system in Temp folder
lngReturn = SaveResItemToDisk(ResourceNumber, ResourceType, strPath)
If lngReturn = 0 Then
' get saved image back from file system as IPictureDisp object
Set WordRibbon_getImage = LoadPicturePlus(strPath)
Else
Set WordRibbon_getImage = Nothing
End If
Else
Set WordRibbon_getImage = Nothing
End If

Kill strPath 'delete temporary image file
End Function

Note that I disable setting the image when running in debug mode since that
adds the marshaling done by the IDE so you're out of process at that point
and trying to pass an IPictureDisp object will cause an exception.

Now here's the XML I'm using for that ribbon setup:

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"
onLoad="WordRibbon_OnLoad">
<ribbon>
<officeMenu>
<menu idMso="FileSendMenu">
<button id="Word2007ZipAttach" insertAfterMso="FileSendAsAttachment"
getLabel="WordRibbon_getLabel" onAction="WordRibbon_onAction"
getSupertip="WordRibbon_getSuperTip" getImage="WordRibbon_getImage"
getVisible="WordRibbon_getVisible" getEnabled="WordRibbon_getEnabled" />
</menu>
<menu idMso="FileSaveAsMenu">
<button id="Word2007SaveAsZip"
insertAfterMso="FileSaveAsOtherFormats" getLabel="WordRibbon_getLabel"
onAction="WordRibbon_onAction" getSupertip="WordRibbon_getSuperTip"
getImage="WordRibbon_getImage" getVisible="WordRibbon_getVisible"
getEnabled="WordRibbon_getEnabled" />
</menu>
</officeMenu>
</ribbon>
</customUI>

In this case the images are PNG's at 32x32 at 256 colors, but I've used
32-bit and 24-bit colors. For a BMP I'd probably stick with 256 colors.
 
P

Peter Karlström

Hi Ken

I'm very sorry I haven't got back to you but I got no notification of your
post.

I think you hit the spot here. The cause of my problem was that I was
running in debugmode all the time.

In compiled mode it works as a charm.

Even my old call to LoadPicture(resourceID, resourceType) works as expected.

Again, thank you for your patience Ken.

Best Regards
--
Peter Karlström
Midrange AB
Sweden


Ken Slovak - said:
Those images should be OK. I usually use PNGs that use 32-bit color and much
larger design surfaces when I create ribbon button images.

For CommandBarButton images the spec calls for 16x16x256 colors as BMP
images.

Here's some code I have for a Word COM addin in VB6 that adds some ribbon
controls to the Office area under Send. This is the signature for the
getImage callback:

Public Function WordRibbon_getImage(ByVal control As IRibbonControl) As
stdole.IPictureDisp
Dim strPath As String
Dim ResourceNumber As Integer
Dim ResourceType As String
Dim lngReturn As Long

On Error Resume Next

#If DEBUGPROC Then
Set WordRibbon_getImage = Nothing
Exit Function
#End If

ResourceType = "CUSTOM"

strPath = GetTempDir() 'ends with final "\"

Select Case control.Id
Case "Word2007ZipAttach"
ResourceNumber = RIBBON_SEND_AS_ZIP_ICON '102
strPath = strPath & RIBBON_SEND_AS_ZIP_FILE_NAME '
"InsertFileZippedIcon.png"
Case "Word2007SaveAsZip"
ResourceNumber = RIBBON_SAVE_AS_ZIP_ICON '101
strPath = strPath & RIBBON_SAVE_AS_ZIP_FILE_NAME ' "SaveAsZipIcon.png"
Case Else
ResourceNumber = 0
End Select

If ResourceNumber <> 0 Then
' save CUSTOM PNG image to file system in Temp folder
lngReturn = SaveResItemToDisk(ResourceNumber, ResourceType, strPath)
If lngReturn = 0 Then
' get saved image back from file system as IPictureDisp object
Set WordRibbon_getImage = LoadPicturePlus(strPath)
Else
Set WordRibbon_getImage = Nothing
End If
Else
Set WordRibbon_getImage = Nothing
End If

Kill strPath 'delete temporary image file
End Function

Note that I disable setting the image when running in debug mode since that
adds the marshaling done by the IDE so you're out of process at that point
and trying to pass an IPictureDisp object will cause an exception.

Now here's the XML I'm using for that ribbon setup:

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"
onLoad="WordRibbon_OnLoad">
<ribbon>
<officeMenu>
<menu idMso="FileSendMenu">
<button id="Word2007ZipAttach" insertAfterMso="FileSendAsAttachment"
getLabel="WordRibbon_getLabel" onAction="WordRibbon_onAction"
getSupertip="WordRibbon_getSuperTip" getImage="WordRibbon_getImage"
getVisible="WordRibbon_getVisible" getEnabled="WordRibbon_getEnabled" />
</menu>
<menu idMso="FileSaveAsMenu">
<button id="Word2007SaveAsZip"
insertAfterMso="FileSaveAsOtherFormats" getLabel="WordRibbon_getLabel"
onAction="WordRibbon_onAction" getSupertip="WordRibbon_getSuperTip"
getImage="WordRibbon_getImage" getVisible="WordRibbon_getVisible"
getEnabled="WordRibbon_getEnabled" />
</menu>
</officeMenu>
</ribbon>
</customUI>

In this case the images are PNG's at 32x32 at 256 colors, but I've used
32-bit and 24-bit colors. For a BMP I'd probably stick with 256 colors.
 

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