从VBA自定义ActiveX按钮 [英] Customize ActiveX button from VBA
问题描述
我编写了一段代码,当您单击一个按钮时,将其复制一张纸,将其剥离以进行格式化,然后添加一个新的按钮,并为其分配一个不同的宏.
I've made a piece of code that, when you click a button, copies a sheet, strips it for formatting and then adds a new button with a different macro assigned to it.
我的问题是我的两个按钮均为灰色,我想对其进行自定义.
My problem is that both of my buttons are grey, and I would like to customize them.
第一个按钮是Excel控件元素,并且始终存在于原始工作表上.通过右键单击字体并进入格式菜单,我已经更改了字体,字体大小,下划线并使其变为粗体,但是找不到更改其背景颜色的选项.
The first button is an Excel control element, and is always present on the original sheet. I've altered the font, font size, underlining and made it bold, by right clicking on it and entering the formatting menu, but I can't find the option to change its background color.
第二个按钮是通过VBA创建的,是ActiveX按钮.添加代码如下
The second button is created through VBA and is an ActiveX button. The code to add it is as follows
Sub TilføjKnap()
Dim Plads As Range
Dim Knap As OLEObject
Dim Kode As String
Sheets("Indleveringsplan (2)").Activate
Set Plads = ActiveSheet.Range("L7:N17")
Set Knap = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False, DisplayAsIcon:=False)
With Knap
.Name = "EksportKnap"
.Object.Caption = "Eksporter til PDF"
.Left = Plads.Left + 10
.Top = Plads.Top
.Width = Plads.Width - 10
.Height = Plads.Height
.PrintObject = False
End With
Kode = "Private Sub EksportKnap_Click()" & vbNewLine
Kode = Kode & "Call Eksporter" & vbNewLine
Kode = Kode & "End Sub"
With ActiveWorkbook.VBProject.VBComponents(Worksheets("Indleveringsplan (2)").CodeName).CodeModule
.InsertLines .CountOfLines + 1, Kode
End With
End Sub
我假设我需要在With Knap内沿BackgroundColor的方式添加一些内容,但是我无法使其正常工作.
I'm assuming that I need to add something along the lines of BackgroundColor inside the With Knap, but I can't get it to work.
话虽如此,我也想在文本下划线,更改其字体和字体大小以及使其加粗.
That being said, I would also like to underline the text, change its font and font size as well as make it bold.
推荐答案
OleObject
封装CommandButton
并通过.Object
公开.Object.BackColor = vbRed
公开其接口.
The OleObject
encapsulates the CommandButton
and exposes its interface via .Object
so .Object.BackColor = vbRed
.
您可以使用类型化的变量来获取智能感知:
You can use a typed variable to get intellisense:
Dim Button As CommandButton
With Knap
Set Button = .Object
With Button
.Caption = "Eksporter til PDF"
.BackColor = vbRed
.Font.Underline = True
...
End With
...
End With
这篇关于从VBA自定义ActiveX按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!