简体   繁体   中英

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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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