2种Excel自定义工具栏命令按钮图标的方式

分类:代码, 博客 标签:,

Excel中当你使用自定义工具栏时,也许希望不仅仅是使用内建的图标(FaceID属性),还更想用其它的自定义图片来作为图标,这里水文工具集给出两种自定义工具栏命令按钮的方式,其一是使用外部图片文件,其二是使用Excel工作表中嵌入的图片。具体VBA实现代码如下:

(1)使用外部图片文件作为Excel自定义工具栏命令按钮图标

Sub ImageFromExternalFile()
    Dim Btn As Office.CommandBarButton
    Set Btn = Application.CommandBars.FindControl(ID:=30007) _
        .Controls.Add(Type:=msoControlButton, temporary:=True)
    With Btn
        .Caption = "Click Me"
        .Style = msoButtonIconAndCaption
        .Picture = LoadPicture("C:\CnHUP.bmp")
    End With
End Sub

(2)使用Excel工作表中嵌入的图片作为自定义工具栏命令按钮图标

Sub ImageFromEmbedded()
    Dim P As Excel.Picture
    Dim Btn As Office.CommandBarButton
    Set Btn = Application.CommandBars.FindControl(ID:=30007) _
        .Controls.Add(Type:=msoControlButton, temporary:=True)
    Set P = Worksheets("Sheet1").Pictures("ThePictOfCnHUP")
    P.CopyPicture xlScreen, xlBitmap
    With Btn
        .Caption = "www.CnHUP.com"
        .Style = msoButtonIconAndCaption
        .PasteFace
    End With
End Sub


分类:代码, 博客 标签:,

发表评论

You must be logged in to post a comment.