簡體   English   中英

如何通過 VBA Excel 中的宏更改命令按鈕的格式

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

我制作了在某些任務完成后向外部人員發送郵件的宏。 所有這些按鈕都是相同的,它們填充了它們所在的單元格,並且都顯示為“郵件”。 每一行都是一個特定的項目,當單擊按鈕時,與該項目有關的所有必要信息都來自同一行(包括項目名稱、要郵寄到的電子郵件地址、日期等)。

但是由於所有的按鈕都是一樣的,所以不會太難點擊錯誤,然后給已經收到郵件的人重新發送郵件,這不是很專業。 因此,我希望在單擊按鈕和未單擊按鈕之間進行視覺區分。

到目前為止,我沒有成功地使用宏的 .ForeColor 或 .Backcolor 屬性更改按鈕本身的顏色,也沒有通過屬性窗口更改它。 我可以通過屬性窗口更改字體顏色,但現在我無法通過宏更改它,顯然我希望按鈕激活的宏可以更改其自己的調用方格式以輕松區分。

我嘗試谷歌搜索的所有內容都讓我解釋了如何手動更改這些格式屬性,而不是通過宏。

有人能告訴我如何解決這個問題嗎? 是否可以有條件地格式化命令按鈕?

這是我上次嘗試用來更改字體的代碼:

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

如有必要,這是完整的宏:

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

它只是.Font.Color而不是.Font.Color.RGB

我沒有看到設置背景顏色的方法,所以如果你想這樣做,使用格式化為按鈕的形狀可能更容易,而不是實際的按鈕。

編輯:使用形狀而不是按鈕的示例 -

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