Copying a FaceID on a userform commandbutton !

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,817
Office Version
  1. 2016
Platform
  1. Windows
Hi Guys,

I am trying to copy The Excel standard print icon on a userform command button that runs a printing procedure on clicking on it.

I have tried different approaches but without any luck.

The following code copies the icon on cell A1 of the activesheet:


Sub copy_faceid()

CommandBars(1).Controls(1).Controls("print...").CopyFace

ActiveSheet.Paste Cells(1, 1)

Application.CutCopyMode = False

End Sub


The approach above however, doesn't seem to work with userforms.

Any suggestions will be much appreciated as I have been like mad looking for a solution for a long long time.

Thanks.

Jaafar.
 
I realized that no always ID matches the faceID from msdn:

Note When you specify a control's Id property, you also specify the action the control will take when it is selected and, if applicable, the image that appears on the face of the control. To add a control's image without its built-in action, you specify only the FaceId property.

So I decided to get all the files in some "/icons" folder:

Code:
Private Sub generateFaceIdsBitmapFiles(count As Integer)
    Dim i As Integer
    
    For i = 0 To count
        Set c = Application.CommandBars.FindControl(Type:=msoControlButton, Tag:=i)
        If Not c Is Nothing Then
            c.CopyFace
            Dim ImgFileName As String
            ImgFileName = Environ("userprofile") & "\icons\faceId-" & i & ".bmp"
            '// Save the img
            SavePicture PastePicture(xlBitmap), ImgFileName
            'Kill ImgFileName
        End If
    Next
End Sub

Private Sub createDumbCommandBar(count As Integer, cbName As String)
    Dim objCBs
    Dim objMyCB
    Dim objButton
    Dim i
    
    On Error Resume Next
        Application.CommandBars(cbName).delete
    On Error GoTo 0
    
    Set objCBs = Application.CommandBars
    Set objMyCB = objCBs.Add(cbName)
    objMyCB.Visible = True
    For i = 0 To count
        Set objButton = objMyCB.Controls.Add(Type:=msoControlButton)
        objButton.Style = 3
        objButton.Caption = i
        objButton.faceId = i
        objButton.Tag = i
        objButton.Visible = True
    Next
End Sub

Public Sub buildIconFiles()
    Dim max As Integer
    Dim name As String
    
    name = "DumbCommandBar"
    max = 50
    
    createDumbCommandBar max, name
    generateFaceIdsBitmapFiles max
    Application.CommandBars(name).delete
    
End Sub

It seems that the command bar needs to be showing or visible for the .CopyFace method work. I have try the code from Ivan F Moala without success.

And then instead I could set the image programmatically or in the design mode.

Regards
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Forum statistics

Threads
1,225,248
Messages
6,183,843
Members
453,192
Latest member
BenToB

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top