When my Word VBA form opens, why won't my the Open or Load events run

Viewed 21

I must be missing something basic (no pun intended), but I can't figure this out.

I have a MS Word VBA form called frmChooseMacros. I want to execute a subroutine on an Open or Load event for that form before the user interacts with the form.

From this topic https://docs.microsoft.com/en-us/office/vba/api/access.form.open, I came up with this code as a test and added it to the code window for frmChooseMacros:

Private Sub Form_Open(Cancel As Integer)
    MsgBox "Running open event"
End Sub

But it never executes when the form loads.

From this topic, https://docs.microsoft.com/en-us/office/vba/api/access.form.load, I also tried the Load event, like this, but still no joy:

Private Sub Form_Load()
    MsgBox "Running open event"
End Sub

I'm running the form through this sub-routine...

Sub DocFix_00_RunMultipleMacros()
    frmChooseMacros.Show
End Sub

...which executes when a button tied is clicked on the menu.

This image may help provide additional context: Word VBA IDE

Any help is appreciated. Thank you!

1 Answers

(Credit goes to @braX for helping me find the answer here.)

For Word, you have to use the UserForm_Initialize or UserForm_Activate events. There is no Form_Load or Form_Open events for Word.

I was able to solve this by first defining a UserForm object and setting it to my frmChooseMacros user form:

Sub DocFix_00_RunMultipleMacros()
    Dim oForm As UserForm
    Set oForm = New frmChooseMacros
    frmChooseMacros.Show
End Sub

Then I use the UserForm_Activate event here:

Private Sub UserForm_Activate()
  MsgBox "Form has activated."
End Sub
Related