1

In Outlook 2010 I have a userform called UserForm1. Here I create a button dynamically and show the form. How can I add an event (the sub btnLoad_OnClick) when the button was clicked?

Here is my code:

Dim btnLoad As MSForms.CommandButton

Sub btnLoad_OnClick()
    MsgBox ("Button Clicked")
End Sub

Sub SaveAttachment()

    Set btnLoad = UserForm1.Controls.Add("Forms.CommandButton.1", "btnLoad", True)

    With btnLoad
        .Caption = "Click Me"
        .Left = 30
        .Top = 30
        .Height = 30
        .Width = 60
    End With

    With UserForm1
        .Width = 850
        .Show
    End With

End Sub
braX
  • 11,506
  • 5
  • 20
  • 33
OLLI_S
  • 67
  • 2
  • 10

1 Answers1

1

In your UserForm1 module add

Private Sub btnLoad_Click()
    MsgBox "Button Clicked"
End Sub

and save it.
Whenever you dynamically add the button btnLoad and click it the message should pop up.

Nacorid
  • 783
  • 8
  • 21
  • In VBA I have a module called `Module1`, the source code in my original posting is also stored in `Module1`. I want the `btnLoad_Click()` also to be stored in "Module1", not in `UserForm1`. Reason for this is: I want to deliver the Outlook VBA to my colleagues and when I tell them to create a new module, a new form and paste all source code in the `Module1` then it is easier than pasting the source code in 3 different files. See also: https://stackoverflow.com/questions/59087790/outlook-vba-how-to-deliver-vba-and-userforms – OLLI_S Nov 29 '19 at 09:58
  • 1
    The event handling code for a button on a userform should be in said userform's module. This is not only good practice, it's also easier, because you wouldn't need additional class modules, since you can only have event handling code in class modules. `Module1` is not a class module, unless your class is called `Module1`. Therefore event handling code will not work in it. If you don't want to put the code in your `UserForm1` module you need to add a new class and declare your button `WithEvents` to handle the event. – Nacorid Nov 29 '19 at 10:03
  • 1
    Why don't you Rightclick -> Export your Modules and Userform to distribute it instead of having someone else creating new modules and pasting the code? – Nacorid Nov 29 '19 at 10:05
  • I only know that I can export each UserForm and each Module separately. So I currently have to export 2 UserForms and 1 Module. Is there a way to export everything in one package? – OLLI_S Nov 29 '19 at 10:13
  • As far as I'm aware, there is not, no. – Nacorid Nov 29 '19 at 10:15
  • 1
    OK, Nacorid, I marked your answer above as solution because it is the solution if you work normally. I have a very special constellation, here your "Export" feedback will help. – OLLI_S Nov 29 '19 at 10:59
  • @OLLI_S I still think you would be better to go for an Excel workbook. You place it on a shared drive and you all can access it. You do not have to get each of your colleagues to import modules, class modules and forms every time you amend the system; you just update the macro and forms in the workbook. Link the macro to the start event then all your colleagues have to do is click the workbook to open and they are straight into the macro. – Tony Dallimore Dec 03 '19 at 00:31