简体   繁体   English

如何通过 VBA Excel 中的宏更改命令按钮的格式

[英]How to change the formatting of a command button through a macro in VBA Excel

I made macros that send mails to external people when certain tasks have been completed.我制作了在某些任务完成后向外部人员发送邮件的宏。 All these buttons are identical, they fill the cell they are in, and all read "Mail".所有这些按钮都是相同的,它们填充了它们所在的单元格,并且都显示为“邮件”。 Every row is a certain project, when clicking the button all necessary information pertaining to that project is being taken from that same row (including name of the project, email address to mail to, date etc.).每一行都是一个特定的项目,当单击按钮时,与该项目有关的所有必要信息都来自同一行(包括项目名称、要邮寄到的电子邮件地址、日期等)。

But since all buttons are identical, it's not all too difficult to misclick, and resend a mail to someone who already received an email, which is not very professional.但是由于所有的按钮都是一样的,所以不会太难点击错误,然后给已经收到邮件的人重新发送邮件,这不是很专业。 Thus, I would like to have a visual distinction between clicked buttons and unclicked ones.因此,我希望在单击按钮和未单击按钮之间进行视觉区分。

Thus far, I did not succeed changing the color of the button itself with the .ForeColor or .Backcolor properties with a macro, neither to change it through the properties window.到目前为止,我没有成功地使用宏的 .ForeColor 或 .Backcolor 属性更改按钮本身的颜色,也没有通过属性窗口更改它。 I was able to change the font color through the properties window, but now I can't get it to change through a macro, and obviously I would like the macro the button activates to change its own callers formatting to get the easy distinction.我可以通过属性窗口更改字体颜色,但现在我无法通过宏更改它,显然我希望按钮激活的宏可以更改其自己的调用方格式以轻松区分。

Everything I tried googling just landed me explanations on how to manually change these formatting properties, but not through a macro.我尝试谷歌搜索的所有内容都让我解释了如何手动更改这些格式属性,而不是通过宏。

Would anybody be able to tell me how i could fix this?有人能告诉我如何解决这个问题吗? Is it possible to conditional format command buttons?是否可以有条件地格式化命令按钮?

Here's the code I last tried using to change the font:这是我上次尝试用来更改字体的代码:

ActiveSheet.Buttons(Application.Caller).Font.Color.RGB = RGB(144, 238, 144)

Here's the full macro if necessary:如有必要,这是完整的宏:

Sub fuiven_goedkeuring()
Application.ScreenUpdating = False

'locatie knop ophalen
Dim Knoplocatie As Range
Set Knoplocatie = ActiveSheet.Buttons(Application.Caller).TopLeftCell

'de gegevens van AV ophalen
Dim Voornaam As String
Voornaam = Left(Cells(Knoplocatie.Row, 3), InStr(Cells(Knoplocatie.Row, 3), " ") - 1)

Dim Emailadres As String
Emailadres = Cells(Knoplocatie.Row, 5)

'gegevens fuif ophalen
Dim Datum As String
Datum = Cells(Knoplocatie.Row, 7)

Dim Naam_Fuif As String
Naam_Fuif = Cells(Knoplocatie.Row, 1)

'mail verzenden
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)


    strbody = "Dag " & Voornaam & "<br><br>" _
    & "Jullie fuifsubsidie voor " & Naam_Fuif & " op " & Datum _
    & " werd goedgekeurd door het college van burgemeester en schepenen. Het dossier is nu dus door naar de financiële dienst die de slotcontroles en uitbetaling regelt." _
    & "Dit kan best nog even duren, maar bij deze is het dus bevestigd dat jullie de fuifsubsidie zullen ontvangen."

If IsEmpty(Cells(Knoplocatie.Row, 7)) Then
    MsgBox "Vul een datum in"
    Exit Sub
End If

If IsEmpty(Cells(Knoplocatie.Row, 1)) Then
    MsgBox "Vul de naam van de fuif in"
    Exit Sub
End If

If IsEmpty(Cells(Knoplocatie.Row, 3)) Then
    MsgBox "Vul de naam van de organisator in"
    Exit Sub
End If


    On Error Resume Next

    With OutMail
        .Display
        .To = Emailadres
        .CC = ""
        .BCC = ""
        .Subject = "Goedkeuring fuifsubsidie " & Naam_Fuif & " " & Datum
        .HTMLBody = "<p style='font-family:calibri;font-size:15'>" & strbody & "</p>" & .HTMLBody
    End With

    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing


ActiveSheet.Buttons(Application.Caller).BackColor.RGB = RGB(144, 238, 144)


ActiveWorkbook.Save
Application.ScreenUpdating = True
End Sub

It's just .Font.Color not .Font.Color.RGB它只是.Font.Color而不是.Font.Color.RGB

I don't see a way to set the background color, so if you want to do that it might be easier to use shapes formatted to look like buttons, instead of actual buttons.我没有看到设置背景颜色的方法,所以如果你想这样做,使用格式化为按钮的形状可能更容易,而不是实际的按钮。

EDIT : example of using shapes instead of buttons -编辑:使用形状而不是按钮的示例 -

Sub fuiven_goedkeuring()
    
    Const SENT_COLOR As Long = 13158600 'RGB(200, 200, 200)
    
    Dim shp As Shape
    
    Set shp = ActiveSheet.Shapes(Application.Caller)
    
    If shp.Fill.ForeColor.RGB = SENT_COLOR Then
        If MsgBox("Mail already sent! Resend", _
           vbQuestion + vbYesNo, "Resend?") <> vbYes Then Exit Sub
    End If
    
    
    '<send the mail>
    
    With shp 'flag as sent
       .Fill.ForeColor.RGB = SENT_COLOR
       .TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(144, 238, 144)
    End With

    'ActiveWorkbook.Save
    Application.ScreenUpdating = True
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM